Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team,

Please look into below code anda advise me how to pass currentdate in out parameter of sqlcmd

code
SQL
Declare 
			@FDate as varchar(10)
	
	set @Fdate = (SELECT CONVERT(INT,CONVERT(CHAR(8),GETDATE()-1,112))) 
	EXEC MASTER.DBO.XP_CMDSHELL 'SQLCMD -S .\ -d SFTPWELSPUN -E -Q "SELECT MAINTAG,INVOICENUMBER,INVOICEDATE,TYPE,SERVICETYPE,ITENARY,DAYS,ADVANCE_PURCHASE,TICKET_TYPE,OPTION1,OPTION2,OPTION3,NET_LOST_SAVING,LOWEST_FARE,HIGHEST_FARE FROM DBO.TBL_SFTPWELSPUN_MASTER where convert(date,invoicedate)=convert(date,getdate()-2) ORDER BY INVOICEDATE " -s "," -o"E:\Report\Details_'+@FDATE+'.CSV"'

Please advise

What I have tried:

error while executing the code
SQL
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '+'.
Posted
Updated 8-Jan-22 3:26am
v3

1 solution

The parameter for a xp_cmdshell needs to be one string. One way to do this is to concatenate the command to a separate variable first and then use it. For example
SQL
Declare 
   @FDate as varchar(10),
   @commandtext as varchar(max)
set @Fdate = (SELECT CONVERT(INT,CONVERT(CHAR(8),GETDATE()-1,112))) 
set @commandtext = 'SQLCMD -S .\ -d SFTPWELSPUN -E -Q "SELECT MAINTAG,INVOICENUMBER,INVOICEDATE,TYPE,SERVICETYPE,ITENARY,DAYS,ADVANCE_PURCHASE,TICKET_TYPE,OPTION1,OPTION2,OPTION3,NET_LOST_SAVING,LOWEST_FARE,HIGHEST_FARE FROM DBO.TBL_SFTPWELSPUN_MASTER where convert(date,invoicedate)=convert(date,getdate()-2) ORDER BY INVOICEDATE " -s "," -o"E:\Report\Details_' + @FDATE + '.CSV"'
EXEC MASTER.DBO.XP_CMDSHELL @commandtext
 
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