Stored procedure parameters

  • gary.rumble

    SSCommitted

    Points: 1725

    Comments posted to this topic are about the item Stored procedure parameters

  • SuperDBA-207096

    SSCrazy Eights

    Points: 8176

    Interesting question. Shows the results may not necessarily be what you'd expect.

    I've been burnt by this before, good knowlege!

  • Aaron N. Cutshall

    SSCrazy Eights

    Points: 8740

    I've been burnt by this "feature" too often to not recognize it! It took me hours to track down an issue cause by a string truncation that was being passed as a parameter to an SP. Increasing the parameter size was all it took to fix it.

    Time spent tracking down the problem: 6.5 hours

    Time spent actually fixing the problem: 5 seconds

  • Slick84

    SSCertifiable

    Points: 5602

    So...why exactly is this happening? I thought the whole 'This is a test string' without the quotes would show up in my result-set.

    --
    :hehe:

  • Aaron N. Cutshall

    SSCrazy Eights

    Points: 8740

    Slick84 (8/10/2009)


    So...why exactly is this happening? I thought the whole 'This is a test string' without the quotes would show up in my result-set.

    If you'll notice, the parameter to the SP is limited to a length of 4 characters. Therefore, the value passed in gets truncated to a maximum length of 4 characters without any warning or notice. It can cause some nasty side-affects if something down the road is dependent upon that parameter.

  • Slick84

    SSCertifiable

    Points: 5602

    That's correct. I missed that. Thanks!

    --
    :hehe:

  • Oleg Netchaev

    SSCertifiable

    Points: 5268

    Aaron N. Cutshall (8/10/2009)


    I've been burnt by this "feature" too often to not recognize it! It took me hours to track down an issue cause by a string truncation that was being passed as a parameter to an SP. Increasing the parameter size was all it took to fix it.

    Time spent tracking down the problem: 6.5 hours

    Time spent actually fixing the problem: 5 seconds

    It can get even more interesting (and fun to debug) if the value of the parameter can be evaluated by the engine to one of the numeric data types, such as int. Then you can get a * as a result of the procedure execution. For example,

    create proc dbo.testProc

    @testVar varchar(4)

    as

    select @testVar

    GO

    exec testProc 123456

    GO

    The result of the above is (with results to text option)

    ----

    *

    (1 row(s) affected)

    While this behavior is by design, it still is a very nice curve ball 🙂

    Oleg

  • myheadhurts

    Ten Centuries

    Points: 1262

    Explanation: Stored procedure calls will silently truncate strings.

    I believe the Explanation here is more fundamental than "Stored procedure calls...", instead having to do simply with the declaration of the var.

    Note that this likewise returns just 4 chars:

    declare @testVar varchar(4)

    set @testVar = 'This is a test string'

    select @testVar

    -MarkO

    "You do not really understand something until you can explain it to your grandmother" - Albert Einstein

  • john.arnott

    SSChampion

    Points: 11882

    Aaron N. Cutshall (8/10/2009)


    Time spent tracking down the problem: 6.5 hours

    Time spent actually fixing the problem: 5 seconds

    This reminds me of the story about a field engineer being called in to fix an HVAC (Heating, Ventilation, Air Conditioning) control system for a large building after the on-site guys couldn't make it work. She walks in. Looks at the controls. Re-positions three knobs and hits the restart button. When the building manager looks at the invoice for the call and says "how can you justify $500 just for turning three knobs?", the FE replies "I should have broken that down. It was $10 for turning the knobs, and $490 for knowing which knobs and how far to turn them."

  • Jon Cooney

    Old Hand

    Points: 329

    This is a cheeky one.

    Technet (link) says the following:

    The data type of a parameter determines the type and range of values that are accepted for the parameter. For example, if you define a parameter with a tinyint data type, only numeric values ranging from 0 to 255 are accepted. An error is returned if a stored procedure is executed with a value incompatible with the data type.

    You would ASSUME 'range of values' implies string length here. Obviously it doesn't.

  • Aaron N. Cutshall

    SSCrazy Eights

    Points: 8740

    joncooney73 (8/13/2009)


    You would ASSUME 'range of values' implies string length here. Obviously it doesn't.

    I don't believe so. The range of numeric values is limited because the datatype is limited. Obviously, the datatype of int is not the same as the datatype of long.

    However, in the case of varchar, the length of the string that it can hold does not alter the definition of the datatype. varchar(4) is the same datatype as varchar(8000). The maximum length of the string that it holds is a property of the implementation of that datatype -- not the definition of the datatype.

    Hope that helps!

  • SQLRNNR

    SSC Guru

    Points: 281205

    This question was good in that it pointed out something really simple, yet way to easy to overlook when troubleshooting or code-reviewing in the first place.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Bhavesh_Patel

    SSCrazy

    Points: 2199

    It was easy one!:-)



    [font="System"]Bhavesh Patel[/font]

    http://bhaveshgpatel.wordpress.com/

Viewing 13 posts - 1 through 13 (of 13 total)

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