My trigger is as below ,
Alter TRIGGER [dbo].[LogTable_InsertTrigger] on [dbo].[std_table] AFTER INSERT
as
DECLARE @ColName varchar(50), @QueryText nvarchar(max)
declare @inserted TABLE(
[CountryID] [int] NOT NULL,
[Country] [nvarchar](255) NOT NULL,
[RegionId] [int] NULL
)
insert into @inserted
select * from inserted
DECLARE objCursor CURSOR FAST_FORWARD FOR
select ColName from dbo.getColumnNames('std_table')
OPEN objCursor
FETCH NEXT FROM objCursor INTO @ColName
WHILE @@FETCH_STATUS = 0
BEGIN
set @QueryText= '
insert into dbo.LogTable
(StandardType,Attribute,Action,NEwValue,UserId,ModifiedDate)
select ''Country'','''+@ColName+''',''Insert'','+@ColName+',1,getdate()
from @inserted'
EXEC sp_executesql @QueryText
FETCH NEXT FROM objCursor INTO @ColName
END
CLOSE objCursor
DEALLOCATE objCursor
When I try to insert to table std_table in DA Layer I get the exception Must declare the table variable "@inserted".
I couldn't use the inserted table directly because I am writing a dynamic query, inside which magic tables wont work. So I am trying to dump the data in inserted table to a temp table and to access from that.
I tried with
select *
into #inserted
from inserted
This works, but since my application is accessed by many users over network this will cause data issues. So I can't use this.