August 8, 2009 at 4:56 pm
Comments posted to this topic are about the item Stored procedure parameters
14090 SW TENNESSEE LN
August 10, 2009 at 4:31 am
Interesting question. Shows the results may not necessarily be what you'd expect.
I've been burnt by this before, good knowlege!
August 10, 2009 at 6:40 am
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
LinkedIn: https://www.linkedin.com/in/sqlrv
Website: https://www.sqlrv.com
August 10, 2009 at 6:49 am
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:
August 10, 2009 at 6:56 am
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.
LinkedIn: https://www.linkedin.com/in/sqlrv
Website: https://www.sqlrv.com
August 10, 2009 at 7:02 am
That's correct. I missed that. Thanks!
--
:hehe:
August 10, 2009 at 9:19 am
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
August 10, 2009 at 10:26 am
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
August 10, 2009 at 6:35 pm
Aaron N. Cutshall (8/10/2009)
Time spent tracking down the problem: 6.5 hoursTime 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."
August 13, 2009 at 9:26 am
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.
August 13, 2009 at 9:36 am
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!
LinkedIn: https://www.linkedin.com/in/sqlrv
Website: https://www.sqlrv.com
August 24, 2009 at 5:56 pm
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
November 21, 2009 at 2:12 am
It was easy one!:-)
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply