Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to auto incremented a column of a table variable while inserting values in it

Tags:

sql

sql-server

DECLARE @BusinessUnit AS Table( [id] varchar(10),sn int );
begin 

INSERT INTO @BusinessUnit([id])
SELECT name
FROM test 
WHERE course = 'Mca';
select * from @BusinessUnit;
end

how i assign auto increment value in sn field when the records are inserted in it. I Need the following type of output:

id 'Deepak' sn  1 and id 'Rohan'  sn  2  like that
like image 300
user3424829 Avatar asked Oct 22 '25 22:10

user3424829


1 Answers

Modify your table variable to use IDENTITY property for sn column:

DECLARE @BusinessUnit AS Table( [id] varchar(10), sn int IDENTITY (1,1) );

The rest of your code should stay the same.

like image 100
Szymon Avatar answered Oct 25 '25 12:10

Szymon