语句如下:
Declare @OrderNoList Varchar(500)
Set @OrderNoList='''136987A'''
Declare @Sqlx Varchar(8000)
Set @Sqlx='select top 10
SubString(b.cust_so_ref,1,Charindex(''-'',b.cust_so_ref)-1) OrderNo,
ISNULL(RTRIM(c.description), '''') AS CheckPointDesc,Sum(a.qty) Qty
from
Arfid_transaction_table a inner join Asp_ord_master b on b.key_seq=a.jo_key_seq and b.status in (1, 2)
inner join Ajo_step c on c.link_key_seq=a.jo_key_seq and c.major_step=a.major and c.component=a.component
inner join Aemployee d on a.employee_id=d.emp_no
inner join Ajo_finished_goods e on e.key_seq=a.jo_sku_key_seq
where
a.major=''276'' and SubString(b.cust_so_ref,1,Charindex(''-'',b.cust_so_ref)-1) in ('+@OrderNoList+')
Group by
b.cust_so_ref,c.description'
Exec(@Sqlx)
报错:
Invalid length parameter passed to the substring function
如果把条件里面的substring这一段去掉就会通过,语句如下:
Declare @OrderNoList Varchar(500)
Set @OrderNoList='''136987A'''
Declare @Sqlx Varchar(8000)
Set @Sqlx='select top 10
SubString(b.cust_so_ref,1,Charindex(''-'',b.cust_so_ref)-1) OrderNo,
ISNULL(RTRIM(c.description), '''') AS CheckPointDesc,Sum(a.qty) Qty
from
Arfid_transaction_table a inner join Asp_ord_master b on b.key_seq=a.jo_key_seq and b.status in (1, 2)
inner join Ajo_step c on c.link_key_seq=a.jo_key_seq and c.major_step=a.major and c.component=a.component
inner join Aemployee d on a.employee_id=d.emp_no
inner join Ajo_finished_goods e on e.key_seq=a.jo_sku_key_seq
where
a.major=''276''
Group by
b.cust_so_ref,c.description'
Exec(@Sqlx)
执行之后结果如下:
OrderNo CheckPointDesc Qty
126445A 發成品釘珠-后整 701
126583A 發成品釘珠-后整 2708
126584A 發成品釘珠-后整 1754
126588A 發成品釘珠-后整 5144
137058T1 發成品釘珠-后整 17886
因为表里面的OrderNo字段内容其实是这样:
OrderNo
126445A-FGCH-1
126583A-FGCH-1
137058T1-HJ-1
因为在传递的参数不会带有减号后面的东东,所以一定要截取字符串,请问大家有没有好方法?
使用环境是SQL2000
------解决方案--------------------
两处的(SELECT 中的及WHERE 中的)
SubString(b.cust_so_ref,1,Charindex(''-'',b.cust_so_ref)-1)
改为
CASE WHEN Charindex(''-'',b.cust_so_ref)>0 THEN SubString(b.cust_so_ref,1,Charindex(''-'',b.cust_so_ref)-1) ELSE b.cust_so_ref END
------解决方案--------------------
OrderNo 字段是不是都有'-'号的?
------解决方案--------------------
这样测试下呢?
SubString(b.cust_so_ref,1,Charindex(''-'',b.cust_so_ref)-1)=''136987A'',
第二句set语句需要'''吗?