存储过程的长度参数错误无效

时间:2017-08-14 21:54:20

标签: sql sql-server reporting-services substring ssrs-2012

我将SSRS中的名称列表传递给SQL Server存储过程但是我收到错误:

  

传递给LEFT或SUBSTRING函数的长度参数无效

这是我的代码:

create table tablename as
select '4FH6V1' as ComputerName, 'AZ1' as Location, '3093' as Rating from dual union all
select '0GLYQ1' as ComputerName, 'AZ1' as Location, '3093' as Rating from dual union all
select 'P191R1' as ComputerName, 'AZ1' as Location, '3093' as Rating from dual union all
select '7CMJ02' as ComputerName, 'AZ3' as Location, '3392' as Rating from dual union all
select '8W2QS1' as ComputerName, 'AZ4' as Location, '3093' as Rating from dual union all
select 'K9CHX1' as ComputerName, 'AZ7' as Location, '3192' as Rating from dual union all
select '3XZNS1' as ComputerName, 'AZ7' as Location, '3093' as Rating from dual union all
select '79RGX1' as ComputerName, 'AZ9' as Location, '3192' as Rating from dual union all
select '02BR22' as ComputerName, 'AZ3' as Location, '2593' as Rating from dual
;

| ComputerName | Location | Rating |
|--------------|----------|--------|
| 4FH6V1       | AZ1      | 3093   |
| 0GLYQ1       | AZ1      | 3093   |
| P191R1       | AZ1      | 3093   |
| 7CMJ02       | AZ3      | 3392   |
| 8W2QS1       | AZ4      | 3093   |
| K9CHX1       | AZ7      | 3192   |
| 3XZNS1       | AZ7      | 3093   |
| 79RGX1       | AZ9      | 3192   |
| 02BR22       | AZ3      | 2593   |

子字符串的原因是删除附加到管理器名称末尾的36个字符的GUID。名称传递如下:

Select substring(item, 1, LEN(item) - 36) 
From dbo.fnsplit(@manager, ',')

我已经读过错误可能是由空格引起的,但我无法弄清楚如何解决这个问题。

2 个答案:

答案 0 :(得分:2)

只需使用res

case

答案 1 :(得分:0)

如果你真的想在所有情况下从右侧删除最后36个字符!?所以,我认为这个hack-version应该可以工作;)。

select 
    replace(item, right(item, 36), '')
from 
    dbo.fnsplit(@manager, ',');
  

注意:实际上我从文本右侧取36个字符,然后替换为'';)将其删除。

[SQL Fiddle Demo]

但我认为你的问题应该是这样的:

select 
    left(item, abs(len(item + ',') - 37))
from
    dbo.fnsplit(@manager, ',');

[SQL Fiddle Demo]