I'm missing something
declare @servername varchar(2000)
set 开发者_如何学Go@EmriServerit=(select @@servername)
declare @dbname varchar(2000)
set @dbname ='Test1'
declare @Dir varchar(2000)
set @Dir='F:\dataclient.sql'
exec master.dbo.xp_cmdshell 'osql -E -S ' + @servername+ ' -d ' +
@dbname +' -i ' + @Dir
It gives me and error: "incorrect sysntax near +"
If i don't use variables it works ok. What am i missing! Thanks in advance
Additions are not allowed in a parameter list. Move them to a separate line, like:
declare @cmd varchar(500)
set @cmd = 'osql -E -S ' + @servername+ ' -d ' + @dbname +' -i ' + @Dir
exec master.dbo.xp_cmdshell @cmd
You don't assign a value to @servername
I would try to concatenate together your command string first, and then check it before executing:
declare @servername varchar(2000)
set @servername = select @@servername
declare @dbname varchar(2000)
set @dbname ='Test1'
declare @Dir varchar(2000)
set @Dir='F:\dataclient.sql'
declare @execCmd varchar(max)
set @execCmd = 'osql -E -S ' + @servername+ ' -d ' + @dbname +' -i ' + @Dir
print @execCmd -- what do you get as output here??
exec master.dbo.xp_cmdshell @execCmd
Try putting the entire exec
on one line (this is a bit of a stab in the dark, I confess).
try :
declare @EmriServerit varchar(2000)
set @EmriServerit=(select @@servername)
declare @dbname varchar(2000)
set @dbname ='Test1'
declare @Dir varchar(2000)
set @Dir='F:\dataclient.sql'
declare @Command varchar(500)
set @command='osql -E -S ' + @EmriServerit+ ' -d ' +
@dbname +' -i ' + @Dir
exec master.dbo.xp_cmdshell @command
you had the server name declared and used differently, and you are not permitted to have expressions as parameters to a stored procedure, so concatenate the command into a variable and then pass it to the master.dbo.xp_cmdshell stored procedure.
精彩评论