Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
programer
Posting Yak Master
221 Posts |
Posted - 2013-09-15 : 07:03:17
|
Hi,In the first table table1 I Inserted 3 rows.Id, values1, test12, test23, test3in the second table2 I want to insert only row 1 and 3I tried:ALTER TRIGGER BetSlipEventsTriggerON dbo.tbl_BetSlipEventsAFTER INSERTASBEGININSERT TOP (1) INTO tbl_BetSlipSystem ( BetSlipEventId )SELECT TOP 1 IDFROM INSERTEDINSERT TOP (3) INTO tbl_BetSlipSystem ( BetSlipEventId )SELECT TOP 3 IDFROM INSERTED ENDNot works. This code inserted 3 rows. |
|
VeeranjaneyuluAnnapureddy
Posting Yak Master
169 Posts |
Posted - 2013-09-16 : 00:38:33
|
INSERT INTO tbl_BetSlipSystem SELECT ID,BetSlipEventIdFROM INSERTEDWHERE ID in (1,3)veeranjaneyulu |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-09-17 : 11:49:32
|
[code]ALTER TRIGGER dbo.BetSlipEventsTriggerON dbo.tbl_BetSlipEventsAFTER INSERT, UPDATE, DELETEASSET NOCOUNT ON;WITH cteSource(ID, rn)AS ( SELECT ID, ROW_NUMBER() OVER (ORDER BY ID) AS rn FROM dbo.tbl_BetSlipEvents)MERGE dbo.tbl_BetSlipSystem AS tgtUSING ( SELECT ID FROM cteSource WHERE rn IN (1, 3) ) AS srcWHEN NOT MATCHED BY TARGET THEN INSERT ( BetSlipEventID ) VALUES ( src.ID )WHEN NOT MATCHED BY SOURCE THEN DELETE;[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
|
|
|
|
|