Click here to Skip to main content
16,020,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to create and execute a temp table inside trigger with a specific table name in sql server. when I write the sql script to crate the temp table it's created but something different table name used hex code. please give the solution at your earliest. I am providing you a test trigger for delete sql script below:

ALTER TRIGGER [dbo].[trdeleteOperation]
on [dbo].[Person]
after delete AS
begin
--********************** #temp table ************
IF OBJECT_ID (N'#tmp') IS NULL
BEGIN
BEGIN TRY

CREATE TABLE #cmmsGlobal
(
AuditUser varchar(36) not null default '',
AuditIP varchar(39) not null default '',
AuditNotes varchar(4000) not null default '',
AuditClientUTC datetime not null default getutcdate()
)
INSERT INTO #tmp(AuditUser,AuditIP,AuditNotes,AuditClientUTC)
values('','','',getutcdate())
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber;
END CATCH
END

--******************************************

END

After trigger fire the temp table name will some hex code (like #B021...) not #tmp. Please suggest and give me an example for this solution or correction the above.
Posted
Comments
Mathi Mani 12-Jun-15 17:39pm    
What is your question? Is it why hex code is added? or are you facing any problem in accessing your temp table?

1 solution

First problem with your sample trigger is that you are trying to insert into a table that doesn't exist - you create #cmmsGlobal but then try to use #tmp

Once the trigger has completed then any temporary tables created by it go out of scope and are not accessible to the session that instigated the trigger - check out the documentation for CREATE[^]

It doesn't really make sense to use a temporary table in this way.

Either create an audit table on your main database, or you could create an actual table on tempdb

e.g.
CREATE TABLE [tempdb].[dbo].tmp 
... 
 
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