Discussion Locked

SQL Server Trigger Exec Deleted Table

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

In SQL server 2000, in a trigger
When i use the Exec(), it cannot find the deleted table. Why?

EXECUTE ('Insert into TestTable Select PatientID from Deleted')

9 replies

by surinderx

Dear

You must log on to akadia.com

Lv

Surinder Kapoor
Sr. EDP Incharge
Vishal Group of Companies

by mark_t

I got the following to work, not sure if its of use or what your looking for, still you might be able
to adapt it.

Run this script, it'll create 2 tables with a trigger on 1, also included is a stored procedure.
The table/stored procedure namesare a bit confusing, but it should be easy to follow anyhow.

------------------------------------------------------------------------------------------------------------
CREATE TABLE [dbo].[tbl_Temp] (
[tablename] [char] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[description] [char] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[tbl_Temp_Audit] (
[triggertable] [char] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[description] [char] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

CREATE PROCEDURE sp_Insert_TriggerTable
@TriggerTable char(30),
@Description char(50)
AS

BEGIN

declare @insertstring char(200)

select @insertstring = 'insert into ' + @TriggerTable + ' VALUES (' + char(39) + @TriggerTable + char(39) + ',' + char(39) + @Description + char(39)+ ')'

EXECUTE(@insertstring)

END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
------------------------------------------------------------------------------------------------------------

From iSQL run

insert into tbl_temp values ('tbl_temp_audit','Test 1')

The trigger for tbl_temp should insert a record into tbl_temp_audit using the stored procedure.

See if this is any good, let me know either way.

Mark

by prashant_bajaj

This insert could be done without using the EXEC call. Please let me know why you want to enforce it using EXEC.

This discussion is closed to new replies.