DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
 
 
SITE SEARCH SQLUSA.com VIDEOS
 
 
SQL E/BOOKS   WORLD, USA & SQL NEWS   FORMAT
SCRIPTS SQL 2005 SQL 2008 ARTICLES
How to export query results into a file?

The following SQL Server stored procedure will export the query results into a flat file in the built-in folder. It assumes Windows authentication.

-- T-SQL stored procedure to export query results to file

CREATE PROC ExportQueryResultToFile(

           @Query    VARCHAR(1024),

           @FileName VARCHAR(35))

AS

  BEGIN

    DECLARE  @FolderAndFileName VARCHAR(100)

    

    SET @FolderAndFileName = 'e:\data\export\' + @FileName

    

    SET @Query = 'sqlcmd -E -Slocalhost -q"SET NOCOUNT ON;' + @Query + '" -s"," -w3000 -n -o "' + @FolderAndFileName + '"'

    

    EXEC MASTER..xp_cmdshell

      @Query ,

      NO_OUTPUT

  END

 

GO

 

-- To execute it:

EXEC ExportQueryResultToFile

  'select * from pubs.dbo.authors where state=''CA''' ,

  'authors.txt'

 

Exam Prep 70-461
SQL 2016 DESIGN & PROGRAMMING
 
 
 
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE