Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 exec statement

Author  Topic 

Trent
Starting Member

39 Posts

Posted - 2005-06-01 : 11:43:55
Does anyone know how I can pass a variable into the select statement below?
declare @x char
set @x = 'Q'

EXEC master..xp_cmdshell
'bcp "select * from MyDB.dbo.myTable where Source = '+@x+' " queryout \\Destination\Folder\Test.txt -c -Usa -Ppass'

The above example doesn't work, but it illustrates what I'm trying to do.
Thank You for any help.

SamC
White Water Yakist

3467 Posts

Posted - 2005-06-01 : 11:57:23
What error do you get?
Go to Top of Page

Trent
Starting Member

39 Posts

Posted - 2005-06-01 : 12:03:12
Incorrect syntax near '+'.
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2005-06-01 : 12:07:11
Easy. You can't perform string evaluations on the EXEC statement. Calculate it beforehand, then

EXEC master..xp_cmdshell @cmd

Go to Top of Page

Trent
Starting Member

39 Posts

Posted - 2005-06-01 : 12:26:22
Thank you for your help. Below is what I did and it is working great.

declare @q varchar(5)
set @q = '00000'

declare @x varchar(250)
set @x = ' bcp "select * from myDB.dbo.myTable where SiteID = "' + @q + '" " queryout \\Destination\Folder\Test.txt -c -Usa -Ppass'

EXEC master..xp_cmdshell @x

I do what is below sometimes, that is what threw me off here.
exec('
declare @tempPIN int
set @tempPIN = CAST(((8999 * RAND())+ 1000) AS int)
declare @PIN char(4)
set @PIN = CAST(@tempPIN AS char(4))
insert into MyDB.dbo.'+@StudyNoS+' values ('+@NextTransX+', '+@SerialNo+', '+@TID+', ''A'', 0, @PIN)
')
Go to Top of Page

Trent
Starting Member

39 Posts

Posted - 2005-06-01 : 12:55:01
NEW PROBLEM:

Whenever the variable (@q) contains a character, not all integers, I get an "Incorrect syntax near 'whatever the character is'."
Go to Top of Page

Trent
Starting Member

39 Posts

Posted - 2005-06-01 : 13:39:21
RESOLVED

Just had to have 4 single quotes on both sides of whatever @q gets set to
Go to Top of Page
   

- Advertisement -