sql - Error occurs when a where clause is used with a table valued function? -


i have following table valued udf:

create function [dbo].[funparserestparams](     @params nvarchar(max),     @delim varchar(100))     returns table              return (             select                  ltrim(rtrim(substring([argument], 1, charindex('=', [argument], 1) - 1))) parameter,                 ltrim(rtrim(substring([argument], charindex('=', [argument], 1) + 1, len([argument]) - charindex('=', [argument], 1)))) value             (                           select                      ltrim(rtrim(substring(@params, [number], charindex(@delim, @params + @delim, [number]) - [number]))) [argument]                 (                     select                          row_number() on (order name) [number]                     sys.all_objects) x                 number <= len(@params) , substring(@delim + @params, [number], len(@delim)) = @delim) y);  go 

i'm running in cursor using parameters string reportserver.dbo.executionlogstorage. parameters this: "some%20param%20name=hello&some%20other%20param=noluck"

when run function in cursor this:

select * reports.dbo.funparserestparams(@param, '&') 

everything comes out fine, when add clause:

    select * reports.dbo.funparserestparams(@param, '&') r      r.[parameter] = 'accesstype' 

i following error message every time:

msg 537, level 16, state 3, line 13 invalid length parameter passed left or substring function. 

so why clause kill function?

edit: adding extra. can paste code below sql server replicate error. error occurs when using "first" column, parameter column in clause. using "second" value column in clause not cause error. can remove commenting below see in action:

declare @params nvarchar(max) declare @delim varchar(1)  set @params = 'greeting=hello&name=george&dessert=jello' set @delim = '&'  select * (     select          ltrim(rtrim(substring([argument], 1, charindex('=', [argument], 1) - 1))) parameter,         ltrim(rtrim(substring([argument], charindex('=', [argument], 1) + 1, len([argument]) - charindex('=', [argument], 1)))) value     (                   select              ltrim(rtrim(substring(@params, [number], charindex(@delim, @params + @delim, [number]) - [number]))) [argument]         (             select                  row_number() on (order name) [number]             sys.all_objects) x         [number] <= len(@params) , substring(@delim + @params, [number], len(@delim)) = @delim) y ) r  --where r.parameter = 'accesstype' --where r.parameter = 'greeting' --where r.value = 'bananas' --where r.value = 'jello' 

you may wish consider following:

create function dbo.splitstrings_moden (    @list nvarchar(max),    @delimiter nvarchar(255) ) returns table schemabinding return   e1(n)        ( select 1 union select 1 union select 1 union select 1                           union select 1 union select 1 union select 1                           union select 1 union select 1 union select 1),        e2(n)        (select 1 e1 a, e1 b),        e4(n)        (select 1 e2 a, e2 b),        e42(n)       (select 1 e4 a, e2 b),        ctetally(n)  (select 0 union select top (datalength(isnull(@list,1)))                           row_number() on (order (select null)) e42),        ctestart(n1) (select t.n+1 ctetally t                          (substring(@list,t.n,1) = @delimiter or t.n = 0))   select item = substring(@list, s.n1, isnull(nullif(charindex(@delimiter,@list,s.n1),0)-s.n1,8000))     ctestart s; 

this code attributed jeff moden here. site has other approaches. may worth adopting well-vetted solution (you must vet yourself) rather trying reinvent wheel , having spend inordinate time debugging well.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -