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-17 : 06:38:22
|
I tried to use SQL this works:INSERT INTO tbl_BetSlipEvents (Event)VALUES('event1');INSERT INTO tbl_BetSlipEvents (Event)VALUES('event2');INSERT INTO tbl_BetSlipEvents (Event)VALUES('event3');INSERT INTO tbl_BetSlipEvents (Event)VALUES('event4');INSERT INTO tbl_BetSlipEvents (Event)VALUES('event5');INSERT INTO tbl_BetSlipSystem (BetSlipEventId)Select id from(Select id,row_number() over(order by id) as Number from tbl_BetSlipEvents) as awhere a.Number=1 or a.Number=3But If I used trigger not works (inserted 5 rows):ALTER TRIGGER BetSlipEventsTriggerON dbo.tbl_BetSlipEventsAFTER INSERTASInsert into tbl_BetSlipSystem(BetSlipEventId)Select Id from(Select row_number() over(order by id) as Number, Id from inserted) as aWhere a.Number=1Where is the problem? |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-09-17 : 08:32:48
|
quote: Originally posted by programer I tried to use SQL this works:INSERT INTO tbl_BetSlipEvents (Event)VALUES('event1');INSERT INTO tbl_BetSlipEvents (Event)VALUES('event2');INSERT INTO tbl_BetSlipEvents (Event)VALUES('event3');INSERT INTO tbl_BetSlipEvents (Event)VALUES('event4');INSERT INTO tbl_BetSlipEvents (Event)VALUES('event5');INSERT INTO tbl_BetSlipSystem (BetSlipEventId)Select id from(Select id,row_number() over(order by id) as Number from tbl_BetSlipEvents) as awhere a.Number=1 or a.Number=3But If I used trigger not works (inserted 5 rows):ALTER TRIGGER BetSlipEventsTriggerON dbo.tbl_BetSlipEventsAFTER INSERTASInsert into tbl_BetSlipSystem(BetSlipEventId)Select Id from(Select row_number() over(order by id) as Number, Id from inserted) as aWhere a.Number=1Where is the problem?
Each of your five inserts causes the trigger to be invoked, and with each invoke it inserts a row into the tbl_BetSlipSystem table.What do you want it to do? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-09-17 : 11:48:59
|
[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 src ON src.ID = tgt.BetSlipEventIDWHEN 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 |
|
|
programer
Posting Yak Master
221 Posts |
Posted - 2013-09-17 : 13:20:20
|
quote: Originally posted by SwePeso
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; Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Thanks for your help.I got:Incorrect syntax the keyword WHEN ? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-09-17 : 13:41:22
|
Sorry, my bad.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 src ON src.ID = tgt.BetSlipEventIDWHEN 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 |
|
|
programer
Posting Yak Master
221 Posts |
Posted - 2013-09-17 : 13:42:50
|
quote: Originally posted by SwePeso Sorry, my bad.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 src [red]ON src.ID = tgt.BetSlipEventID 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
Incorrect syntax near 'red' ? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-09-17 : 13:45:16
|
See edited response 09/17/2013 : 13:41:22 Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
programer
Posting Yak Master
221 Posts |
Posted - 2013-09-17 : 14:06:32
|
quote: Originally posted by SwePeso See edited response 09/17/2013 : 13:41:22 Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
I checked.The problem is because If I have:tbl_BetSlipEventsId, Event27, event129, event2and then inserted new values:31, event433, event434, event5Is my tbl_BetSlipEvents:27, event129, event231, event333, event434, event5 SELECT ID FROM cteSource WHERE rn IN (1, 3)WHERE is 1,3. This is ok. But inseted in the second table 27,. I need to insert 31,33 because my new values is inserted: 31, event333, event434, event5Please help. |
|
|
programer
Posting Yak Master
221 Posts |
Posted - 2013-09-17 : 14:39:14
|
This I mean:tbl_BetSlipEvents currently:Id, Event27, event129, event2and then I inserted new values:Id, event31, event333, event434, event5tbl_BetSlipEvents is now:Id, event27, event129, event231, event333, event434, event5In a second table the tbl_BetSlipSystem I want to insert:tbl_BetSlipEvents:Id, BetSlipEventId52, 3153, 34Pls help[/quote] |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-09-17 : 14:40:49
|
Only if you insert them at ONCE! In ONE batch. Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
programer
Posting Yak Master
221 Posts |
Posted - 2013-09-17 : 14:43:48
|
quote: Originally posted by SwePeso Only if you insert them at ONCE! In ONE batch. Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Yes. How to inserted at once?I tried to use inserted but inserted three record: SELECT ID, ROW_NUMBER() OVER (ORDER BY ID) AS rn FROM inserted |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-09-17 : 14:51:33
|
INSERT tbl_BetSlipEvents (Event)VALUES ('event1'), ('event2'), 'event3'), ('event4'), ('event5'); Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
programer
Posting Yak Master
221 Posts |
Posted - 2013-09-17 : 18:41:34
|
Yes I tried and not works.CREATE TABLE [dbo].[tbl_BetSlipEvents]( [ID] [int] IDENTITY(1,1) NOT NULL, [Event] [nvarchar](50) NULL, CONSTRAINT [PK_tbl_BetSlipEvents] PRIMARY KEY CLUSTERED (CREATE TABLE [dbo].[tbl_BetSlipSystem]( [ID] [int] IDENTITY(1,1) NOT NULL, [BetSlipEventId] [int] NULL, CONSTRAINT [PK_tbl_BetSlipSystem] PRIMARY KEY CLUSTERED Trigger:USE [test_events]GO/****** Object: Trigger [dbo].[BetSlipEventsTrigger] Script Date: 09/18/2013 00:36:10 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER TRIGGER [dbo].[BetSlipEventsTrigger]ON [dbo].[tbl_BetSlipEvents]AFTER 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 src ON src.ID = tgt.BetSlipEventIDWHEN NOT MATCHED BY TARGET THEN INSERT ( BetSlipEventID ) VALUES ( src.ID )WHEN NOT MATCHED BY SOURCE THEN DELETE;Insert:INSERT tbl_BetSlipEvents (Event)VALUES ('event1'), ('event2'), ('event3'), ('event4'), ('event5');I got:tbl_BetSlipEvents:ID Event128 event1129 event2130 event3131 event4132 event5133 event1134 event2135 event3136 event4137 event5tbl_BetSlipSystem:64 12865 130I need to get:64 13365 135This is because I inserted 5 new values and my row is 1 and 3.Check and you will see where is my problem.quote: Originally posted by SwePeso INSERT tbl_BetSlipEvents (Event)VALUES ('event1'), ('event2'), 'event3'), ('event4'), ('event5'); Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
|
|
|
programer
Posting Yak Master
221 Posts |
Posted - 2013-09-17 : 19:12:05
|
Works now. Inserted was missingquote: Originally posted by programer Yes I tried and not works.CREATE TABLE [dbo].[tbl_BetSlipEvents]( [ID] [int] IDENTITY(1,1) NOT NULL, [Event] [nvarchar](50) NULL, CONSTRAINT [PK_tbl_BetSlipEvents] PRIMARY KEY CLUSTERED (CREATE TABLE [dbo].[tbl_BetSlipSystem]( [ID] [int] IDENTITY(1,1) NOT NULL, [BetSlipEventId] [int] NULL, CONSTRAINT [PK_tbl_BetSlipSystem] PRIMARY KEY CLUSTERED Trigger:USE [test_events]GO/****** Object: Trigger [dbo].[BetSlipEventsTrigger] Script Date: 09/18/2013 00:36:10 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER TRIGGER [dbo].[BetSlipEventsTrigger]ON [dbo].[tbl_BetSlipEvents]AFTER 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 src ON src.ID = tgt.BetSlipEventIDWHEN NOT MATCHED BY TARGET THEN INSERT ( BetSlipEventID ) VALUES ( src.ID )WHEN NOT MATCHED BY SOURCE THEN DELETE;Insert:INSERT tbl_BetSlipEvents (Event)VALUES ('event1'), ('event2'), ('event3'), ('event4'), ('event5');I got:tbl_BetSlipEvents:ID Event128 event1129 event2130 event3131 event4132 event5133 event1134 event2135 event3136 event4137 event5tbl_BetSlipSystem:64 12865 130I need to get:64 13365 135This is because I inserted 5 new values and my row is 1 and 3.Check and you will see where is my problem.quote: Originally posted by SwePeso INSERT tbl_BetSlipEvents (Event)VALUES ('event1'), ('event2'), 'event3'), ('event4'), ('event5'); Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
|
|
|
programer
Posting Yak Master
221 Posts |
Posted - 2013-09-18 : 14:52:34
|
Hi again,your code works perfectly.I want to add insert and different where=1,3 but not works.Trigger:WITH cteSource(ID,BetSlipDetailId, rn)AS ( SELECT ID,BetSlipDetailId, ROW_NUMBER() OVER (ORDER BY ID) AS rn FROM inserted)MERGE dbo.tbl_BetSlipSystem AS tgtUSING ( SELECT ID, BetSlipDetailId FROM cteSource WHERE rn IN (1, 2, 3) ) AS src ON src.ID = tgt.BetSlipEventIDWHEN NOT MATCHED BY TARGET THEN INSERT (BetSlipDetailID, BetSlipEventID, SystemBet ) VALUES ( src.BetSlipDetailId, src.ID, '1,3' );WITH cteSource2(ID,BetSlipDetailId, rn)AS ( SELECT ID,BetSlipDetailId, ROW_NUMBER() OVER (ORDER BY ID) AS rn FROM inserted)MERGE dbo.tbl_BetSlipSystem AS tgt2USING ( SELECT ID, BetSlipDetailId FROM cteSource2 WHERE rn IN (1, 3) ) AS src2 ON src2.ID = tgt2.BetSlipEventIDWHEN NOT MATCHED BY TARGET THEN INSERT (BetSlipDetailID, BetSlipEventID, SystemBet ) VALUES ( src2.BetSlipDetailId, src2.ID, '2,3' );quote: Originally posted by programer Works now. Inserted was missingquote: Originally posted by programer Yes I tried and not works.CREATE TABLE [dbo].[tbl_BetSlipEvents]( [ID] [int] IDENTITY(1,1) NOT NULL, [Event] [nvarchar](50) NULL, CONSTRAINT [PK_tbl_BetSlipEvents] PRIMARY KEY CLUSTERED (CREATE TABLE [dbo].[tbl_BetSlipSystem]( [ID] [int] IDENTITY(1,1) NOT NULL, [BetSlipEventId] [int] NULL, CONSTRAINT [PK_tbl_BetSlipSystem] PRIMARY KEY CLUSTERED Trigger:USE [test_events]GO/****** Object: Trigger [dbo].[BetSlipEventsTrigger] Script Date: 09/18/2013 00:36:10 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER TRIGGER [dbo].[BetSlipEventsTrigger]ON [dbo].[tbl_BetSlipEvents]AFTER 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 src ON src.ID = tgt.BetSlipEventIDWHEN NOT MATCHED BY TARGET THEN INSERT ( BetSlipEventID ) VALUES ( src.ID )WHEN NOT MATCHED BY SOURCE THEN DELETE;Insert:INSERT tbl_BetSlipEvents (Event)VALUES ('event1'), ('event2'), ('event3'), ('event4'), ('event5');I got:tbl_BetSlipEvents:ID Event128 event1129 event2130 event3131 event4132 event5133 event1134 event2135 event3136 event4137 event5tbl_BetSlipSystem:64 12865 130I need to get:64 13365 135This is because I inserted 5 new values and my row is 1 and 3.Check and you will see where is my problem.quote: Originally posted by SwePeso INSERT tbl_BetSlipEvents (Event)VALUES ('event1'), ('event2'), 'event3'), ('event4'), ('event5'); Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
|
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-09-18 : 15:08:59
|
quote: Originally posted by programer Hi again,your code works perfectly.I want to add insert and different where=1,3 but not works.
How does it not work? Do you get an error, results you did not expect or other?Can you post your sample data and expected output?http://www.sqlservercentral.com/articles/Best+Practices/61537/http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
|
|
programer
Posting Yak Master
221 Posts |
Posted - 2013-09-18 : 15:52:49
|
Not get any error.Insert only:WITH cteSource(ID,BetSlipDetailId, rn)AS ( SELECT ID,BetSlipDetailId, ROW_NUMBER() OVER (ORDER BY ID) AS rn FROM inserted)MERGE dbo.tbl_BetSlipSystem AS tgtUSING ( SELECT ID, BetSlipDetailId FROM cteSource WHERE rn IN (1, 2, 3) ) AS src ON src.ID = tgt.BetSlipEventIDWHEN NOT MATCHED BY TARGET THEN INSERT (BetSlipDetailID, BetSlipEventID, SystemBet ) VALUES ( src.BetSlipDetailId, src.ID, '1,3' );Code: WITH cteSource2(ID,BetSlipDetailId, rn)Like it does not exist.quote: Originally posted by Lamprey
quote: Originally posted by programer Hi again,your code works perfectly.I want to add insert and different where=1,3 but not works.
How does it not work? Do you get an error, results you did not expect or other?Can you post your sample data and expected output?http://www.sqlservercentral.com/articles/Best+Practices/61537/http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
|
|
|
programer
Posting Yak Master
221 Posts |
Posted - 2013-09-19 : 06:07:35
|
Still I need your help.quote: Originally posted by programer Not get any error.Insert only:WITH cteSource(ID,BetSlipDetailId, rn)AS ( SELECT ID,BetSlipDetailId, ROW_NUMBER() OVER (ORDER BY ID) AS rn FROM inserted)MERGE dbo.tbl_BetSlipSystem AS tgtUSING ( SELECT ID, BetSlipDetailId FROM cteSource WHERE rn IN (1, 2, 3) ) AS src ON src.ID = tgt.BetSlipEventIDWHEN NOT MATCHED BY TARGET THEN INSERT (BetSlipDetailID, BetSlipEventID, SystemBet ) VALUES ( src.BetSlipDetailId, src.ID, '1,3' );Code: WITH cteSource2(ID,BetSlipDetailId, rn)Like it does not exist.quote: Originally posted by Lamprey
quote: Originally posted by programer Hi again,your code works perfectly.I want to add insert and different where=1,3 but not works.
How does it not work? Do you get an error, results you did not expect or other?Can you post your sample data and expected output?http://www.sqlservercentral.com/articles/Best+Practices/61537/http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
|
|
|
|
|
|
|
|