SqlBeginner Posted July 17, 2013 Share Posted July 17, 2013 I am wanting to write a procedure that will insert new/updated fields into another table Ex: tblStaff and tblUsers Fields in tblUsers needs to be inserted into tblStaff when new records or added or fields are updated. What is the best route to go to accomplish this? Link to comment Share on other sites More sharing options...
justsomeguy Posted July 17, 2013 Share Posted July 17, 2013 Triggers and stored procedures are used for different things. If you want something to happen when records are added or updated then you use a trigger for that. Your trigger can run a stored procedure if you want it to. Link to comment Share on other sites More sharing options...
SqlBeginner Posted July 17, 2013 Author Share Posted July 17, 2013 OK so there is a trigger on the table that add/updates information and I would need to write the stored procedure to have that information updated to the second table? Link to comment Share on other sites More sharing options...
justsomeguy Posted July 17, 2013 Share Posted July 17, 2013 You can write a stored procedure, or just write the code in the trigger. A stored procedure would make sense if you would run it from places other than just the trigger. Link to comment Share on other sites More sharing options...
SqlBeginner Posted July 18, 2013 Author Share Posted July 18, 2013 DELIMITER //CREATE PROCEDURE GetOfficeByCountry(IN countryName VARCHAR(255))BEGINSELECT city, phoneFROM officesWHERE country = countryName;END //DELIMITER ; Will you explain delimiter and the reason for the information in () Link to comment Share on other sites More sharing options...
justsomeguy Posted July 18, 2013 Share Posted July 18, 2013 You should probably read up on stored procedures if you don't understand that. The delimiter keyword changes the end-of-statement delimiter. They change it before defining the stored procedure so that the entire create procedure is seen as a single statement, and then they change it back to the default after defining the procedure. The parameter in parentheses defines a variable called countryname that is an input varchar variable. Link to comment Share on other sites More sharing options...
SqlBeginner Posted July 19, 2013 Author Share Posted July 19, 2013 I have been looking over stored procedures for quite some time now and can't seem to find an example of what I'm wanting to do. I'm wanting to pull certain fields from database A to be inserted into database B and update accordingly. I was first attempting to write one procedure but then was looking at writing a stored procedure in A and then calling the procedure in B and then inserting there... Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now