variable Scoping

  • Hi all,

    DECLARE @batchSize INT

    Declare @siteid nchar(3)

    DECLARE @Instanceid nchar(3)

    set @siteid = '002'

    SET @InstanceId = '001'

    SELECT @batchSize = CAST(ConfigurationValue AS Int)

    from ApplicationConfiguration

    where ConfigurationDescription='SyncBatchSize' and InstanceId = @InstanceID and SiteiD = @SIteID

    select @batchsize

    -- Result NULL

    SELECT @batchSize = CAST(ConfigurationValue AS Int)

    from ApplicationConfiguration

    where ConfigurationDescription='SyncBatchSize' and InstanceId = '001' and SiteiD = '002'

    select @batchsize

    -- Result 501

    While passing variable I am gettign NULL in result while when I pass static value then it gives me result as 501, why this is happening...

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Maybe because you switched values in your example.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Koen Verbeeck (2/7/2014)


    Maybe because you switched values in your example.

    ohhh I typed incorrectly here... but after make correction also its not working

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • With DDL for TABLE ApplicationConfiguration, specifically data types for InstanceId and SiteiD, someone might be able to propose a hypothesis.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply