I am trying to create a stored procedure in SSMS to export the query result to CSV. But I am getting below error while creating.
SQL statement:
CREATE PROCEDURE SelectUsers
AS
SELECT *
FROM [IMBookingApp].[dbo].[usertest]
INTO OUTFILE 'C:/S3/users.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
GO;
Error
Msg 156, Level 15, State 1, Procedure SelectUsers, Line 4 [Batch Start Line 0]
Incorrect syntax near the keyword 'INTO'
Any help would be appreciated.
try the following:
exec master..xp_cmdshell 'bcp "[IMBookingApp].[dbo].[userTEST]" out "c:\S3\users.csv" -c -t, -T'
or try
bcp "select * from [IMBookingApp].[dbo].[userTEST]" queryout "c:\S3\users.csv" -c -t, -T
from the command line
CREATE PROCEDURE SelectUsers AS
SELECT * INTO OUTFILE 'C:/S3/users.csv'
FROM [IMBookingApp].[dbo].[usertest]
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';GO;
You can refer to this blog and this is going to resolve you issue for sure. https://www.sqlservercentral.com/blogs/export-a-ssms-query-result-set-to-csv
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.