Hady.J.Wolf
Hady.J.Wolf

Reputation: 5

Stored procedure Insert After Insert

I'm working on SQL Server 2008 and I have two tables:

Now I want database to insert a row into Citizen whenever I insert a row into Born, which means I want to "insert into Citizen after insert into Born".

But I need it to be parameter so I don't have to do the same thing every time

I've added a stored procedure

Create proc CitizenBorn
As
Begin
    Insert into dbo.Citizen (ID, Name, Income, Address, Card_ID)
        Select 
            Id, Name, '0', Address, Card_ID 
        From 
            dbo.Born
End

But it didn't work, because I tried to insert row into "Born table" but it didn't affected "Citizen table"

Upvotes: 0

Views: 1934

Answers (1)

pmbAustin
pmbAustin

Reputation: 3970

Create a trigger on Born to insert into the Citizen table with each insert into Born:

CREATE TRIGGER Born_Insert 
   ON  dbo.Born 
   AFTER INSERT
AS 
BEGIN
    SET NOCOUNT ON;
    INSERT INTO dbo.Citizen ([ID], [Name], [Income], [Address], [Card_ID])
        SELECT [Id], [Name],'0', [Address], [Card_ID] 
          FROM inserted;
END
GO

Upvotes: 1

Related Questions