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
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Null handling oddity

Null handling oddity

Scheduled Pinned Locked Moved Database
databasesql-servervisual-studiocomgame-dev
9 Posts 6 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
    thrakazog
    wrote on last edited by
    #1

    I was working on a chunk of SQL in management studio. Finally got it to return the results I wanted. Then I converted it to a stored procedure, where it promptly stopped working. Upon tracking the bug down I found that SQL was handling Null variables differently in dynamic SQL vs my stored procedure. I had this bit of code:

    if (@Replace != '')
    set @MaterialNumber = @Replace

    In dynamic SQL if @Replace was null, we did not enter the if block. In a stored procedure when @replace was null we entered the if block and set @MaterialNumber to null. :wtf: To the best of my knowledge, this should never happen. Is this a bug in SQL Server or is there a setting hidden somewhere that could account for this difference?

    Kill some time, play my game Hop Cheops[^]

    C P J C J 5 Replies Last reply
    0
    • T thrakazog

      I was working on a chunk of SQL in management studio. Finally got it to return the results I wanted. Then I converted it to a stored procedure, where it promptly stopped working. Upon tracking the bug down I found that SQL was handling Null variables differently in dynamic SQL vs my stored procedure. I had this bit of code:

      if (@Replace != '')
      set @MaterialNumber = @Replace

      In dynamic SQL if @Replace was null, we did not enter the if block. In a stored procedure when @replace was null we entered the if block and set @MaterialNumber to null. :wtf: To the best of my knowledge, this should never happen. Is this a bug in SQL Server or is there a setting hidden somewhere that could account for this difference?

      Kill some time, play my game Hop Cheops[^]

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #2

      Whenever I check for NULL, I avoid doing so with comparison operators. There should be some IS NULL check available to use instead. :)

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

      1 Reply Last reply
      0
      • T thrakazog

        I was working on a chunk of SQL in management studio. Finally got it to return the results I wanted. Then I converted it to a stored procedure, where it promptly stopped working. Upon tracking the bug down I found that SQL was handling Null variables differently in dynamic SQL vs my stored procedure. I had this bit of code:

        if (@Replace != '')
        set @MaterialNumber = @Replace

        In dynamic SQL if @Replace was null, we did not enter the if block. In a stored procedure when @replace was null we entered the if block and set @MaterialNumber to null. :wtf: To the best of my knowledge, this should never happen. Is this a bug in SQL Server or is there a setting hidden somewhere that could account for this difference?

        Kill some time, play my game Hop Cheops[^]

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        That seems correct to me, but I'm unsure what you mean by "dynamic SQL".

        T 1 Reply Last reply
        0
        • T thrakazog

          I was working on a chunk of SQL in management studio. Finally got it to return the results I wanted. Then I converted it to a stored procedure, where it promptly stopped working. Upon tracking the bug down I found that SQL was handling Null variables differently in dynamic SQL vs my stored procedure. I had this bit of code:

          if (@Replace != '')
          set @MaterialNumber = @Replace

          In dynamic SQL if @Replace was null, we did not enter the if block. In a stored procedure when @replace was null we entered the if block and set @MaterialNumber to null. :wtf: To the best of my knowledge, this should never happen. Is this a bug in SQL Server or is there a setting hidden somewhere that could account for this difference?

          Kill some time, play my game Hop Cheops[^]

          J Offline
          J Offline
          Jorgen Andersson
          wrote on last edited by
          #4

          In SQL-server '' is not the same as null, it's a zero length string.

          Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

          1 Reply Last reply
          0
          • T thrakazog

            I was working on a chunk of SQL in management studio. Finally got it to return the results I wanted. Then I converted it to a stored procedure, where it promptly stopped working. Upon tracking the bug down I found that SQL was handling Null variables differently in dynamic SQL vs my stored procedure. I had this bit of code:

            if (@Replace != '')
            set @MaterialNumber = @Replace

            In dynamic SQL if @Replace was null, we did not enter the if block. In a stored procedure when @replace was null we entered the if block and set @MaterialNumber to null. :wtf: To the best of my knowledge, this should never happen. Is this a bug in SQL Server or is there a setting hidden somewhere that could account for this difference?

            Kill some time, play my game Hop Cheops[^]

            C Offline
            C Offline
            Corporal Agarn
            wrote on last edited by
            #5

            May not be the best way but try:

            IF (ISNULL(@Replace, '') != '')
            SET @MaterialNumber = @Replace;

            T 1 Reply Last reply
            0
            • C Corporal Agarn

              May not be the best way but try:

              IF (ISNULL(@Replace, '') != '')
              SET @MaterialNumber = @Replace;

              T Offline
              T Offline
              thrakazog
              wrote on last edited by
              #6

              My concern wasn't so much the null handling, that's easy enough to work around. I'm more concerned by the fact that the SQL code behaved differently when it was run in a stored procedure VS when it was run directly in Enterprise Manager. This is the first instance I've discovered where there was a difference.

              Kill some time, play my game Hop Cheops[^]

              1 Reply Last reply
              0
              • P PIEBALDconsult

                That seems correct to me, but I'm unsure what you mean by "dynamic SQL".

                T Offline
                T Offline
                thrakazog
                wrote on last edited by
                #7

                By dynamic I meant SQL run directly in Enterprise Manager. When run in a stored procedure the SQL code behaved one way, when run directly in Enterprise Manager it behaved another way. This is the first instance I've discovered where there was a difference between the two.

                Kill some time, play my game Hop Cheops[^]

                1 Reply Last reply
                0
                • T thrakazog

                  I was working on a chunk of SQL in management studio. Finally got it to return the results I wanted. Then I converted it to a stored procedure, where it promptly stopped working. Upon tracking the bug down I found that SQL was handling Null variables differently in dynamic SQL vs my stored procedure. I had this bit of code:

                  if (@Replace != '')
                  set @MaterialNumber = @Replace

                  In dynamic SQL if @Replace was null, we did not enter the if block. In a stored procedure when @replace was null we entered the if block and set @MaterialNumber to null. :wtf: To the best of my knowledge, this should never happen. Is this a bug in SQL Server or is there a setting hidden somewhere that could account for this difference?

                  Kill some time, play my game Hop Cheops[^]

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #8

                  thrakazog wrote:

                  In dynamic SQL if @Replace was null, we did not enter the if block.

                  Create a stored procedure that demonstrates exactly that and then post it.

                  T 1 Reply Last reply
                  0
                  • J jschell

                    thrakazog wrote:

                    In dynamic SQL if @Replace was null, we did not enter the if block.

                    Create a stored procedure that demonstrates exactly that and then post it.

                    T Offline
                    T Offline
                    thrakazog
                    wrote on last edited by
                    #9

                    Gaaaah, I finally found the cause of the problem. I apparently created my original sproc with "SET ANSI_NULLS OFF". By default queries in management studio runs with ANSI_NULLS ON. DOH![^] For a simple example of the sproc I was playing with:

                    create PROCEDURE [dbo].[Bob_Test]
                    @MaterialNumber varchar(18)
                    AS

                    DECLARE @Replace varchar(18)

                    SELECT @MaterialNumber

                    IF (@Replace != '')
                    SET @MaterialNumber = @Replace

                    SELECT @MaterialNumber

                    When I call this with ANSI_NULLS OFF

                    Bob_Test '123456'

                    my results show 123456 from the first select in the sproc, and NULL for the select after the if statement. With ANSI_NULLS ON both selects return 123456. My faith SQL Server has been restored. Tune in next week for more inept coding... :cool:

                    Kill some time, play my game Hop Cheops[^]

                    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