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
 New to SQL Server Programming
 need a trigger or sproc, not sure which or how

Author  Topic 

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2013-09-26 : 10:50:39
Alright, probably a simple situation. In my database, i have seven tables. When a new record is added to the db, it is added to the "Servers" database. The columns in there are only three "AutoID" and INT, primary key, autonumbered. "Servers", varchar 50, "PersonAdding" Varchar 50, and "DateAdded" Datetime.

If someone adds a new record to this table, i need another table, "MainServerInfo" in the same db, to automatically receive a new record, with the same AutoID from the Servers table to go into the AutoID (the PK) in MainServerInfo, and i need the "Servers" column value for the new entry to be inserted into the Servername column for that row in MainServerInfo.

what's the best way for this to happen and, since i've never written a trigger before, how would the coding look.

thanks

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-09-26 : 11:08:10
Would be like this:
CREATE TRIGGER ATriggerName
ON dbo.ServersTable
AFTER INSERT
AS
INSERT INTO MainServerInfo (AutoId, ServersColumn)
SELECT AutoId,ServersColumn FROM INSERTED;
GO
Don't you also need to do something if the data in the Servers table is updated or deleted?
Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2013-09-26 : 11:21:52
Thanks, and no. Because in this case, the data is never deleted and the data in the servers table is never updated. All the other child tables get the updates and those updates are not dependent on each other.
Go to Top of Page
   

- Advertisement -