This is a discussion on "Update two tables using Stored Procedure in SQL" within the Databases section. This forum, and the thread "Update two tables using Stored Procedure in SQL are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Update two tables using Stored Procedure in SQL
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
Update two tables using Stored Procedure in SQL
Is it possible to update two different tables with one stored procedure or would I need a sp and a trigger? Or would I need a stored procedure with a transaction?
I basically need to change similar fields in both tables from 'null' to 'Yes' - any ideas on best practice? Many thanks |
|
|
|
#2
|
||||
|
||||
|
Re: Update two tables using Stored Procedure in SQL
What's the exact situation? Is this a one off or something that will happen on a regular basis?
FACT: You can only update one table at a time (you can use views to give the impression that multiple tables are updated at once but we don't need to go there). Trigger or SP? A trigger is essentially a stored procedure that gets fired in response to an event (insert/update/detele).How do you do it? PROCEDURE:Hope this helps
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
|
#3
|
|||
|
|||
|
Re: Update two tables using Stored Procedure in SQL
Thanks for the response.
Could you guide me as to where to put the 'WHERE' clause? I have tried this:
Would it be easier to have an update sp on the payment table then a trigger on the paymentAdd table - if so can you use the WHERE clause in a trigger? This action is not a one off and will be called many times. My problem started with this query in my asp page - I have had to extend an application and have added an overflow table to catch additional data. Therefore both tables need to be updated at the same time:
|
|
#4
|
||||
|
||||
|
Re: Update two tables using Stored Procedure in SQL
Ok, let me try and explain...
The 'WHERE' clause can be used in any T-SQL statement, whether it's within a trigger, stored procedure, function, etc... I understand you have 2 tables, but what are their key fields? Is there an 'id' or 'payment_id' field?
UPDATE ... WHERE payments.collegetotalpay = 'Yes' AND payments.collegetotalpaid IS NULL AND invoice_id = 9999
When you've figure that out, add another parameter to your procedure like this...
UPDATE payments SET amount = 9.99, date = getdate() WHERE payment_id = 9999 instead of... UPDATE payments SET payments.amount = 9.99, payments.date = getdate() WHERE payments.payment_id = 9999
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Last edited by spinal007; Jul 1st, 2007 at 14:01. |
|
#5
|
|||
|
|||
|
Re: Update two tables using Stored Procedure in SQL
Many thanks for your reply...putting both 'WHERE' clauses worked (strange they didn't work when I was trying my combinations - possibly blind panic and stress mixed in).
As your 'id' question - I am not using one because the call runs through the 'collegetotalpay' column looking for a 'Yes', if there are any then it writes in the 'collegetotalpaid' field 'Yes' (then a date) and the finance run proceeds on all of those records. Hence, I am looking for all records not one specific invoice. Thanks again for your help - onwards now to the asp part and setting values in parameters for the stored procedure to collect. Does this EVER get easier because I honestly feel like changing professions!! |
|
#6
|
||||
|
||||
|
Re: Update two tables using Stored Procedure in SQL
Quote:
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
![]() |
| Tags |
| sql, stored procedure |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| update two tables | padmajyothi | Databases | 7 | Jul 13th, 2007 14:50 |
| stored proc | neoandzuco | Databases | 1 | Feb 14th, 2007 14:39 |
| Stored proc | kebi | Databases | 0 | Oct 28th, 2005 11:17 |
| Help on ASP procedure with VBScript | malambing57 | Classic ASP | 6 | Apr 26th, 2004 17:05 |