SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Execute cmd shell from a TSQL
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

xhostx
Constraint Violating Yak Guru

USA
261 Posts

Posted - 07/10/2012 :  08:30:55  Show Profile  Reply with Quote
Hi all,
I have the following statement that is part of my TSQL transactions. I'm trying to select multiple rows with a Available condition in the where clause. However, the single quotes issues has emerged because the xp_cmdshell procedures needs too.
example:
exec master..xp_cmdshell 'isql /Uxx /Pxx /db /h-1 /Q"set nocount on select distinct device from devices where online = '+'''Available'''+'" /C:\tmp\work\file1.txt'

when I try to run, this is what I get:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '+'.

Any idea please...or suggestions.

Thanks,

--------------------------
Get rich or die trying
--------------------------

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3442 Posts

Posted - 07/10/2012 :  08:33:38  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
you can't do a calculation when passing values as parameters. This should word

BEGIN TRAN

DECLARE @foo VARCHAR(8000) =  'isql /Uxx /Pxx /db /h-1 /Q"set nocount on select distinct device from devices where online = '+'''Available'''+'" /C:\tmp\work\file1.txt'
exec master..xp_cmdshell @foo

ROLLBACK


Transact Charlie

Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
Go to Top of Page

xhostx
Constraint Violating Yak Guru

USA
261 Posts

Posted - 07/10/2012 :  08:47:48  Show Profile  Reply with Quote
quote:
Originally posted by Transact Charlie

you can't do a calculation when passing values as parameters. This should word

BEGIN TRAN

DECLARE @foo VARCHAR(8000) =  'isql /Uxx /Pxx /db /h-1 /Q"set nocount on select distinct device from devices where online = '+'''Available'''+'" /C:\tmp\work\file1.txt'
exec master..xp_cmdshell @foo

ROLLBACK



Transact Charlie

Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.



Thanks a lot for your help.
this works perfect. I'm wondering if is it possible to assign directly as you wrote in your example or should I use SELECT/SET keywords?

Thanks.

--------------------------
Get rich or die trying
--------------------------
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8515 Posts

Posted - 07/10/2012 :  08:52:09  Show Profile  Visit webfred's Homepage  Reply with Quote
Since sql server 2008 it is possible to assign the value in the declare statement.



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3442 Posts

Posted - 07/10/2012 :  08:52:52  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
you can assign directly as long as you are using 2008.

On earlier editions you would probably do something like
BEGIN TRAN

DECLARE @onlineParam VARCHAR(8000)
DECLARE @command VARCHAR(8000)
DECLARE @outputFile VARCHAR(8000)

SET @onlineParam = 'Available'
SET @outputFile = 'C:\tmp\work\file1.txt'
SET @command = 'isql /Uxx /Pxx /db /h-1 /Q"set nocount on select distinct device from devices where online = ' + QUOTENAME(@onlineParam, '''')  + '" /' + @outputFile

exec master..xp_cmdshell @command

ROLLBACK


Transact Charlie

Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.

Edited by - Transact Charlie on 07/10/2012 08:53:54
Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3442 Posts

Posted - 07/10/2012 :  08:54:45  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
Note that
QUOTENAME(@var, '''')

Will escape the value of @var in single quotes -- it's a nice safe way of dealing with strings (avoiding sql injection of the parameter)

Transact Charlie

Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
Go to Top of Page

xhostx
Constraint Violating Yak Guru

USA
261 Posts

Posted - 07/10/2012 :  09:09:10  Show Profile  Reply with Quote
quote:
Originally posted by Transact Charlie

Note that
QUOTENAME(@var, '''')

Will escape the value of @var in single quotes -- it's a nice safe way of dealing with strings (avoiding sql injection of the parameter)

Transact Charlie

Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.



Thank you T_charlie, and Webfred too.

This tricks were helpful.

Thanks again.

--------------------------
Get rich or die trying
--------------------------
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000