Set a parameter in select statement
I am trying to set a parameter in the select statement and then pass it to a user defined func开发者_如何学运维tion in the same statement. Is this possible? If yes, where is my mistake? If no, then I guess I will have to write a cursor and some temporary tables which I want to avoid.
declare @param varchar(1000)
select Pincode, Name,( @param = AlternateName) as AlternateName
,(select Top(1) part from SDF_SplitString (@param,',')) as NewName from PinCodeTable
You can either get all of the fields out as variables, or get the usual set of rows, but you can't mix and match in a single SELECT. Variables are limited to queries that return no more than one row. (Why do I feel like I'm about to learn something frightening?)
If you are writing a stored procedure and doing something like header/trailer rowsets, you can always return a rowset built from variables:
SELECT @Foo = Foo, @Bar = Bar, ... from Headers where Id = 42
SELECT @Foo as Foo -- Return the first rowset.
SELECT Size, Weight, Color from Trailers where Bar = @Bar -- Return second rowset.
精彩评论