Click here to Skip to main content
15,896,359 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have written one bcp command in sql to create text file..

please help me how to corect this query..

declare @bcpcmd varchar(50),
declare @query varchar(500),
declare @filepath varchar(500),
declare @separator varchar(1)
set @query='select * from tbl_fs_mst'
set @filepath='D:pulse'
set @separator=' '

set @bcpcmd='bcp "'+@query+'" queryout '+@filepath+' -T -c -t "'+@separator+'"'

exec master..xp_cmdshell @bcpcmd

please find the error statment below

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'declare'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'declare'.
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'declare'.
Msg 137, Level 15, State 1, Line 5
Must declare the scalar variable "@query".
Msg 137, Level 15, State 1, Line 6
Must declare the scalar variable "@filepath".
Msg 137, Level 15, State 2, Line 9
Must declare the scalar variable "@query".
Msg 137, Level 15, State 2, Line 11
Must declare the scalar variable "@bcpcmd".
Posted
Comments
barneyman 22-Jun-15 2:12am    
all the clues are there

1 solution

If you want to declare several variables with a comma separated list you need DECLARE only once!
SQL
declare @bcpcmd varchar(50),
@query varchar(500),
@filepath varchar(500),
@separator varchar(1)

Or you can write every variable with its own DECLARE on separate lines, but in that case you need no commas between them (it is a syntax error)
SQL
declare @bcpcmd varchar(50)
declare @query varchar(500)
declare @filepath varchar(500)
declare @separator varchar(1)

(notice the lack of commas here)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900