Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had in mind to evaluate and rollback if not match the table row

create and populate the table

Use Test
go

if OBJECT_ID('ttable','U') is not null
	drop table ttable
go

declare @jj nvarchar(MAX) = N'[
{"text":"10","f_date":"1/11/20"},
{"text":"11","f_date":"1/11/21"},
{"text":"12","f_date":"1/11/22"}]';

select * into ttable from OPENJSON(@jj) with ([text] nvarchar(50),f_date smalldatetime)


and the trigger for each INSERT and UPDATE

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE or alter TRIGGER dbo.TTest 
   ON dbo.ttable
   AFTER INSERT, UPDATE
AS 
BEGIN
	SET NOCOUNT ON;
	declare @txt nvarchar(50);  

	set @txt = (select [text] from inserted);
if @txt != '333'
	begin
		RAISERROR ('%s Not equal 333', 16, 1, @txt) with nowait;
			rollback TRANSACTION;  
	END
end
GO


populate the table with some records

insert into ttable values('2','1/1/12')
insert into ttable values('3','1/1/13')
insert into ttable values('4','1/1/14')
insert into ttable values('5','1/1/15')
insert into ttable values('6','1/1/16')
insert into ttable values('7','1/1/17')
insert into ttable values('8','1/1/18')
insert into ttable values('9','1/1/19')


I supposed the trigger will evaluate each row according to IF and rollback just one current record because there is just one row in current single transaction

but the trigger evaluates just the first record and blocks the handling the rest of rows

Could you please help

What I have tried:

TSQL, MSDN, C#, ROLLBACK TRANSACTION (Transact-SQL)[^]
Posted
Updated 16-May-20 6:09am
Comments
Richard Deeming 17-Sep-19 13:12pm    
NB: Triggers can and will fire with multiple rows in the inserted table. Your trigger needs to handle that case; currently, it's just checking the text from the last inserted/updated row.
[no name] 19-Sep-19 9:35am    
Hi, Richard!
The RAISERROR throws an exception to the Try\Catch.
I was curious - how to handle the inserts one by one using the trigger evaluation
and to reject not matched rows
Richard Deeming 19-Sep-19 9:57am    
You can't. Either the transaction succeeds, or the transaction fails. You can't have some rows inserted and others rejected within a single transaction.

If you want to insert the rows that match, then you'll need to insert each row in its own batch. In SQL Server Management Studio, put GO between each INSERT line:
insert into ttable values (...)
GO
insert into ttable values (...)
GO
...
[no name] 19-Sep-19 10:10am    
yeah!
I've read the MSDN regarding this theme.
Otherwise the ROLLBACK cancels the rest of batch

1 solution

You missed the part about "begin transactions".

BEGIN TRANSACTION (Transact-SQL) - SQL Server | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900