dbTalk Databases Forums  

INVALID LENGTH PARAMETER PASSED....

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss INVALID LENGTH PARAMETER PASSED.... in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
ansonee
 
Posts: n/a

Default INVALID LENGTH PARAMETER PASSED.... - 06-04-2007 , 12:43 PM






I have the follwoing stored procedure:

ALTER procedure [dbo].[up_GetExecutionContext](
@ExecutionGUID int = null
) as
begin
set nocount on

declare @s varchar(500)
declare @i int

set @s = ''
select @s = @s + EventType + ',' -- Dynamically build the list of
events
from(
select distinct top 100 percent [event] as EventType
from dbo.PackageStep
where (@ExecutionGUID is null or PackageStep.packagerunid =
@ExecutionGUID)
order by 1
) as x

set @i = len(@s)
select case @i
when 500 then left(@s, @i - 3) + '...' -- If string is too long then
terminate with '...'
else left(@s, @i - 1) -- else just remove the final comma
end as 'Context'

set nocount off
end --procedure
GO

When I run this and pass in a value of NULL, things work fine. When I
pass in an actual value (i.e. 15198), I get the following message:

Invalid length parameter passed to the SUBSTRING function.

There is no SUBSTRING being used anywhere in the query and the
datatypes look okay to me.

Any suggestions would be greatly appreciated.

Thanks!!


Reply With Quote
  #2  
Old   
rshivaraman@gmail.com
 
Posts: n/a

Default Re: INVALID LENGTH PARAMETER PASSED.... - 06-04-2007 , 01:11 PM






On Jun 4, 12:43 pm, ansonee <anso... (AT) yahoo (DOT) com> wrote:
Quote:
I have the follwoing stored procedure:

ALTER procedure [dbo].[up_GetExecutionContext](
@ExecutionGUID int = null
) as
begin
set nocount on

declare @s varchar(500)
declare @i int

set @s = ''
select @s = @s + EventType + ',' -- Dynamically build the list of
events
from(
select distinct top 100 percent [event] as EventType
from dbo.PackageStep
where (@ExecutionGUID is null or PackageStep.packagerunid =
@ExecutionGUID)
order by 1
) as x

set @i = len(@s)
select case @i
when 500 then left(@s, @i - 3) + '...' -- If string is too long then
terminate with '...'
else left(@s, @i - 1) -- else just remove the final comma
end as 'Context'

set nocount off
end --procedure
GO

When I run this and pass in a value of NULL, things work fine. When I
pass in an actual value (i.e. 15198), I get the following message:

Invalid length parameter passed to the SUBSTRING function.

There is no SUBSTRING being used anywhere in the query and the
datatypes look okay to me.

Any suggestions would be greatly appreciated.

Thanks!!
increase the value of @s from 500 to 5000 maybe and test it ?



Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: INVALID LENGTH PARAMETER PASSED.... - 06-04-2007 , 05:34 PM



ansonee (ansonee (AT) yahoo (DOT) com) writes:
Quote:
set @s = ''
select @s = @s + EventType + ',' -- Dynamically build the list of
events
from(
select distinct top 100 percent [event] as EventType
from dbo.PackageStep
where (@ExecutionGUID is null or PackageStep.packagerunid =
@ExecutionGUID)
order by 1
) as x
I'm afraid that this relies on undefined behaviour. It may produce what
you want today. It might not tomorrow. If you are on SQL 2000, you
will need to run a cursor. On SQL 2005 there exists an option with
XML. See SQL Server MVP Antith Sen's article on
http://www.projectdmx.com/tsql/rowconcatenate.aspx for more information.

Quote:
set @i = len(@s)
select case @i
when 500 then left(@s, @i - 3) + '...' -- If string is too
long then
terminate with '...'
else left(@s, @i - 1) -- else just remove the final comma
end as 'Context'

set nocount off
end --procedure
GO

When I run this and pass in a value of NULL, things work fine. When I
pass in an actual value (i.e. 15198), I get the following message:

Invalid length parameter passed to the SUBSTRING function.

There is no SUBSTRING being used anywhere in the query
No, but there is LEFT, which is just a shortcut for SUBSTRING.

More to the point, you have failed to handle the case that the query
does not find any events, and @i is the empty string.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.