Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a stored procedure to insert or delete a movie in/from a table Movies.
The input parameters are Title and Director of the movie.
If the movie doesn't exist it will be created, otherwise I will increase the number of copies (the movie has an INT attribute 'numberOfCopies' to keep trace of it).
If I can't delete it the entire operation has to be cancelled.
I wrote the following batch code but when I run it I get the following error for each column:

the multi-part identifier Movies.NameOfColumn could not be bound


And also this one:

Invalid column name 'SCOPE:IDENTITY'


What I have tried:

CREATE PROCEDURE up_Movie_StoredProcedure 
@Title NVARCHAR(300),
@Director NVARCHAR(50)
@NumberOfCopies INT OUTPUT         -- parameters 
AS
-- Insert -->
IF  (
     Movie.Title != @Title
     AND Movie.Director != @Director
     	 )
    BEGIN  -- code block runs if condition is true
    INSERT INTO Movies(Title, Director)
	       VALUES (@Title, @Director );
		   SELECT SCOPE_IDENTITY;
    END 
ELSE
   BEGIN 
   UPDATE Movies
   SET Title = @Title,
       Director = @Director
       NumberOfCopies = NumberOfCopies + 1   -- output number of copies
   WHERE Title = @Title 
   OR   Director = @Director
   END
BEGIN
     SET @NumberOfCopies = (SELECT NumberOfCopies FROM Movies WHERE Title = @Title AND Dirctor = @Director);
   END

-- delete -->
BEGIN TRY
   BEGIN TRANSACTION
       DELETE 
	   FROM Movies
	   WHERE Title = @Title 
	     AND Director = @Director;
        DELETE r
		FROM Reservations r
		INNER JOIN Movies m ON m.IDMOvies = r.IDMovies
		WHERE m.Title = @Title
	      AND m.Director = @Director;
  COMMIT Transaction
END TRY
BEGIN CATCH
   ROLLBACK Transaction
END CATCH
Posted
Updated 9-Nov-20 5:01am
v9
Comments
[no name] 9-Nov-20 7:40am    
Select count(*) for title and director. If the count is 0, you "insert" (based on your specs).
Richard Deeming 9-Nov-20 7:42am    
Please stop using this UPSERT anti-pattern - SQLPerformance.com[^]

Run the UPDATE first. If no rows were affected, run the INSERT.

1 solution

There is nothing passed in to the procedure to indicate whether you want to insert/update the movie or delete the movie.

You should have two different procedures - one to insert or update:
SQL
CREATE PROCEDURE AddMovie
(
    @Title NVARCHAR(10), 
    @Director NVARCHAR(10)
)
As
BEGIN
    SET NOCOUNT ON;
    
    UPDATE
        Movies
    SET
        NumberOfCopies = NumberOfCopies + 1
    WHERE
        Title = @Title
    And
        Director = @Director
    ;
    
    If @@ROWCOUNT = 0
    BEGIN
        INSERT INTO Movies
        (
            Title,
            Director,
            NumberOfCopies
        )
        VALUES
        (
            @Title,
            @Director,
            1
        );
    END;
    
    SELECT
        NumberOfCopies
    FROM
        Movies
    WHERE
        Title = @Title
    And
        Director = @Director
    ;
END
and one to delete:
SQL
CREATE PROCEDURE RemoveMovie
(
    @Title NVARCHAR(10), 
    @Director NVARCHAR(10)
)
As
BEGIN
    SET NOCOUNT ON;
    
    BEGIN TRY;
    BEGIN TRANSACTION;
        
        DELETE
        FROM R
        FROM Reservations As R
        INNER JOIN Movies As M ON M.IDMovie = R.IDMovie
        WHERE M.Title = @Title
        And M.Director = @Director;
        
        DELETE
        FROM Movies
        WHERE Title = @Title
        And Director = @Director;
        
        COMMIT;
    END TRY
    BEGIN CATCH;
        IF @@TRANCOUNT > 0 ROLLBACK;
        THROW;
    END CATCH;
END    
 
Share this answer
 
Comments
Richard Deeming 9-Nov-20 7:47am    
Then do that, and check the parameter within the stored procedure.

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