WITH sql中的dml查询

时间:2013-05-09 13:09:32

标签: sql sql-server sql-server-2008 tsql common-table-expression

我只是假设我可以在SQL Server 2008中的set query语句中添加WITH

所以我写了几行代码:

with temp as   
(
    SELECT row_number() OVER (ORDER BY [ShareItems].ItemId) AS Row, ItemId FROM [ShareItems]
)
set @row = ItemId //<---error

但我收到上述错误,但如果使用select关键字WITH语句,则没有错误???

with temp as   
(
    SELECT 
        row_number() OVER (ORDER BY [ShareItems].ItemId) AS Row, ItemId 
    FROM [ShareItems]
)
SELECT ItemId 
FROM temp //<--- OK (no error)

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

尝试这种方式:

with temp as   
(
  SELECT row_number() OVER (ORDER BY [ShareItems].ItemId) AS Row, ItemId 
  FROM [ShareItems]
)

select  @row = ItemId from temp 

答案 1 :(得分:2)

试试这个:

with temp as   
    (
        SELECT row_number() OVER (ORDER BY [ShareItems].ItemId) AS Row, ItemId FROM [ShareItems]
    )
SELECT @row = ItemId FROM temp