2013-01-09 46 views
2

我想從子查詢中選擇多個變量以便稍後使用。這裏是我的代碼:如何在T-SQL中分配多個內聯變量?

DECLARE @MarketplaceName VARCHAR(50), 
    @ProductFieldID INT, 
    @FromValue VARCHAR(255) = (
     SELECT mcvm.MarketplaceName, 
      mcvm.ProductFieldID, 
      mcvm.FromValue 
     FROM [Meta].[MarketplaceCategoryValueMapping] AS mcvm 
     WHERE mcvm.MarketplaceCategoryValueMappingID = @MapID 
     ) 

我收到以下錯誤信息:

只有一個表達式可以在選擇列表時,子查詢不與EXISTS引入的指定。

我試圖映射@MarketplaceNamemcvm.MarketplaceName

我想是沒有寫每個變量的多個SELECT語句。

回答

8

你想要做到這一點嗎?

DECLARE @MarketplaceName VARCHAR(50), 
     @ProductFieldID INT, 
     @FromValue VARCHAR(255) 
SELECT @MarketplaceName = mcvm.MarketplaceName, 
     @ProductFieldID  = mcvm.ProductFieldID, 
     @FromValue   = mcvm.FromValue 
FROM [Meta].[MarketplaceCategoryValueMapping] AS mcvm 
WHERE mcvm.MarketplaceCategoryValueMappingID = @MapID 
+0

是的,就是這樣。謝謝。 – Codeman

+0

非常歡迎! – MarkD

3

這就是你如何做到的。此外,這不是一個子查詢,它只是一個查詢。

DECLARE @MarketplaceName VARCHAR(50), 
     @ProductFieldID INT, 
     @FromValue VARCHAR(255) 


SELECT @MarketplaceName = mcvm.MarketplaceName, 
     @ProductFieldID = mcvm.ProductFieldID, 
     @FromValue = mcvm.FromValue 
FROM [Meta].[MarketplaceCategoryValueMapping] AS mcvm 
WHERE mcvm.MarketplaceCategoryValueMappingID = @MapID 
+0

Gotcha,我想嘗試用子查詢來做到這一點讓我感到困惑。謝謝! – Codeman