如何将SELECT语句中的值赋给变量?

时间:2011-11-29 19:17:56

标签: sql sql-server database sql-server-2008

我正在尝试在我的存储过程中执行此操作:


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

但它不喜欢它的结构方式。任何想法?

2 个答案:

答案 0 :(得分:3)

这就是你要找的东西:

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

答案 1 :(得分:2)

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

话虽如此,请不要发布有关如何在SO上分配变量的问题。这实际上远低于网站的范围,可以通过查看SQL Server的任何文档来解决。