2012-10-05 37 views
0

我被困在這個地方。我試圖獲得一個單一的行集值在一個變量。單排變量

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'. 

是光標對這個問題的唯一解決方案。任何幫助將有所幫助?

回答

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 
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) 
+0

很透徹,但我從來沒有使用:=運算符。給谷歌! –

0

您可以將parenthese圍繞SELECT聲明

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