SQL Server - Problem with recursive call of trigger.

Asked By inf
27-Jul-07 02:32 PM
Hi.
I have a problem with UPDATE trigger and SQL 2005, when trigger calls
update of the table. The sample environment, table 'test' with
trigger:

create table webdata..test(col int, help int)
go

insert into webdata..test values (1,1)
go

CREATE TRIGGER [ud_test] ON  webdata..test AFTER UPDATE  AS
BEGIN
IF NOT UPDATE(help)
exec xp_cmdshell 'SQLCMD.EXE -Q "update webdata..test set
col=1,help=1 where col=1"'

END
GO

update webdata..test  set col=1  where col=1
Go

The problem: Trigger calls update statement on the same table. This
construction works without problems in older SQL version, but update
in SQL 2005 locks the record in table and the SQLCMD.EXE timeouts.

The one solution is an Asynchronous call of the command line. But I
need solution for this case.

Thank you.
SQL Server 2005
(1)
SQL Server 2000
(1)
SQL Server
(1)
Recursive
(1)
Trigger
(1)
Exec
(1)
Runnable
(1)
SQLCMD
(1)
  Tibor Karaszi replied...
27-Jul-07 02:57 PM
That should not work in prior releases, since you shell out and this gets a new connection. SQL
Server doesn't understand the relationship between the "mother" connection and the "child"
connection.

Unless it isn't the same row you are updating, of course. If it isn't the same row, then you might
see different level of locks between the versions (for various reasons, but lets not get into this
unless this actually is the case).

Can you post a runnable repro that *does* work in an earlier release? I could fire up my 2000 VPC,
but I want to be certain that I first have something to execute that does work.

You might want to investigate sp_getbindtoken. But this is deprecated, and also you will have to
pass that token to the child process somehow.

I should also add that as the script is written the while xp_cmdshell and SQLCMD is not necessary,
but I assume you know that and your case is more convoluted.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
  Erland Sommarskog replied...
29-Jul-07 07:33 PM
(info@pstruh.cz) writes:

Pray tell, why are you not updating the table directly in the trigger?

As Tibor said, the above is asking for trouble. There is a fair chance
that the statement that fired the trigger will block the rows the
other process needs to access.

Also, keep in mind that any security-aware DBA may flatly refuse to
enable xp_cmdshell.

I think it's back to the drawing board for a redesign.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Create New Account
help
update date using trigger? SQL Server I have a last updated column in a table and i want the date to be set to the current date on update. I was hoping to use an update trigger on the table, but thought of a logic issue. If i set it to update way cooler than vb and there''s no go''n back!!!) thanks (as always) kes SQL Server Programming Discussions SQL Server 2005 (1) SQL Server 2000 (1) Stored procedure (1) Recursive (1) Trigger (1) Disk (1
Preventing infinite recursion SQL Server I have a table that I am using a CTE to get the parent / child prevent someone from accidently adding a record manually that would cause the problem. Thanks, Tom SQL Server Programming Discussions SQL Server 2005 (1) SQL Server (1) MySQL (1) Oracle (1) CREATE FUNCTION (1) CREATE TABLE (1) PRIMARY KEY (1
Automated way to discover the structure of the result set of a stored procedure? SQL Server Is it possible to write a script that, as its input, takes the name of stored procedure and manually constructing a definition for the result set. Can anyone help? Behzad SQL Server Discussions SQL Server (1) Stored procedure (1) CommandBehavior (1) SELECT INTO (1) Distributed (1) Recursive (1) Catalog (1) Trigger (1) Behzad Sadeghi (behzad.sadeghi@gmail.com) writes: For the general case, the answer is
Hierarchyid is long, how to index? SQL Server Got a table that holds multiple hierarchies in the same structure. Suggestions for redesign not that type, starting at any node that the user specifies. I created another column of SQL server heirarchyID datatype with a CTE to make it easier to traverse the tree instead of functions for manipulating a nested sets tree instead of doing the enumerated path? THanks, Bill SQL Server Discussions IDENTFIERvarchar (1) SQL Server (1) Oracle (1) Stored procedure (1) CREATE TABLE (1) Nonclustered (1) ALTER TABLE (1) PRIMARY
Create Adjancency Model From Flat Data? SQL Server Gurus, I have some book club categories in a text file. I need to fit IT | Computer Science | Project Management Online | IT | Software | Database | Microsoft Online | IT | Software | Database | Microsoft | SQL Server Online | IT | Software | Database | Microsoft | SQL Server | DML Manip Online | IT | Software | Database | Microsoft | SQL Server | DDL Manip SQL Server Programming Discussions SQL Server 2005 (1) SQL Server (1) Oracle (1