SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 need a trigger or sproc, not sure which or how
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

WJHamel
Aged Yak Warrior

USA
646 Posts

Posted - 09/26/2013 :  10:50:39  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3724 Posts

Posted - 09/26/2013 :  11:08:10  Show Profile  Reply with Quote
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

USA
646 Posts

Posted - 09/26/2013 :  11:21:52  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000