Export result of a select to a text file [closed]

0

I have a SELECT in SQL Server and I want to export it to a text file (result.txt), I can see that it can be done with some programming language, but I want to do it with TSQL. If it is possible?

    
asked by brcordova 28.11.2017 в 23:23
source

1 answer

-1

At least for SQL Server you should do:

You must first run these queries

EXEC master.dbo.sp_configure ‘show advanced options’, 1
RECONFIGURE
EXEC master.dbo.sp_configure ‘xp_cmdshell’, 1
RECONFIGURE

this is to make sure that the xp_cmdshell is enabled

With step 2 you must use this command

EXEC xp_cmdshell ‘bcp “SELECT * FROM sysfiles” queryout “C:\bcptest.txt” -T -c -t,’

Here we have to pass the query as we need it, and we pass the path where we want to create the text file (the route must be local)

The parameters that are being used are:

Queryout - is the one that allows you to specify the query with which you will work

File name - where the result will be inserted (it must be the full path)

-T , which specifies that the bcp utility connect to SQL Server with a secure connection, you can use -P (password) and -U (user)

-c , specifies the type of characters that will be used for each field

-t , allows you to specify the field delimiter, the character that is specified after -t will be the one that separates each field

-S , can be used to specify the name of the server. If you have a named Instance, it will be necessary to use this parameter. Something like,

EXEC xp_cmdshell ‘bcp “select name, type_desc, create_date from sys.objects” queryout “C:\bcptest2.txt” -T -SDEVELOP\DEV1 -c -t,’
    
answered by 29.11.2017 / 15:10
source