user1141584
user1141584

Reputation: 619

Single rowset in variable

I m stuck at this place. I m trying to get a single rowset value in a variable.

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

It is returning the below error

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

Is Cursor the only solution to this problem. Any help would be helpful ?

Upvotes: 0

Views: 92

Answers (3)

Robert
Robert

Reputation: 25763

Try:

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

or

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

Upvotes: 0

Taryn
Taryn

Reputation: 247850

You can place parenthese around the SELECT statement

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

Upvotes: 0

juergen d
juergen d

Reputation: 204884

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

or

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

or

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

Upvotes: 1

Related Questions