Didaxis
Didaxis

Reputation: 8756

How to assign a value from a SELECT statement into a variable?

I'm trying to do this in my stored procedure:


declare @CategoryId int

-- stuff

IF EXISTS (select A_categoryId from dbo.categories
           where B_categoryId = @b_categoryId
           and C_cactegoryId = @c_categoryId
          )
          -- it doesn't like the following line:
          @CategoryId = select A_categoryId from dbo.categories
           where B_categoryId = @b_categoryId
           and C_cactegoryId = @c_categoryId

but it doesn't like the way it's structured. any ideas?

Upvotes: 1

Views: 203

Answers (2)

Adriano Carneiro
Adriano Carneiro

Reputation: 58665

This is what you are looking for:

set @CategoryId = (select A_categoryId from dbo.categories
       where B_categoryId = @b_categoryId
       and C_cactegoryId = @c_categoryId)

Upvotes: 3

JNK
JNK

Reputation: 65217

SELECT @CategoryId = A_categoryId from dbo.categories...

That being said, please don't post questions about how to assign variables on SO. This is really way below the scope of what the site is about and could have been resolved by reviewing any documentation for SQL Server.

Upvotes: 2

Related Questions