September 14, 2006 at 6:02 pm
I'm simplifying an example of a problem I'm having. I'm getting the error, must declare the variable. This is probably a stupid looking example but if I can get this to work, I'm thinking I'll be able to get my real query to work. Thanks!!
DECLARE @UseIt1 as varchar(100)
EXEC ('SELECT @UseIt1 = A.AcctNo ' +
'FROM QueueOrderExcessDol A ' +
' INNER JOIN _FinalFlat B
ON A.AcctNo = B.AcctNo
INNER JOIN QueueComboBox D
ON B.WorkQueue = D.ProgramKeyField
WHERE B.Tagged = 0
AND D.ProgramKeyField = @WorkQueue ' +
' ORDER BY RiskId')
September 14, 2006 at 6:45 pm
try like this
DECLARE @UseIt1 varchar(100),
@WorkQueue varchar(100)
EXEC ('SELECT A.AcctNo ' +
'FROM QueueOrderExcessDol A ' +
' INNER JOIN _FinalFlat B
ON A.AcctNo = B.AcctNo
INNER JOIN QueueComboBox D
ON B.WorkQueue = D.ProgramKeyField
WHERE B.Tagged = 0
AND D.ProgramKeyField ='''+ @WorkQueue +''' ORDER BY RiskId')
September 14, 2006 at 6:50 pm
Well in the context of your EXEC(), the variable @WorkQueue is not declared. If you've declared it earlier you'll need to either parse it in or pass it in via sp_executesql (I'd recommend you use the latter as it's the proper way to do this).
September 14, 2006 at 7:07 pm
Yikes, I'm getting tired. I made a simple example but posted the wrong one. Here is the simple example.
DECLARE @UseIt1 as varchar(100)
EXEC ('SELECT @UseIt1 = 1')
September 14, 2006 at 7:39 pm
You'd have to use dynamic sql for this...also, your select should be a "set"..
DECLARE @UseIt1 as varchar(100) EXEC ('SET ' + @UseIt1 + ' = 1')
**ASCII stupid question, get a stupid ANSI !!!**
September 14, 2006 at 9:03 pm
Sushila,
You're having one of those days like I did on the trigger thing...
DECLARE @UseIt1 as varchar(100)
EXEC ('SET ' + @UseIt1 + ' = 1')
PRINT @UseIt1
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '='.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 14, 2006 at 9:14 pm
SwTrans,
I guess I don't see what your problem is or what you're actually trying to do... why wouldn't the following work for you?
DECLARE @UseIt1 as varchar(100)
DECLARE @WorkQueue AS VARCHAR(100)
SET @WorkQueue = somestringvalue
SELECT @UseIt1 = A.AcctNo
FROM QueueOrderExcessDol A
INNER JOIN _FinalFlat B
ON A.AcctNo = B.AcctNo
INNER JOIN QueueComboBox D
ON B.WorkQueue = D.ProgramKeyField
WHERE B.Tagged = 0
AND D.ProgramKeyField = @WorkQueue
ORDER BY RiskId
You have no variable column names, no variable table names... the above should work just fine... or.... perhaps you're building the query in pieces from a GUI? More info, please...
--Jeff Moden
Change is inevitable... Change for the better is not.
September 15, 2006 at 5:55 am
At least your "trigger moments" are very few and far between - mine're increasing at an alarming frequency..
However, I do not think you can use "PRINT @UseIt1" for testing since I believe the dynamic sql is in its' own scope...
**ASCII stupid question, get a stupid ANSI !!!**
September 15, 2006 at 6:18 am
Heh, heh.... mine are getting worse... old brain.
You hit the nail on the head about the scope thing... I was curious as to why you declared the variable external to the dynamic SQL and then tried to set it's value within the dynamic SQL. The only place I've seen that work is with sp_ExecuteSQL.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply