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.
| 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 charset @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? |
 |
|
|
Trent
Starting Member
39 Posts |
Posted - 2005-06-01 : 12:03:12
|
| Incorrect syntax near '+'. |
 |
|
|
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, thenEXEC master..xp_cmdshell @cmd |
 |
|
|
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 @xI do what is below sometimes, that is what threw me off here.exec('declare @tempPIN intset @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)') |
 |
|
|
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'." |
 |
|
|
Trent
Starting Member
39 Posts |
Posted - 2005-06-01 : 13:39:21
|
| RESOLVEDJust had to have 4 single quotes on both sides of whatever @q gets set to |
 |
|
|
|
|
|