Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7528697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:27:11+00:00 2026-05-30T04:27:11+00:00

My trigger is as below , Alter TRIGGER [dbo].[LogTable_InsertTrigger] on [dbo].[std_table] AFTER INSERT as

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:27:13+00:00Added an answer on May 30, 2026 at 4:27 am

    Several things wrong here.

    1. That is a table variable, not a user defined table type.
    2. If #temp tables work, why do you think that will cause data issues for multiple users? Each user will get their own version of the #temp table.
    3. If you know there are exactly three columns and you can hard-code the table variable declaration, why do you need to then generate the three sets of inserts dynamically? Aren’t the column names CountryID,Country,RegionID?

    If you really need to do this dynamically then it seems like you could do this an easier way without an explicit cursor – not that this is necessarily a bad thing or that it will perform any worse than the below, but the cursor is just much more verbose and ugly code:

    ALTER TRIGGER [dbo].[LogTable_InsertTrigger] 
    ON [dbo].[std_table]  
    AFTER  INSERT
    AS
    BEGIN
      SET NOCOUNT ON;
    
      SELECT * INTO #t FROM inserted;
    
      DECLARE @sql NVARCHAR(MAX);
    
      SET @sql = N'';
    
      SELECT @sql = @sql + CHAR(13) + CHAR(10) + N'INSERT INTO dbo.LogTable
            (StandardType,Attribute,Action,NewValue,UserId,ModifiedDate)
            SELECT ''Country'','''+ColName+''',''Insert'','+ColName+',1,GETDATE() 
         FROM #t;'
      FROM dbo.GetColumnNames('std_table');
    
      EXEC sp_executesql @sql;
    END
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have below trigger ALTER TRIGGER [dbo].[DeleteUserData] ON [dbo].[site_users] AFTER DELETE AS BEGIN SET
I created a trigger as below: CREATE OR REPLACE TRIGGER trigger_test AFTER INSERT ON
The trigger below is delaying my insert response. How can I prevent this? create
We have created a after logon trigger to alter the session in order to
The old version of the question is below, after researching more, I decided to
How can I enable or trigger the submit event after ajax post? For instance
I need to alter a trigger in sql server 2005 and I want to
I am newbie in SQL.I am reading Triggers created two trigger on a table.Below
I have a statement level trigger that fires whenever INSERT UPDATE or DELETE operations
When trying to create the trigger below, I get this message #1064 - You

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.