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.

 All Forums
 General SQL Server Forums
 Script Library
 How to insert new data in trigger

Author  Topic 

HC@ZZF
Starting Member

5 Posts

Posted - 2007-06-14 : 12:55:08
In oracle, I use the following script in trigger:
INSERT INTO TABLE1 (CITY, STATE) VALUES (:new.city,:new.state);

I am wondering how to insert new data in SQL server. Thanks!

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-06-14 : 13:00:52
there is an "inserted" table where the data is first inserted into before it goes into the actual table. So you can just SELECT from the inserted table.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

sshelper
Posting Yak Master

216 Posts

Posted - 2007-06-14 : 13:01:20
In SQL Server, you read from the inserted system table for the newly inserted records in a table:

INSERT INTO Table1 ( City, State )
SELECT City, State FROM inserted

SQL Server Helper
http://www.sql-server-helper.com
Go to Top of Page

HC@ZZF
Starting Member

5 Posts

Posted - 2007-06-14 : 13:21:07
I got it. Thanks! -Henry
Go to Top of Page

HC@ZZF
Starting Member

5 Posts

Posted - 2007-06-14 : 14:05:21
What if I have delete and insert in trigger.
Can you give me a example how to use IF to do it?
Thanks!
-Henry

quote:
Originally posted by sshelper

In SQL Server, you read from the inserted system table for the newly inserted records in a table:

INSERT INTO Table1 ( City, State )
SELECT City, State FROM inserted

SQL Server Helper
http://www.sql-server-helper.com

Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-06-14 : 14:49:12
No problem. There is a "Deleted" table too.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page
   

- Advertisement -