| Author |
Topic |
|
programer
Posting Yak Master
221 Posts |
Posted - 2011-12-13 : 10:06:58
|
| Hi,I have two table:tbl_table1 and tbl_table2.Structure tbl_table1:Id, Desc, IsApproved1, Desc1, false2, Desc2, falseStructure tbl_table2Id, table1Id, Winnings1, 1, false2, 1, false3, 1, trueIn the first table in column IsApproved is="false" becauseis the tbl_table2 column winnings is false.How to update first table is in the table tbl_table2 WinningId, table1Id, Winnings1, 1, true2, 1, true3, 1, trueIf i have Id, table1Id, Winnings1, 1, true2, 1, false3, 1, trueis updated first table in the column IsApproved is="false"So, i need with trigger update column IsApproved false or true depending in another table? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-13 : 10:53:41
|
| so table1 IsApproved will be true only if all corresponding records in table2 have Winnings = true?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
programer
Posting Yak Master
221 Posts |
Posted - 2011-12-13 : 11:31:07
|
quote: Originally posted by visakh16 so table1 IsApproved will be true only if all corresponding records in table2 have Winnings = true?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Yes.Help me |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-13 : 11:43:36
|
something likeCREATE TRIGGER TrigUpdateON tbl_table2FOR INSERTASBEGINUPDATE tSET t.IsApproved = CASE WHEN NonWinCnt> 0 THEN 'false' ELSE 'true' ENDFROM tbl_table1 tINNER JOIN (SELECT t2.table1ID, COUNT(CASE WHEN t2.Winnings = 'false' THEN 1 ELSE NULL END) AS NonWinCnt FROM (SELECT DISTINCT table1ID FROM INSERTED) i INNER JOIN table2 t2 ON t2.table1ID = i.table1ID GROUP BY t2.table1ID)iON i.table1ID = t.IDEND ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
programer
Posting Yak Master
221 Posts |
Posted - 2011-12-13 : 12:21:29
|
quote: Originally posted by visakh16 something likeCREATE TRIGGER TrigUpdateON tbl_table2FOR INSERTASBEGINUPDATE tSET t.IsApproved = CASE WHEN NonWinCnt> 0 THEN 'false' ELSE 'true' ENDFROM tbl_table1 tINNER JOIN (SELECT t2.table1ID, COUNT(CASE WHEN t2.Winnings = 'false' THEN 1 ELSE NULL END) AS NonWinCnt FROM (SELECT DISTINCT table1ID FROM INSERTED) i INNER JOIN table2 t2 ON t2.table1ID = i.table1ID GROUP BY t2.table1ID)iON i.table1ID = t.IDEND ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
works thanks!.Is it possible to call with stored procedure as trigger? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-13 : 12:25:17
|
| hmm...trigger itself is a special type of procedure that will be triggered by DML/DDL operationsdidnt understand why you want it to call as a separate procedure?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
programer
Posting Yak Master
221 Posts |
Posted - 2011-12-13 : 14:12:25
|
quote: Originally posted by visakh16 hmm...trigger itself is a special type of procedure that will be triggered by DML/DDL operationsdidnt understand why you want it to call as a separate procedure?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Because someone wrote me that in my case I prefer to use Stored Procedures to reduce burden. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-13 : 23:39:44
|
| then how will you determine when to call this procedure?making it a trigger will ensure it will get executed automatically upon each insert operation------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
programer
Posting Yak Master
221 Posts |
Posted - 2011-12-14 : 04:24:52
|
quote: Originally posted by visakh16 then how will you determine when to call this procedure?making it a trigger will ensure it will get executed automatically upon each insert operation------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
this is true! thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-14 : 04:49:11
|
| wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-12-14 : 09:37:58
|
| I think trigger should allow for UPDATE, and perhaps use EXISTS instead of SUM() for efficiency |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-14 : 10:17:16
|
| which SUM? or do you mean COUNT() needs to be replaced by EXISTS? Ok that makes sense------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-12-14 : 10:39:12
|
| Sorry, yes meant COUNT() and not SUM() |
 |
|
|
|