T-SQL Sample: Insert to Self and Insert to Another Table from a Trigger


–1. Insert into self
INSERT INTO dbo.Table_1 ([num])
select top (20)[num] from dbo.Table_1
select * from dbo.Table_1

–2.Use a trigger to insert table2
CREATE TRIGGER UpdatetbB ON [dbo].[Table_1]
FOR INSERT
AS

BEGIN
— SET NOCOUNT ON added to prevent extra result sets from
— interfering with SELECT statements.
SET NOCOUNT ON;

INSERT INTO [Table_2]([num])
SELECT num FROM inserted

END
GO

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s