views:

78

answers:

5
declare @servername varchar(2000)
set @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

A: 

Try putting the entire exec on one line (this is a bit of a stab in the dark, I confess).

Marcelo Cantos
That's my fault... I made a line break when I reformtted the code.
Anders Abel
+2  A: 

You don't assign a value to @servername

Álvaro G. Vicario
I'm sorry i wrote it wrong on the post!It is declare as servername
Redi
+3  A: 

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
Andomar
thanks this is the thing!
Redi
+1  A: 

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
marc_s
Thanks this was helpful
Redi
A: 

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.

KM