日期:2014-05-18  浏览次数:20544 次

Select 结果赋值求救。。
问题描述如下:
Declare @Content varchar
Select top 1 Content From db.Table where Content like '%**%'

我想把这个结果赋值给变量@Content,应该如何操作呢?

------解决方案--------------------
Declare @Content varchar
select @Content=content from 
(Select top 1 Content From db.Table where Content like '%**%') 


------解决方案--------------------
Declare @Content varchar
select @Content=(Select top 1 Content From db.Table where Content like '%**%')
select @Content
------解决方案--------------------
SQL code
Declare @Content varchar(1000)

set @Content=(Select top 1 Content From db.Table where Content like '%**%')

--or

Select top 1 @Content=Content From db.Table where Content like '%**%'

------解决方案--------------------
SQL code
----set赋值不就可以了
Declare @Content varchar
set @Content=(Select top 1 Content From db.Table where Content like '%**%')

------解决方案--------------------
select 
set 
建议用set
------解决方案--------------------
直接set @Content=(Select top 1 Content From db.Table where Content like '%**%')
------解决方案--------------------
这里用set 和select 赋值都可以
------解决方案--------------------
Declare @Content varchar(1000)
set @Content=(Select top 1 Content From db.Table where Content like '%**%')
或者
Select top 1 @Content=Content From db.Table where Content like '%**%'

------解决方案--------------------
楼上都回答完了。
------解决方案--------------------
直接用set或select都可以
------解决方案--------------------
--方法一
DECLARE @Name VARCHAR(50);
SELECT TOP 1 @Name=Name FROM dbo.DataDictional
PRINT @Name
--方法二
SELECT @Name=(SELECT TOP 1 Name FROM dbo.DataDictional)
PRINT @Name
--方法三

SELECT @Name=Name FROM (SELECT TOP 1 Name FROM dbo.DataDictional) AS t

PRINT @Name