Showing posts with label trigger. Show all posts
Showing posts with label trigger. Show all posts

Monday, March 26, 2012

Error inside trigger

hi

there is a programing flow that I want to know exactly what will happen

1- A database which has 3 table T1,T2,T3

2-a Storeprocedure called STP

3- Inside stp we insert to T1 and T2

4-T2 has 2 triggers TRG1,TRG2

5 - TRG2 check some thing and raise error .

so, I want to know at this case all rows which inserted will rollback or no

You are inserting in more than one table, so if you want to rollback the whole transaction inside the trigger, you have to initiate an explicit transaction using "begin transaction" (before start inserting), if not, just the insert on T2 will be rolled back.

AMB

|||

TRG2 will ROLLBACK the INSERT into T2 ONLY.

As Alejandro indicated, if you wish to ROLLBACK ALL activites, you need to start a TRANSACTION inside STP.

|||

I did think like u said befor but !!! you try this :

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table_1](
[Cola] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Colb] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]

go

exec sys.sp_addmessage @.msgnum= 50001, @.severity= 16, @.msgtext= N'User error', @.replace=N'replace'

go

SETANSI_NULLSON

GO

SETQUOTED_IDENTIFIERON

GO

createTRIGGER dbo.trg1

ON dbo.table_1

AFTER INSERT,UPDATE

AS

BEGIN

SETNOCOUNTON;

raiserror(50001,16,1)

END

GO

they make u a table (Table_1) and a trigger (Trg1)

as u can see in Trg1 an error will raise so if we insert any row it will be rollbacked. so try to insert :

INSERTINTO [dbo].[Table_1]

([Cola]

,[Colb])

VALUES

('aaa'

,'bbb')

then u can see a row added !!!

sql

Wednesday, March 7, 2012

Error in inserting value by trigger in linked server

Hi Pls help me
I have 2 db servers and its a lnked server
while am inserting value in source table (table having a trigger to raise ti
other table in defferent server) i am facing this error and Data is not
getting inserted.
error is
- Unable to preserve trigger 'InsertIntoHPD_HelpDesk'.
ODBC error: [Microsoft][ODBC SQL Server Driver][SQL Server]The operation
could not be performed because the OLE DB provider 'SQLOLEDB' was unable to
begin a distributed transaction.
[Microsoft][ODBC SQL Server Driver][SQL Server][OLE/DB provider returned
message: New transaction cannot enlist in the specified transaction
coordinator. ]
regards
KRP
Do you have the Distributed Transaction Coordinator running? On SQL Server
2000 it is one of the services in the SQL Server suite along with SQL Server
and SQL Agent.
RLF
"KRP" <KRP @.discussions.microsoft.com> wrote in message
news:8C37B85A-951E-414C-A6BA-9D7BBFC039D0@.microsoft.com...
> Hi Pls help me
> I have 2 db servers and its a lnked server
> while am inserting value in source table (table having a trigger to raise
> ti
> other table in defferent server) i am facing this error and Data is not
> getting inserted.
> error is
> - Unable to preserve trigger 'InsertIntoHPD_HelpDesk'.
> ODBC error: [Microsoft][ODBC SQL Server Driver][SQL Server]The operation
> could not be performed because the OLE DB provider 'SQLOLEDB' was unable
> to
> begin a distributed transaction.
> [Microsoft][ODBC SQL Server Driver][SQL Server][OLE/DB provider returned
> message: New transaction cannot enlist in the specified transaction
> coordinator. ]
> regards
> KRP
|||Yes,
Its already started and every pre conf hasbeen done..
Please Can you send me a steps of creating a linked server?
Thanks
KRp
"Russell Fields" wrote:

> Do you have the Distributed Transaction Coordinator running? On SQL Server
> 2000 it is one of the services in the SQL Server suite along with SQL Server
> and SQL Agent.
> RLF
> "KRP" <KRP @.discussions.microsoft.com> wrote in message
> news:8C37B85A-951E-414C-A6BA-9D7BBFC039D0@.microsoft.com...
>
>

Error in inserting value by trigger in linked server

Hi Pls help me
I have 2 db servers and its a lnked server
while am inserting value in source table (table having a trigger to raise ti
other table in defferent server) i am facing this error and Data is not
getting inserted.
error is
- Unable to preserve trigger 'InsertIntoHPD_HelpDesk'.
ODBC error: [Microsoft][ODBC SQL Server Driver][SQL Server]The o
peration
could not be performed because the OLE DB provider 'SQLOLEDB' was unable to
begin a distributed transaction.
[Microsoft][ODBC SQL Server Driver][SQL Server][OLE/DB provi
der returned
message: New transaction cannot enlist in the specified transaction
coordinator. ]
regards
KRPDo you have the Distributed Transaction Coordinator running? On SQL Server
2000 it is one of the services in the SQL Server suite along with SQL Server
and SQL Agent.
RLF
"KRP" <KRP @.discussions.microsoft.com> wrote in message
news:8C37B85A-951E-414C-A6BA-9D7BBFC039D0@.microsoft.com...
> Hi Pls help me
> I have 2 db servers and its a lnked server
> while am inserting value in source table (table having a trigger to raise
> ti
> other table in defferent server) i am facing this error and Data is not
> getting inserted.
> error is
> - Unable to preserve trigger 'InsertIntoHPD_HelpDesk'.
> ODBC error: [Microsoft][ODBC SQL Server Driver][SQL Server]The
operation
> could not be performed because the OLE DB provider 'SQLOLEDB' was unable
> to
> begin a distributed transaction.
> [Microsoft][ODBC SQL Server Driver][SQL Server][OLE/DB pro
vider returned
> message: New transaction cannot enlist in the specified transaction
> coordinator. ]
> regards
> KRP|||Yes,
Its already started and every pre conf hasbeen done..
Please Can you send me a steps of creating a linked server?
Thanks
KRp
"Russell Fields" wrote:

> Do you have the Distributed Transaction Coordinator running? On SQL Serve
r
> 2000 it is one of the services in the SQL Server suite along with SQL Serv
er
> and SQL Agent.
> RLF
> "KRP" <KRP @.discussions.microsoft.com> wrote in message
> news:8C37B85A-951E-414C-A6BA-9D7BBFC039D0@.microsoft.com...
>
>

Error in inserting value by trigger in linked server

Hi Pls help me
I have 2 db servers and its a lnked server
while am inserting value in source table (table having a trigger to raise ti
other table in defferent server) i am facing this error and Data is not
getting inserted.
error is
- Unable to preserve trigger 'InsertIntoHPD_HelpDesk'.
ODBC error: [Microsoft][ODBC SQL Server Driver][SQL Server]The operation
could not be performed because the OLE DB provider 'SQLOLEDB' was unable to
begin a distributed transaction.
[Microsoft][ODBC SQL Server Driver][SQL Server][OLE/DB provider returned
message: New transaction cannot enlist in the specified transaction
coordinator. ]
regards
KRPDo you have the Distributed Transaction Coordinator running? On SQL Server
2000 it is one of the services in the SQL Server suite along with SQL Server
and SQL Agent.
RLF
"KRP" <KRP @.discussions.microsoft.com> wrote in message
news:8C37B85A-951E-414C-A6BA-9D7BBFC039D0@.microsoft.com...
> Hi Pls help me
> I have 2 db servers and its a lnked server
> while am inserting value in source table (table having a trigger to raise
> ti
> other table in defferent server) i am facing this error and Data is not
> getting inserted.
> error is
> - Unable to preserve trigger 'InsertIntoHPD_HelpDesk'.
> ODBC error: [Microsoft][ODBC SQL Server Driver][SQL Server]The operation
> could not be performed because the OLE DB provider 'SQLOLEDB' was unable
> to
> begin a distributed transaction.
> [Microsoft][ODBC SQL Server Driver][SQL Server][OLE/DB provider returned
> message: New transaction cannot enlist in the specified transaction
> coordinator. ]
> regards
> KRP|||Yes,
Its already started and every pre conf hasbeen done..
Please Can you send me a steps of creating a linked server?
Thanks
KRp
"Russell Fields" wrote:
> Do you have the Distributed Transaction Coordinator running? On SQL Server
> 2000 it is one of the services in the SQL Server suite along with SQL Server
> and SQL Agent.
> RLF
> "KRP" <KRP @.discussions.microsoft.com> wrote in message
> news:8C37B85A-951E-414C-A6BA-9D7BBFC039D0@.microsoft.com...
> > Hi Pls help me
> >
> > I have 2 db servers and its a lnked server
> > while am inserting value in source table (table having a trigger to raise
> > ti
> > other table in defferent server) i am facing this error and Data is not
> > getting inserted.
> > error is
> > - Unable to preserve trigger 'InsertIntoHPD_HelpDesk'.
> > ODBC error: [Microsoft][ODBC SQL Server Driver][SQL Server]The operation
> > could not be performed because the OLE DB provider 'SQLOLEDB' was unable
> > to
> > begin a distributed transaction.
> > [Microsoft][ODBC SQL Server Driver][SQL Server][OLE/DB provider returned
> > message: New transaction cannot enlist in the specified transaction
> > coordinator. ]
> >
> > regards
> > KRP
>
>

Sunday, February 26, 2012

Error in DELETE trigger

Hi,
I get an error when I try to delete multiple rows from a table.
Server: Msg 512, Level 16, State 1, Procedure tr_Documents_Delete, Line 16
Subquery returned more than 1 value. This is not permitted when the subquery
follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
I guess it is because of my trigger. The trigger looks like this:
ALTER TRIGGER tr_Documents_Delete
ON DocumentTrans
AFTER DELETE
AS
if @.@.rowcount = 0
return
DECLARE @.count INT
SET @.count = (SELECT COUNT(*) FROM DocumentTrans INNER JOIN Deleted
ON DocumentTrans.TableReference = Deleted.TableReference
AND DocumentTrans.RowNumberReference = Deleted.RowNumberReference)
IF @.count > 0
return
DECLARE @.TableName VARCHAR(100)
SET @.TableName = (SELECT TableReference FROM Deleted)
DECLARE @.SQLString VARCHAR(1000)
SET @.SQLString = 'UPDATE ' + @.TableName +
' SET DocumentsExists = NULL WHERE ' +
@.TableName + '.RowNumber = ' + CAST((SELECT RowNumberReference FROM
Deleted) AS VARCHAR(100))
EXEC(@.SQLString)
I have tried with a loop. But it also gives me an error
WHILE (SELECT TableReference FROM Deleted) IS NOT NULL
BEGIN
DECLARE @.TableName VARCHAR(100)
SET @.TableName = (Select TableReference FROM Deleted)
DECLARE @.SQLString VARCHAR(1000)
SET @.SQLString = 'UPDATE ' + @.TableName +
' SET DocumentsExists = NULL WHERE ' +
@.TableName + '.RowNumber = ' + CAST((SELECT RowNumberReference FROM
Deleted) AS VARCHAR(100))
EXEC(@.SQLString)
END
Any ideas?
Best regards
HenrikHi
The offending line could be:
SET @.TableName = (SELECT TableReference FROM Deleted)
A trigger fires once per batch, and not once per row deleted. The assumption
that only 1 row is being deleted fails on that line. The select is returning
more than one row, but the variable can not hold more than one row's value.
Either look at the way you process the row, or disallow more than one row to
be deleted at the same time with another check earlier on the in trigger.
Regards
Mike
"Henrik Skak Pedersen" wrote:

> Hi,
> I get an error when I try to delete multiple rows from a table.
> Server: Msg 512, Level 16, State 1, Procedure tr_Documents_Delete, Line 16
> Subquery returned more than 1 value. This is not permitted when the subque
ry
> follows =, !=, <, <= , >, >= or when the subquery is used as an expression
.
> The statement has been terminated.
> I guess it is because of my trigger. The trigger looks like this:
> ALTER TRIGGER tr_Documents_Delete
> ON DocumentTrans
> AFTER DELETE
> AS
> if @.@.rowcount = 0
> return
> DECLARE @.count INT
> SET @.count = (SELECT COUNT(*) FROM DocumentTrans INNER JOIN Deleted
> ON DocumentTrans.TableReference = Deleted.TableReference
> AND DocumentTrans.RowNumberReference = Deleted.RowNumberReference)
> IF @.count > 0
> return
> DECLARE @.TableName VARCHAR(100)
> SET @.TableName = (SELECT TableReference FROM Deleted)
> DECLARE @.SQLString VARCHAR(1000)
> SET @.SQLString = 'UPDATE ' + @.TableName +
> ' SET DocumentsExists = NULL WHERE ' +
> @.TableName + '.RowNumber = ' + CAST((SELECT RowNumberReference FROM
> Deleted) AS VARCHAR(100))
> EXEC(@.SQLString)
> I have tried with a loop. But it also gives me an error
> WHILE (SELECT TableReference FROM Deleted) IS NOT NULL
> BEGIN
> DECLARE @.TableName VARCHAR(100)
> SET @.TableName = (Select TableReference FROM Deleted)
> DECLARE @.SQLString VARCHAR(1000)
> SET @.SQLString = 'UPDATE ' + @.TableName +
> ' SET DocumentsExists = NULL WHERE ' +
> @.TableName + '.RowNumber = ' + CAST((SELECT RowNumberReference FROM
> Deleted) AS VARCHAR(100))
> EXEC(@.SQLString)
> END
> Any ideas?
> Best regards
> Henrik
>
>

Wednesday, February 15, 2012

error handling and logging within a trigger - how much of this would work / would be

Hi,
a little bit of background info; the T-SQL I'm writing is against a
proprietary database schema, I have triggers which are being called
upon through events in a client application over which I have no
control and the schema can change because of patches/new functionality
The pseudo code below is a collation of things I'm contemplating on
doing in my code. The reason being that I want to provide our
application specialists with the possibility to determine what went
wrong where in our production environment, if and when it does, and the
possibility to test patches / new functionality of the properiaty
system by testing in the user interface against a copy of the
production database.
I'd appreciate any feedback on how much of this pseudo code would work
/ would be required?
create trigger trInserrorHandlingInTrigger on propTable for insert as
declare @.returnStatus int, @.aVar int, @.err int
if @.@.rowcount = 0 return
if @.@.rowcount > 1 return
set @.aVar = (select aVal from aTable)
insert into logTable (Nme, sqlStm, aBool, Val) values
('trInserrorHandlingInTrigger', '', 1, @.aVar)
if @.aVar is null return
save transaction lastResort
EXEC @.returnStatus = sp_1_InsertRecords @.aVar = @.aVar
EXEC @.returnStatus = sp_2_InsertRecordsOrRollbackSpOne @.aVar = @.aVar,
@.returnStatus = @.returnStatus
EXEC @.returnStatus = sp_3_UpdateRecordsOrRollback @.aVar = @.aVar,
@.returnStatus = @.returnStatus
select @.err = @.@.error
if @.err <> 0 rollback transaction lastResort
if @.returnStatus <> 0 rollback transaction lastResort
commit transaction
go
create procedure sp_1_InsertRecords
@.aVar int
as
declare
@.err int
insert into wTable (aVal) values (@.aVar)
select @.err = @.@.error
if @.err <> 0
begin
insert into logTable (Nme, sqlStm, aBool, Val) values
('sp_2_InsertRecordsOrRollbackSpOne','lb
l One', 0, @.aVar)
return 1
end
else insert into logTable (Nme, sqlStm, aBool, Val) values
('sp_2_InsertRecordsOrRollbackSpOne','lb
l One', 1, @.aVar)
go
create procedure sp_2_InsertRecordsOrRollbackSpOne
@.aVar int, @.returnStatus int
as
declare
@.err int
if @.returnStatus = 0
begin
-- lbl One
insert into xTable (aVal) values (@.aVar)
select @.err = @.@.error
if @.err <> 0
begin
insert into logTable (Nme, sqlStm, aBool, Val) values
('sp_2_InsertRecordsOrRollbackSpOne','lb
l One', 0, @.aVar)
return 1
end
else insert into logTable (Nme, sqlStm, aBool, Val) values
('sp_2_InsertRecordsOrRollbackSpOne','lb
l One', 1, @.aVar)
end
else
-- lbl Two
delete from wTable where aVal = @.aVar
select @.err = @.@.error
if @.err <> 0
begin
insert into logTable (Nme, sqlStm, aBool, Val) values
('sp_2_InsertRecordsOrRollbackSpOne','lb
l Two', 0, @.aVar)
return 1
end
else insert into logTable (Nme, sqlStm, aBool, Val) values
('sp_2_InsertRecordsOrRollbackSpOne','lb
l Two', 1, @.aVar)
go
create procedure sp_3_UpdateRecordsOrRollback
@.aVar int, @.returnStatus int
as
declare
@.err int
if @.returnStatus = 0
begin
-- lbl One
update yTable set aVal = @.aVar
select @.err = @.@.error
if @.err <> 0
begin
insert into logTable (Nme, sqlStm, aBool, Val) values
('sp_3_UpdateRecordsOrRollback','lbl One', 0, @.aVar)
return 1
end
else insert into logTable (Nme, sqlStm, aBool, Val) values
('sp_3_UpdateRecordsOrRollback','lbl One', 1, @.aVar)
end
else
-- lbl Two
delete from aTable where aVal = @.aVar
select @.err = @.@.error
if @.err <> 0
begin
insert into logTable (Nme, sqlStm, aBool, Val) values
('sp_3_UpdateRecordsOrRollback','lbl Two', 0, @.aVar)
return 1
end
else insert into logTable (Nme, sqlStm, aBool, Val) values
('sp_3_UpdateRecordsOrRollback','lbl Two', 1, @.aVar)
-- lbl Three
delete from wTable where aVal = @.aVar
select @.err = @.@.error
if @.err <> 0
begin
insert into logTable (Nme, sqlStm, aBool, Val) values
('sp_3_UpdateRecordsOrRollback', 'lbl Three', 0, @.aVar)
return 1
end
else insert into logTable (Nme, sqlStm, aBool, Val) values
('sp_3_UpdateRecordsOrRollback','lbl Three', 1, @.aVar)
go
regards,
GerardGerard (g.doeswijk@.gmail.com) writes:
> create trigger trInserrorHandlingInTrigger on propTable for insert as
> declare @.returnStatus int, @.aVar int, @.err int
>
> if @.@.rowcount = 0 return
> if @.@.rowcount > 1 return
> set @.aVar = (select aVal from aTable)
Would be from inserted, not just any table.

> save transaction lastResort
> EXEC @.returnStatus = sp_1_InsertRecords @.aVar = @.aVar
> EXEC @.returnStatus = sp_2_InsertRecordsOrRollbackSpOne @.aVar = @.aVar,
> @.returnStatus = @.returnStatus
> EXEC @.returnStatus = sp_3_UpdateRecordsOrRollback @.aVar = @.aVar,
> @.returnStatus = @.returnStatus
>
> select @.err = @.@.error
> if @.err <> 0 rollback transaction lastResort
> if @.returnStatus <> 0 rollback transaction lastResort
>
> commit transaction
First, the sp_ prefix is reserved for system procedures, don't use it
for your own code. There could be nasty surprises.
This appears to be a bit overkill. And flat out wrong as for the COMMIT
part.
This is the scoop: if an error occurs when you are in a trigger, including
a stored procedure called by a trigger, the entire batch is aborted, and
the current transaction is rolled back. So this savepoint thing is not
likely to work.
And what's wrong with the COMMIT? If a trigger exits with a lower trancount
that it was entered with, that too aborts the batch, and rolls back any
transaction. The idea is that a rollback in a trigger should roll back
the batch, but if you COMMIT the same thing happens.|||Thanks for the feedback, BOL was not particularly clear for me on the
SAVE TRAN and COMMIT PART. I had sort of thought that there was some
possibility to persist some data outside the scope of the trigger by
giving this commit. I think we just have to start living with the idea
in any case.