SQL Server 2005: T-SQL INSERT INTO and OUTPUT timestamp for variable

Example:

IF OBJECT_ID('T1') IS NOT NULL
DROP TABLE T1;
GO

CREATE TABLE T1 (id int PRIMARY KEY, timestamp);
GO

INSERT INTO T1(id) VALUES (1);
GO

declare @v timestamp;
INSERT INTO T1(id) OUTPUT inserted.timestamp as v VALUES (10);
select @v

How can I get insert.timestamp in @v variable?

+3
source share
3 answers

Here is what I should work:

IF OBJECT_ID('T1') IS NOT NULL 
DROP TABLE T1; 
GO 

CREATE TABLE T1 (id int PRIMARY KEY, timestamp); 
GO 

INSERT INTO T1(id) VALUES (1); 
GO 

declare @v as table ([timestamp] varbinary) --timestamp; 
INSERT INTO T1(id) 
OUTPUT inserted.[timestamp] into @v 
VALUES (10); 

select * from @v 

One thing you need to understand is the timestamp field, which cannot be manually filled. Therefore, you should use a different type in your output table. ANd BTW timestamp is out of date, I would not use it in the new development at all. Use rowversion instead. And the timestamp does not mean that it will be a date for those who think it should be similar to ANSII Standard, IN SQL Server is not a date or a convertible date.

+2
source

- :

declare @v timestamp; 
declare @ID int; 
INSERT INTO T1(id) OUTPUT inserted.timestamp as v VALUES (10); 
select @ID =@@IDENTITY;
select @v=timestamp from T1 where id=@ID;

(edit)... , . , , , , , .

, ? , , .

0

Well, you are inserting a specific value into the column ID, which is the main key, so just read the row after inserting it ...

INSERT INTO T1(id) VALUES (1);
GO

DECLARE @v timestamp

SELECT @v = TIMESTAMP 
FROM dbo.T1 
WHERE ID = 1

SELECT @v
0
source

Source: https://habr.com/ru/post/1770562/


All Articles