变量中的单行集

时间:2012-10-05 20:21:46

标签: sql sql-server-2008 sql-server-2005 sql-server-2008-r2

我被困在这个地方。我试图在变量中获得单个行集值。

DECLARE @last_timestamp as varchar;
SET @last_timestamp = SELECT MAX(modify_timestamp) FROM dbo.table1
PRINT @last_timestamp

返回以下错误

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'SELECT'.

Cursor是解决此问题的唯一方法。任何帮助都会有所帮助吗?

3 个答案:

答案 0 :(得分:1)

SELECT @last_timestamp := MAX(modify_timestamp) FROM dbo.table1

SELECT MAX(modify_timestamp) INTO @last_timestamp FROM dbo.table1 

SET @last_timestamp = (SELECT MAX(modify_timestamp) FROM dbo.table1)

答案 1 :(得分:0)

您可以在SELECT语句

周围放置括号
DECLARE @last_timestamp as varchar;
SET @last_timestamp = (SELECT MAX(modify_timestamp) FROM dbo.table1)
PRINT @last_timestamp

答案 2 :(得分:0)

尝试:

DECLARE @last_timestamp as varchar;
SELECT @last_timestamp  = MAX(modify_timestamp) FROM dbo.table1
PRINT @last_timestamp

DECLARE @last_timestamp as varchar;
SET @last_timestamp  = (SELECT MAX(modify_timestamp) FROM dbo.table1)
PRINT @last_timestamp