Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 how to find row with trigger
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

programer
Posting Yak Master

Slovenia
221 Posts

Posted - 09/14/2013 :  14:40:15  Show Profile  Reply with Quote
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

Slovenia
221 Posts

Posted - 09/14/2013 :  15:27:13  Show Profile  Reply with Quote
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

Slovenia
221 Posts

Posted - 09/14/2013 :  17:36:09  Show Profile  Reply with Quote
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)

Singapore
17689 Posts

Posted - 09/15/2013 :  23:55:43  Show Profile  Reply with Quote
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
Time is always against us

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

Posted - 09/17/2013 :  11:49:41  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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;



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000