Data is Null. This method or property cannot be called on Null values

后端 未结 7 1044
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 20:07

I\'m working on an application where one can get information on movies from a database as well as add, update and delete the movies. In the database I have three tables (Movie,

7条回答
  •  遇见更好的自我
    2021-02-06 20:52

    The simplest answer is to replace the nulls with non-null values. Try:

    ALTER PROCEDURE usp_GetMovieGenreByMovieID
    @MovieID int
    AS
    BEGIN
        BEGIN TRY
            SELECT m.MovieID, 
                   coalesce(g.GenreID,0) GenreID, 
                   coalesce(mg.MovieGenreID,0) MovieGenreID, 
                   coalesce(g.Genre, 'Not Applicable') Genre
            FROM Movie AS m
            LEFT JOIN MovieGenre AS mg
                ON m.MovieId = mg.MovieID
            LEFT JOIN Genre AS g
                ON mg.GenreID = g.GenreID
            WHERE m.MovieID = @MovieID
        END TRY
        BEGIN CATCH
            RAISERROR ('Error while trying to receive genre(s).',16,1)
        END CATCH
    END
    

提交回复
热议问题