Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi All,I have written sp like this, everytime the insert operation happens , the eid column should be automatically incrementCREATE PROCEDURE Ass_insertable_new7(@a int OUTPUT,@ename nchar(40) OUTPUT,@sal int OUTPUT,@dept int OUTPUT)ASbeginINSERT table11(eid, ename, sal, dept)VALUES (@a, @ename, @sal, @dept) endGOdeclare @a intdeclare @ename nchar(40)declare @sal intdeclare @dept intset @a=1set @a=@a+1set @ename='basava'set @sal=1500set @dept=50Execute Ass_insertable_new7 @a=@a OUTPUT,@ename= @ename OUTPUT, @sal= @sal OUTPUT, @dept= @dept OUTPUTbut in this the value is 2 for all the records...i want it to be incremented how to proceed.Thanks in advance
khtan
In (Som, Ni, Yak)
17689 Posts
Posted - 2011-10-14 : 03:30:13
you will need read the existing eid from table and increment it inside the stored procedure. Not oustside
CREATE PROCEDURE Ass_insertable_new7(@a int OUTPUT,@ename nchar(40) OUTPUT,@sal int OUTPUT,@dept int OUTPUT)ASbeginselect @a = max(eid) from table11select @a = isnull(@a, 0) + 1INSERT table11 (eid, ename, sal, dept) VALUES (@a, @ename, @sal, @dept)end