Author |
Topic |
Shilpa22
Starting Member
37 Posts |
Posted - 2010-08-18 : 02:43:33
|
Hello Frnds,I have 1 table in which a Job updates a STATUS col with(6000,9000) based on the Datasource column for AccountID. As soon as the value is updated in the Table 1 I want to decrement an INT col with 1 value for tht Account.How can I achieve this. Please help me out.THanks in Advance.Thanks in AdvanceShilpa |
|
Shilpa22
Starting Member
37 Posts |
Posted - 2010-08-18 : 03:37:12
|
I am trying with the below 1, but getting some errorCREATE TRIGGER afterupdate_JobQuery ON DWS..DWS_Job_Query for updateif update(query_status)update dws..DWS_AccountStatus set Req_Count = Req_Count - 1 where account_id = inserted.account_id and data_source_id=inserted.Data_Source_ID GoError:Incorrect syntax near the keyword 'if'Please help me out.Thanks in AdvanceShilpa |
 |
|
manub22
Starting Member
6 Posts |
Posted - 2010-08-18 : 03:55:25
|
Modify your code below:CREATE TRIGGER afterupdate_JobQuery ON DWS..DWS_Job_Query for updateasdeclare @account_id intdeclare @Data_Source_id intselect @account_id = account_id from insertedselect @Data_Source_ID = Data_Source_ID from insertedif update(STATUS)update dws..DWS_AccountStatus set Req_Count = Req_Count - 1 where account_id = @account_id and data_source_id = @Data_Source_IDGo |
 |
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-08-18 : 10:13:35
|
quote: Originally posted by manub22 Modify your code below:CREATE TRIGGER afterupdate_JobQuery ON DWS..DWS_Job_Query for updateasdeclare @account_id intdeclare @Data_Source_id intselect @account_id = account_id from insertedselect @Data_Source_ID = Data_Source_ID from insertedif update(STATUS)update dws..DWS_AccountStatus set Req_Count = Req_Count - 1 where account_id = @account_id and data_source_id = @Data_Source_IDGo
The above wont work if multiple records are updated at a time.Use like..SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TRIGGER afterupdate_JobQuery ON DWS..DWS_Job_Query FOR UPDATEASBEGIN IF UPDATE(query_status) BEGIN UPDATE A SET A.Req_Count = A.Req_Count - 1 FROM dws..DWS_AccountStatus A INNER JOIN Inserted B ON A.account_id = B.account_id and A.data_source_id=B.Data_Source_ID ENDEND |
 |
|
X002548
Not Just a Number
15586 Posts |
|
Shilpa22
Starting Member
37 Posts |
Posted - 2010-08-20 : 17:06:21
|
Thanks for your help..I followed your sugestions...but still the records are not getting updated.pls helpThanks in AdvanceShilpa |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-20 : 17:22:06
|
You can't do sequential substraction like that! And yes, your table is updated, not just with the result you want.If you want us to help you, please be more precise. "Not getting updated" means, for us, that no value changed at all.See here in this example how your thinking works in real life.DECLARE @Source TABLE ( x INT, i INT )INSERT @SourceVALUES (1, 10)SELECT *FROM @SourceDECLARE @Control TABLE ( x INT )INSERT @ControlSELECT 1 UNION ALLSELECT 2 UNION ALLSELECT 3 UNION ALLSELECT 1 UNION ALLSELECT 1 UNION ALLSELECT 2 UNION ALLSELECT 1 UNION ALLSELECT 1UPDATE sSET s.i = s.i - 1FROM @Source AS sINNER JOIN @Control AS c ON c.x = s.x-- You would expect 5 right? You will get 9 instead.-- Why? Because you still think RBAR instead of set-based.-- The reason is that the JOIN will produce a match, but the-- substraction is done per statement, not per matching rows in the join.SELECT *FROM @Source This is how you must proceed with your trigger code!CREATE TRIGGER AfterUpdate_JobQueryON DWS..DWS_Job_QueryAFTER UPDATEASSET NOCOUNT ONIF UPDATE(Query_Status) UPDATE accstat SET accstat.Req_Count = accstat.Req_Count - i.cnt FROM dws..DWS_AccountStatus AS accstat INNER JOIN ( SELECT Account_ID, Data_Source_ID, COUNT(*) AS cnt FROM inserted GROUP BY Account_ID, Data_Source_ID ) AS i ON i.Account_ID = accstat.Account_ID AND i.Data_Source_ID = accstat.Data_Source_IDGO N 56°04'39.26"E 12°55'05.63" |
 |
|
Shilpa22
Starting Member
37 Posts |
Posted - 2010-08-21 : 08:55:56
|
Hi,I used the same above trigger, still the values are not changing in the second Table. Not getting any clue..how to resolve this.Please help meThanks in AdvanceShilpa |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-21 : 10:12:24
|
Post all relevant code. You must be missing something in your copy and paste operation. N 56°04'39.26"E 12°55'05.63" |
 |
|
Shilpa22
Starting Member
37 Posts |
Posted - 2010-08-21 : 11:24:08
|
Below is the one which I am trying...alter TRIGGER AfterUpdate_JobQueryON DWS..DWS_Job_QueryAFTER UPDATEASSET NOCOUNT ONIF UPDATE(Query_Status) UPDATE accstat SET accstat.Req_Count = accstat.Req_Count - i.cnt FROM dws..DWS_AccountStatus AS accstat INNER JOIN ( SELECT Account_ID, Data_Source_ID, COUNT(*) AS cnt FROM inserted GROUP BY Account_ID, Data_Source_ID ) AS i ON i.Account_ID = accstat.Account_ID AND i.Data_Source_ID = accstat.Data_Source_IDGOThanks in AdvanceShilpa |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-21 : 13:19:09
|
[code]alter TRIGGER AfterUpdate_JobQueryON DWS..DWS_Job_QueryAFTER UPDATEASSET NOCOUNT ONIF EXISTS(SELECT 1 FROM INSERTED i JOIN DELETED d ON d.Account_ID = i.Account_ID AND i.Data_Source_ID = d.Data_Source_ID AND d.Query_Status <> i.Query_Status AND i.Query_Status IN (9000,6000) )BEGINUPDATE accstatSET accstat.Req_Count = accstat.Req_Count - i.cntFROM dws..DWS_AccountStatus AS accstatINNER JOIN (SELECT Account_ID,Data_Source_ID,COUNT(*) AS cntFROM insertedGROUP BY Account_ID,Data_Source_ID) AS i ON i.Account_ID = accstat.Account_IDAND i.Data_Source_ID = accstat.Data_Source_IDJOIN DELETED d ON d.Account_ID = i.Account_ID AND i.Data_Source_ID = d.Data_Source_ID AND d.Query_Status <> i.Query_Status AND i.Query_Status IN (9000,6000)ENDGO[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Shilpa22
Starting Member
37 Posts |
Posted - 2010-08-23 : 02:43:37
|
Sorry...I tried the above trigger.I am getting the below error msg..Msg 207, Level 16, State 1, Procedure AfterUpdate_JobQuery, Line 30Invalid column name 'Query_Status'.Msg 207, Level 16, State 1, Procedure AfterUpdate_JobQuery, Line 31Invalid column name 'Query_Status'.Msg 207, Level 16, State 1, Procedure AfterUpdate_JobQuery, Line 31..Any thoughts on this?Invalid column name 'Query_Status'.Thanks in AdvanceShilpa |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-23 : 04:47:45
|
i gave suggestion based on your earlier query. do you really have column called query_status in your table?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Shilpa22
Starting Member
37 Posts |
Posted - 2010-08-23 : 05:26:15
|
yes I do have that column(query_status).Thanks in AdvanceShilpa |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-23 : 08:50:28
|
its on which table? DWS..DWS_Job_Query?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|