Re: Why can't variable assignment occur in a data retrieval operat
From: Novice (6tc1ATqlinkDOTqueensuDOTca)
Date: 11/22/04
- Next message: Deepak: "RE: Help with query"
- Previous message: Sparky: "Unable to step through SQL stored procedures in Visual Studio.Net 2003"
- In reply to: Adam Machanic: "Re: Why can't variable assignment occur in a data retrieval operation?"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 22 Nov 2004 15:53:07 -0800
You've answered my question - I didn't realize that the problem _wasn't_ that
it is not allowed to have both variable assingment in a select statement in a
stored procedure and then another select statement that uses those variables.
The actual restriction is that you can't have a single select statement both
assign to variables and return results.
I still find that strange - since the server should be able to return the
portion of the select statement that you are not assigning to variables - but
whatever.
Thanks for you help,
Novice
"Adam Machanic" wrote:
> That restriction is in place, I assume, because of the logical problem with
> how to construct a rowset when some of the rowset should be sent to the
> client and some assigned to a variable... But based on your narrative, I
> don't understand why you're running into the issue. Can you post some code?
>
> It's legal to do:
>
> SELECT @A = ColA,
> @B = ColB
> FROM YourTable
> WHERE ...
>
> SELECT *
> FROM OtherTable
> WHERE ColA = @A
> AND ColB = @B
>
>
>
> What's not legal is:
>
> SELECT @A = ColA,
> @B = ColB,
> ColC
> FROM YourTable
> WHERE ...
>
> --
> Adam Machanic
> SQL Server MVP
> http://www.sqljunkies.com/weblog/amachanic
> --
>
>
> "Novice" <6tc1ATqlinkDOTqueensuDOTca> wrote in message
> news:026E848F-B367-4F47-B518-CA17D02742E9@microsoft.com...
> > Hey all, I just ran across a peculiar "feature" in SQL server. I wrote
> this
> > stored procedure that is set up to return multiple result sets (from 3
> > different select statements). I could make the 3 different select
> statements
> > MUCH more efficient if I could assign some data from the first select
> > statement to a couple of variables and then use those variables in the
> next
> > two select statements.
> >
> > However, if I try to return any result sets from my stored procedure as
> well
> > as assign data from a previous select to a variable I get this error:
> > A SELECT statement that assigns a value to a variable must not be combined
> > with data-retrieval operation
> >
> > I have another way that I can construct the query - but I can't for the
> life
> > of me think of why SQL Server would prohibit this in a stored procedure.
> Is
> > this a security feature?
> >
> > Novice
>
>
>
- Next message: Deepak: "RE: Help with query"
- Previous message: Sparky: "Unable to step through SQL stored procedures in Visual Studio.Net 2003"
- In reply to: Adam Machanic: "Re: Why can't variable assignment occur in a data retrieval operation?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|