Desperate about the use of ' and " within an SQL statement!

  • I really hope some1 can point out what I am doing wrong here. I receive a syntax error on below statement, while checking each and every statement alone gives no error:

    sp_configure 'xp_cmdshell', 1;

    GO

    RECONFIGURE;

    GO

    DECLARE @sql NVARCHAR(1000)

    SELECT @sql = N'exec master..xp_cmdShell ''bcp DBA.dbo.x in \\RemoteServer\e$\DIR\Filename.csv -F02 /Sservname\Instancename -T /c /t;'''

    sp_executesql @sql

    GO

    sp_configure 'xp_cmdshell', 0;

    GO

    RECONFIGURE;

    GO

    I'm pretty sure it has to do with the SELECT @sql statement, but I cant figure it out anymore. I have tried several combinations of ' and ", to no avail. Please tell me how to write this statement correctly. The BCP-statement works correctly.

    TIA

    Greetz,
    Hans Brouwer

  • Use EXECUTE sp_executesql @sql

    Instead of sp_executesql @sql

  • Hi there, please move the semicolon at the end of the statement at the penultimate place.

    Like this

    SELECT @sql = N'exec master..xp_cmdShell ''bcp DBA.dbo.x in \\RemoteServer\e$\DIR\Filename.csv -F02 /Sservname\Instancename -T /c /t'';'

    Tell us back if that solved teh issue!

  • EXEC sp_executesql did it...

    (burries his head in both hands and starts to cry)

    Tnx for answering both.

    Greetz,
    Hans Brouwer

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply