June 18, 2015 at 1:28 am
How to put data from sql SERVER to notepad using t-sql
I want to run select * from t1 and result i want in Notepad saved in some location. Please help me how to do this.
June 18, 2015 at 1:43 am
you can use bcp command in command prompt or place the script in a batch file to do that i.e.:
bcp "select * from t1" queryout TextFile.txt -t, -c -S .\SQLINSTANCE -d DATABASE -T
June 18, 2015 at 1:50 am
Thanks for the reply....
But how to run this from SSMS
June 18, 2015 at 1:55 am
rajeshjaiswalraj (6/18/2015)
Thanks for the reply....But how to run this from SSMS
Have you tried selecting Query / Results To File?
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
June 18, 2015 at 1:59 am
you can use xp_cmdshell proc to run a command in command prompt.
EXEC xp_cmdshell 'bcp "SELECT * FROM database.dbo.table1" queryout "C:\Folder\TextFile.txt" -t, -c -S .\SQLEXPRESS -U user -P password'
June 18, 2015 at 2:10 am
Yes i have tried and got result below
Msg 102 , Level , State 1 , Line 17
incorrect syntax near 'queryout'.
I ran below command
bcp"select * from t1" queryout "e:ew folder\textfile.txt" -t, -c -s SQLONE -d DataAsets -T
I ran below command also
bcp"select * from t1" queryout "textfile.txt" -t, -c -s SQLONE -d DataAsets -T
Msg 102 , Level , State 1 , Line 17
incorrect syntax near 'queryout'.
June 18, 2015 at 2:27 am
You can't easily do this from T-SQL. T-SQL/SSMS executes inside of the server, not in the file system. There is no BULK EXPORT, mostly because the commands you run are on the server, not on your client. Your client sends them to the server.
Are you looking for something formatted for notepad? Or a CSV file? Does this need to be interactive (something you do) or automated (happens without you at your machine?)
You can certainly select all results in the grid (right click) and save this as a text file.
June 18, 2015 at 8:15 am
I am again getting error as
EXEC xp_cmdshell 'bcp "SELECT * FROM database.dbo.table1" queryout "C:\TextFile.txt" -t, -c -S SONY-VAIO\PROD -U sa -Password@12345 '
result -
'bcp' is not recognized as an internal or external command,
operable program or batch file.
Please help me on this
June 23, 2015 at 10:33 am
bcp isn't in the path on the server. The server service account (or xp_cmdshell proxy) is calling bcp, but can't find it.
Get the path on the server to bcp.exe and put the full path in the call.
June 23, 2015 at 2:16 pm
June 23, 2015 at 3:14 pm
Steve Jones - SSC Editor (6/23/2015)
bcp isn't in the path on the server. The server service account (or xp_cmdshell proxy) is calling bcp, but can't find it.Get the path on the server to bcp.exe and put the full path in the call.
BCP is normally automatically installed in a file path where SQL Server can find it when SQL Server is installed. Something else is going on here.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply