我正在使用SQL服务器。我有5个变量:@contentsType, @itemIDand, @dealerID, @userID,@itemID
如何在此查询中设置一次这些变量值?
SET @contentsType = (select top 1 mi.type_id from #tempResults as tr left join [myDB].[dbo].myTable mi on tr.item_id = mi.id)
SET @itemID = (select top 1 mi.item_id from #tempResults as tr left join [myDB].[dbo].myTable mi on tr.item_id = mi.id)
SET @dealerID = (select top 1 mi.dealer_id from #tempResults as tr left join [myDB].[dbo].myTable mi on tr.item_id = mi.id)
SET @userID = (select top 1 mi.user_id from #tempResults as tr left join [myDB].[dbo].myTable mi on tr.item_id = mi.id)
SET @itemID = (select top 1 id from #tempResults)
答案 0 :(得分:4)
您可以使用select set
select top 1
@contentsType = mi.type_id,
@itemID = mi.item_id,
@dealerID = mi.dealer_id,
@userID = mi.user_id
from #tempResults as tr
left join [myDB].[dbo].myTable mi on tr.item_id = mi.id;
select top 1 @itemID = id from #tempResults