2011-11-29 143 views
1

我試圖做到這一點在我的存儲過程:如何將一個SELECT語句的值賦給一個變量?

 

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 

,但它不喜歡它的結構化的方式。有任何想法嗎?

+0

@downvoter -care評論? – Didaxis

回答

2

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

話雖這麼說,請不要發佈有關如何在SO變量賦值的問題。這實際上低於網站的範圍,可以通過查看SQL Server的任何文檔來解決。

+0

如果一個有效的問題,有人不能回答我不明白他爲什麼不能發表這樣的問題。如果你不想要這個問題,你爲什麼要發佈答案? – JonH

+0

@JonH - '太本地化了'我覺得它涵蓋了很多東西。這個問題幾乎保證不會在將來幫助其他任何人。 – JNK

+2

你會感到驚訝 - 特別是TSQL。 – JonH

3

這是你在找什麼:

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