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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:08:16+00:00 2026-06-09T10:08:16+00:00

I’m trying to create a fairly simple trigger that would add one to a

  • 0

I’m trying to create a fairly simple trigger that would add one to a column that keeps track of the number of rentals from a movie distribution company similar to Netflix.

The columns I am focused on are:

  • Movies (movie_id, movie_title, release_year, num_rentals)
  • Customer_rentals (item_rental_id, movie_id, rental_date_out, rental_date_returned)

My current trigger looks like this:

CREATE TRIGGER tr_num_rented_insert
ON customer_rentals FOR INSERT
AS
BEGIN 
UPDATE movies
SET num_rentals=num_rentals+1
WHERE customer_rentals.movie_id=movies.movie_id;
END;

It returns the error:

Msg 4104, Level 16, State 1, Procedure tr_num_rented_insert, Line 7
The multi-part identifier “customer_rentals.movie_id” could not be
bound.

I just want it to match the movie_id’s and add 1 to the number of rentals.

  • 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-09T10:08:19+00:00Added an answer on June 9, 2026 at 10:08 am

    You need to join to the inserted pseudo-table:

    CREATE TRIGGER dbo.tr_num_rented_insert
    ON dbo.customer_rentals 
    FOR INSERT
    AS
    BEGIN 
      UPDATE m
        SET num_rentals = num_rentals + 1
      FROM dbo.movies AS m
      INNER JOIN inserted AS i
      ON m.movie_id = i.movie_id;
    END
    GO
    

    But I have to ask, what is the point of keeping this count up to date in the movies table? You can always get the count in a query instead of storing it redundantly:

    SELECT m.movie_id, COALESCE(COUNT(r.movie_id))
      FROM dbo.moves AS m
      LEFT OUTER JOIN dbo.customer_rentals AS r
      ON m.movie_id = r.movie_id
      GROUP BY m.movie_id;
    

    And if performance of that query becomes an issue, you can create an indexed view to maintain the count so that you don’t have to keep it up to date with a trigger:

    CREATE VIEW dbo.rental_counts
    WITH SCHEMABINDING
    AS
      SELECT movie_id, num_rentals = COUNT_BIG(*)
      FROM dbo.customer_rentals
      GROUP BY movie_id;
    

    This causes the same kind of maintenance as your trigger, but does it without your trigger, and does it without affecting the movies table. Now to get the rental counts you can just say:

    SELECT m.movie_id, m.other_columns, 
        num_rentals = COALESCE(r.num_rentals, 0)
      FROM dbo.movies AS m
      LEFT OUTER JOIN dbo.rental_counts AS r
      ON m.movie_id = r.movie_id;
    

    (We use a LEFT JOIN here because a movie may not have been rented yet.)

    An additional bonus here is that you don’t have to perform any tricks to get other columns from the movies table into the result. It also ensures that the data is accurate even if a rental is deleted (your trigger will continue to happily add +1 to the count).

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

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.