Reputation: 1669
How can I insert values into table variable from another table by adding a column that counts original records. e.g.
Value Number Value
---------- ---> ----------------
56 1 56
78 2 78
90 3 90
However, I cannot use an IDENTITY(1,1)
to automatically generate counter values because I want to explicitly insert values into Number
column. May be I should use CTE?
Upvotes: 0
Views: 9019
Reputation: 1642
If you don't use 'ORDER BY' during your query, you may use:
SET @number=0;
SELECT @number:=@number+1 AS number, value FROM your_table;
Upvotes: 0
Reputation: 13486
select row_number() over (order by (select 0)) as number,value from @tablevariable
Upvotes: 4