Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored Procedure: Reading a .txt File and return Text

Status
Not open for further replies.

sam4help

Programmer
Jul 22, 2011
74
AE
I need a Stored Procedure that reads a text file and give the text as output;
I have one in MS SQL that works well; can anyone help in converting that into MySQL Stored Procedure.

I will be passing the Full Path of the File as parameter.

Code:
/****** Object:  StoredProcedure [dbo].[readTextFile]    Script Date: 08/06/2011 13:08:59 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROC [dbo].[readTextFile]  
    @os_file_name NVARCHAR(256) 
   ,@text_file NVARCHAR(MAX) OUTPUT  
/* Reads a text file into @text_file */  
AS  
DECLARE @sql NVARCHAR(MAX) 
      , @parmsdeclare NVARCHAR(MAX)  

SET NOCOUNT ON  

SET @sql = 'select @text_file=(select * from openrowset ( 
           bulk ''' + @os_file_name + ''' 
           ,SINGLE_CLOB) x 
           )' 

SET @parmsdeclare = '@text_file NVARCHAR(max) OUTPUT'  

EXEC sp_executesql @stmt = @sql 
                 , @params = @parmsdeclare 
                 , @text_file = @text_file OUTPUT 

-- Example calling above proc
--DECLARE @t NVARCHAR(MAX) 
--EXEC readTextFile 'c:\readMe.txt', @t output 
GO
 
Thank goodness MySQL cannot just read any file from the OS. Imagine how a simple SQL injection could return the contents of sensitive (and legible by the database server user) files. MySQL can read CSV files, but this feature is disabled on many machines also for the same reasons.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Thanks for the reply DonQuichote,
So you mean to say its not possible in MySQL. That sounds strange :(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top