How to produce an csv output file from stored procedure in SQL Server

后端 未结 7 827
孤独总比滥情好
孤独总比滥情好 2020-12-03 14:01

Is it possible to generate a csv file from a stored procedure in SQL Server? I created my stored procedure and I want to stored some result as csv, does someone know how to

相关标签:
7条回答
  • 2020-12-03 14:41

    Found a really helpful link for that. Using SQLCMD for this is really easier than solving this with a stored procedure

    http://www.excel-sql-server.com/sql-server-export-to-excel-using-bcp-sqlcmd-csv.htm

    0 讨论(0)
  • 2020-12-03 14:46

    I have tried this and it is working fine for me:

    sqlcmd -S servername -E -s~ -W -k1 -Q  "sql query here" > "\\file_path\file_name.csv"
    
    0 讨论(0)
  • 2020-12-03 14:49

    Just to answer a native way to do this that finally worked, everything had to be casted as a varchar

    ALTER PROCEDURE [clickpay].[sp_GetDocuments] 
    
    @Submid bigint
    
    AS
    BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
       DECLARE @raw_sql varchar(max)
    
    DECLARE @columnHeader VARCHAR(max)
    SELECT @columnHeader = COALESCE(@columnHeader+',' ,'')+ ''''+column_name +'''' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Documents'
    
    DECLARE @ColumnList VARCHAR(max)
    SELECT @ColumnList = COALESCE(@ColumnList+',' ,'')+ 'CAST('+column_name +' AS VARCHAR)' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Documents'
    
    SELECT @raw_sql = 'SELECT '+ @columnHeader +' UNION ALL SELECT ' + @ColumnList + ' FROM ' + 'Documents where Submid = ' +  CAST(@Submid AS VARCHAR)
    
    -- Insert statements for procedure here
    exec(@raw_sql)
      END
    
    0 讨论(0)
  • 2020-12-03 14:59

    I think it is possible to use bcp command. I am also new to this command but I followed this link and it worked for me.

    0 讨论(0)
  • 2020-12-03 14:59

    I also used bcp and found a couple other helpful posts that would benefit others if finding this thread

    Don't use VARCHAR(MAX) as your @sql or @cmd variable for xp_cmdshell; you will get and error

    Msg 214, Level 16, State 201, Procedure xp_cmdshell, Line 1 Procedure expects parameter 'command_string' of type 'varchar'.

    http://www.sqlservercentral.com/Forums/Topic1071530-338-1.aspx

    Use NULLIF to get blanks for the csv file instead of a NUL (viewable in hex editor, or notepad++). I used that in the SELECT statement for bcp

    How to make Microsoft BCP export empty string instead of a NUL char?

    0 讨论(0)
  • 2020-12-03 15:04

    You can do this using OPENROWSET as suggested in this answer. Reposting Slogmeister Extrarodinare answer:

    Use T-SQL

    INSERT INTO OPENROWSET ('Microsoft.ACE.OLEDB.12.0','Text;Database=D:\;HDR=YES;FMT=Delimited','SELECT * FROM [FileName.csv]')
    SELECT Field1, Field2, Field3 FROM DatabaseName
    

    But, there are couple of caveats:

    1. You need to have the Microsoft.ACE.OLEDB.12.0 provider available. The Jet 4.0 provider will work, too, but it's ancient, so I used this one instead.

    2. The .CSV file will have to exist already. If you're using headers (HDR=YES), make sure the first line of the .CSV file is a delimited list of all the fields.

    0 讨论(0)
提交回复
热议问题