从select语句中指定计数值

时间:2014-07-30 09:06:28

标签: sql sql-server

我有一个简单的选择,如何将select语句的计数分配给@count

DECLARE @count int
SELECT Tag FROM @Tags intersect SELECT Tag from @TagsFromRecord

set @count= (select count(1) 
             from (SELECT Tag 
                   FROM @Tags 
                   intersect 
                   SELECT Tag 
                   from @TagsFromRecord))  >> incorrect syntax

select @count= count(1) from (SELECT Tag 
                              FROM @Tags 
                              intersect 
                              SELECT Tag 
                              from @TagsFromRecord)

2 个答案:

答案 0 :(得分:1)

你需要一个别名:

DECLARE @count int

set @count= 
(
    select count(1) 
    from 
    (
        SELECT Tag FROM @Tags 
        intersect 
        SELECT Tag from @TagsFromRecord
    ) AS T
)

答案 1 :(得分:0)

我认为,这也可行,

DECLARE @count int

select @count = count(1) 
from 
(
    SELECT Tag FROM @Tags 
    intersect 
    SELECT Tag from @TagsFromRecord
) AS T