Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Why is a null value still being passed to the next insert statement in this stored procedure?

Why is a null value still being passed to the next insert statement in this stored procedure?

Scheduled Pinned Locked Moved Database
databasehelpquestionlearning
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tonyonlinux
    wrote on last edited by
    #1

    CREATE PROCEDURE usp_AddBook
    --Variables
    @Title varchar(200),
    @Price decimal(18,2),
    @Keywords varchar(200),
    @Retired bit

    AS

    BEGIN
    SET NOCOUNT ON

    DECLARE @bookId int -- SET A VARIABLE TO HOLD THE BOOKID SQL MAKES

    -- INSERT THE BOOK INFORMATION
    SELECT @bookId = Book.ID from Book where Book.Title = @Title

    IF @bookId = null
    BEGIN
    INSERT Book(Title,Price,Keywords,Retired)
    VALUES
    (@Title,@Price,@Keywords,@Retired)
    SELECT @bookId = @@IDENTITY; -- SET THE BOOK ID TO THE ID SQL MADE
    END

    INSERT INTO BookNumber (BookID)
    VALUES
    (@bookId)
    END

    From my view It should check to see if the @bookID is null and if it is then do the insert if its not null then the book already exist and i simply wanna insert the bookID into the booknumber table. The sql manager keeps telling me I can't pass a null to booknumber.bookID (well yeah its non nullable column, but it shouldn't be getting a null value HELP PLEASE :) THANKS!

    L 1 Reply Last reply
    0
    • T tonyonlinux

      CREATE PROCEDURE usp_AddBook
      --Variables
      @Title varchar(200),
      @Price decimal(18,2),
      @Keywords varchar(200),
      @Retired bit

      AS

      BEGIN
      SET NOCOUNT ON

      DECLARE @bookId int -- SET A VARIABLE TO HOLD THE BOOKID SQL MAKES

      -- INSERT THE BOOK INFORMATION
      SELECT @bookId = Book.ID from Book where Book.Title = @Title

      IF @bookId = null
      BEGIN
      INSERT Book(Title,Price,Keywords,Retired)
      VALUES
      (@Title,@Price,@Keywords,@Retired)
      SELECT @bookId = @@IDENTITY; -- SET THE BOOK ID TO THE ID SQL MADE
      END

      INSERT INTO BookNumber (BookID)
      VALUES
      (@bookId)
      END

      From my view It should check to see if the @bookID is null and if it is then do the insert if its not null then the book already exist and i simply wanna insert the bookID into the booknumber table. The sql manager keeps telling me I can't pass a null to booknumber.bookID (well yeah its non nullable column, but it shouldn't be getting a null value HELP PLEASE :) THANKS!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Shouldn't that be

      IF @bookId IS NULL

      ?

      I are Troll :suss:

      T 1 Reply Last reply
      0
      • L Lost User

        Shouldn't that be

        IF @bookId IS NULL

        ?

        I are Troll :suss:

        T Offline
        T Offline
        tonyonlinux
        wrote on last edited by
        #3

        ha thanks. let me give that a go. i appreciate it!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups