shashi
shashi

Reputation: 4696

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 function 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

Upvotes: 2

Views: 22798

Answers (1)

HABO
HABO

Reputation: 15816

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.

Upvotes: 3

Related Questions