Unable to create a stored procedure in SSMS

感情迁移 提交于 2020-01-25 07:31:16

问题


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.


回答1:


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




回答2:


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;



回答3:


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



来源:https://stackoverflow.com/questions/59602890/unable-to-create-a-stored-procedure-in-ssms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!