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 9181675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:22:28+00:00 2026-06-17T18:22:28+00:00

I am trying to create a simple to insert trigger that gets the count

  • 0

I am trying to create a simple to insert trigger that gets the count from a table and adds it to another like this

CREATE TABLE [poll-count](
id VARCHAR(100),
altid BIGINT,
option_order BIGINT,
uip VARCHAR(50),
[uid] VARCHAR(100),
[order] BIGINT
PRIMARY KEY NONCLUSTERED([order]),
FOREIGN KEY ([order]) references ord ([order]  
)
GO

CREATE TRIGGER [get-poll-count]
ON [poll-count]
FOR INSERT 
AS 
    BEGIN
        DECLARE @count INT
        SET @count = (SELECT COUNT (*) FROM [poll-count] WHERE option_order = i.option_order)
        UPDATE [poll-options] SET [total] = @count WHERE [order] = i.option_order
    END
GO

when i ever i try to run this i get this error:

The multi-part identifier “i.option_order” could not be bound

what is the problem?

thanks

  • 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-06-17T18:22:29+00:00Added an answer on June 17, 2026 at 6:22 pm

    Your trigger currently assumes that there will always be one-row inserts. Have you tried your trigger with anything like this?

    INSERT dbo.[poll-options](option_order --, ...)
      VALUES(1 --, ...),
            (2 --, ...);
    

    Also, you say that SQL Server “cannot access inserted table” – yet your statement says this. Where do you reference inserted (even if this were a valid subquery structure)?

    SET @count = (SELECT COUNT (*) FROM [poll-count] 
      WHERE option_order = i.option_order)
    -----------------------^ "i" <> "inserted"
    

    Here is a trigger that properly references inserted and also properly handles multi-row inserts:

    CREATE TRIGGER dbo.pollupdate
    ON dbo.[poll-options]
    FOR INSERT
    AS
    BEGIN
        SET NOCOUNT ON;
    
        ;WITH x AS 
        (
          SELECT option_order, c = COUNT(*)
           FROM dbo.[poll-options] AS p
           WHERE EXISTS 
           (
            SELECT 1 FROM inserted 
            WHERE option_order = p.option_order
           )
           GROUP BY option_order
        )
        UPDATE p SET total = x.c
          FROM dbo.[poll-options] AS p
          INNER JOIN x 
          ON p.option_order = x.option_order;
    END
    GO
    

    However, why do you want to store this data on every row? You can always derive the count at runtime, know that it is perfectly up to date, and avoid the need for a trigger altogether. If it’s about the performance aspect of deriving the count at runtime, a much easier way to implement this write-ahead optimization for about the same maintenance cost during DML is to create an indexed view:

    CREATE VIEW dbo.[poll-options-count]
    WITH SCHEMABINDING
    AS
      SELECT option_order, c = COUNT_BIG(*)
        FROM dbo.[poll-options]
        GROUP BY option_order;
    GO
    CREATE UNIQUE CLUSTERED INDEX oo ON dbo.[poll-options-count](option_order);
    GO
    

    Now the index is maintained for you and you can derive very quick counts for any given (or all) option_order values. You’ll have test, of course, whether the improvement in query time is worth the increased maintenance (though you are already paying that price with the trigger, except that it can affect many more rows in any given insert, so…).

    As a final suggestion, don’t use special characters like - in object names. It just forces you to always wrap it in [square brackets] and that’s no fun for anyone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a simple insert trigger from Products table to ProductPrice
I'm trying to write a table trigger which queries another table that is outside
I'm trying to create a fairly simple trigger that would add one to a
I'm trying to create a simple Before Insert MySQL Trigger, to check if any
I am trying to create a Trigger, that will update the History Table with
I'm trying to create simple script that subscribes a user to a company calendar.
I am trying to create a simple about page that uses JQuery click to
I'm trying to create a simple web page that resembles the following: I've got
I'm trying to create a simple textblock control and I'm trying to insert a
I'm trying to create a simple program that waits for a file using sockets

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.