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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 how to find row with trigger

Author  Topic 

programer
Posting Yak Master

221 Posts

Posted - 2013-09-14 : 14:40:15
Hi,

I have table
tbl_BetSlipEvents
Id, event
202, event1
203, event2
204, event

and
tbl_BetSlipSystem


In the table tbl_BetSlipEvents inserted regarding on row in the tbl_BetSlipEvents table
Id, BetSlipEventId
73, 202
74, 204

I need to find row 1 and row 3 in the first table and to insert the second table.



programer
Posting Yak Master

221 Posts

Posted - 2013-09-14 : 15:27:13
quote:
Originally posted by programer

Hi,

I have table
tbl_BetSlipEvents
Id, event
202, event1
203, event2
204, event

and
tbl_BetSlipSystem


In the table tbl_BetSlipEvents inserted regarding on row in the tbl_BetSlipEvents table
Id, BetSlipEventId
73, 202
74, 204

I need to find row 1 and row 3 in the first table and to insert the second table.







My trigger:
ALTER TRIGGER BetSlipEventsTrigger
ON dbo.tbl_BetSlipEvents
AFTER INSERT

AS

BEGIN
INSERT INTO tbl_BetSlipSystem(BetSlipEventId)
SELECT Id FROM INSERTED WHERE id =352 ;
END

where id=352 works, but I need 1 row and 3 row which was inserted in the table.

Please help
Go to Top of Page

programer
Posting Yak Master

221 Posts

Posted - 2013-09-14 : 17:36:09
Still I need your help how to select row 1 and 3 and then inserted on other table

quote:
Originally posted by programer

quote:
Originally posted by programer

Hi,

I have table
tbl_BetSlipEvents
Id, event
202, event1
203, event2
204, event

and
tbl_BetSlipSystem


In the table tbl_BetSlipEvents inserted regarding on row in the tbl_BetSlipEvents table
Id, BetSlipEventId
73, 202
74, 204

I need to find row 1 and row 3 in the first table and to insert the second table.







My trigger:
ALTER TRIGGER BetSlipEventsTrigger
ON dbo.tbl_BetSlipEvents
AFTER INSERT

AS

BEGIN
INSERT INTO tbl_BetSlipSystem(BetSlipEventId)
SELECT Id FROM INSERTED WHERE id =352 ;
END

where id=352 works, but I need 1 row and 3 row which was inserted in the table.

Please help


Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-09-15 : 23:55:43
not really sure what are you trying to do but, to get the 1st and 3rd row, you can use row_number()

select *
from
(
select *, rn = row_number() over ( order by somecol )
from sometable
) d
where rn = 1
or rn = 3



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-09-17 : 11:49:41
[code]ALTER TRIGGER dbo.BetSlipEventsTrigger
ON dbo.tbl_BetSlipEvents
AFTER INSERT,
UPDATE,
DELETE
AS

SET 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 tgt
USING (
SELECT ID
FROM cteSource
WHERE rn IN (1, 3)
) AS src
WHEN 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
Go to Top of Page
   

- Advertisement -