create a stored procedure to create a txt file

  • Hi,

    I need to create a stored procedure that accepts a parameter, this paramentes is a name. When i pass the name to the stored procedure, this procedure creates a empty txt file with the name that i passed as the parameter.

    Is it possible to do this using t-sql?

    Thank you

  • I can't believe this is the slickest way to achieve what you're after, but this works :w00t:

    [font="Arial Black"]CREATE PROCEDURE EXPORT_DATA

    @FileName varchar(255)

    as

    begin

    declare @bcpCommand varchar(255), @Result int

    set @bcpCommand = 'bcp "Select 1 as a" queryout "' + @FileName + '" -c -t, -T -S '

    exec @Result = master..xp_cmdshell @bcpCommand, no_output

    end[/font]

    Don't forget that xp_cmdshell is not enabled in the db configuration by default

    Mike.

  • Is there any other way to create store procedure

  • You can use sp_OACreate to access the the File System Object (Active X)

    reading & writing files in sql server[/url]

    Director of Transmogrification Services
  • Hi,

    Here the txt file will be created in server. Is it possible to create a file in local machine?

  • The file will be created from the context of the sql server, so you should be able to write it to any drive or share the sql server has access to.

  • A little more information would be nice as to what the big picture of this is... Is the parameter some data that needs to be retrieved from SQL? I could whip out a VBS script to hit the DB, grab the value, and pump it into the script. Powershell possibly?

    Director of Transmogrification Services
  • Why not use an SSIS package to generate the file?

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply