Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CREATE TABLE [dbo].[Employee1](
[id] CHAR(10) PRIMARY KEY,
[name] VARCHAR(50),
[address] varchar(50)
)
GO
CREATE TRIGGER AutoIncrement_Trigger ON [dbo].[Employee1]
instead OF INSERT AS
BEGIN

DECLARE @num INT
SELECT @num=ISNULL(MAX(id),0)+1 FROM [dbo].[Employee1]

INSERT INTO [dbo].[Employee1] (id,name,address) SELECT (@num),
name=i.name,
address =i.address
from inserted i
END


SQL
INSERT INTO [dbo].[Employee1] (name,address) VALUES('a','aaa')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('Aslam','pakistan')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('ali','hindustan')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('shah','iran')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('azeem','nipal')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('asad','sirlandka')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('amir','karachi')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('arif','lahore')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('Raja','islamabad')
Posted
Comments
King Fisher 7-May-14 3:18am    
solved ?

1 solution

Instead of doing this you can use simple way to create table.

SQL
CREATE TABLE [dbo].[Employee1](
[id] int  PRIMARY KEY identity(1,1),
[name] VARCHAR(50),
[address] varchar(50)
)
GO


and you can insert data like this..

Need not to create trigger..

your ID colomn will be incremented by 1 automatically

SQL
INSERT INTO [dbo].[Employee1] (name,address) VALUES('a','aaa')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('Aslam','pakistan')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('ali','hindustan')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('shah','iran')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('azeem','nipal')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('asad','sirlandka')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('amir','karachi')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('arif','lahore')
INSERT INTO [dbo].[Employee1] (name,address) VALUES('Raja','islamabad')
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900