27,580
社区成员
发帖
与我相关
我的任务
分享
你return LEN(@ret) 看看有多長
CREATE function dbo.hebing(@a VARCHAR(20))
returns varchar(8000)
as
begin
declare @ret varchar(8000)
set @ret = ''
select @ret = @ret+case when charindex(b+'/',@ret) > 0 then '' else '/'+rtrim(b) end from table where a= @a
set @ret = stuff(@ret,1,1,'')
return @ret
end
select a,dbo.hebing(a) from table
假如1有200多行,用dbo.hebing(a)查询到的结果是不全的,少了很多,我算了算,没有到8000个字符啊,是为什么呢?create table table1 (a varchar(10),b varchar(10),c int)
insert table1
select 'dh001005','dh001014',10 union
select 'dh001015','dh001024',10 union
select 'dh001025','dh001034',10
create table table2 (a varchar(10),b varchar(10))
insert table2
select 'dh001005','1'
select T1.a,T1.b,T1.c
from table1 T1 left join
table2 T2 on T1.a=T2.a
order by case when T2.b is not null then char(0) else 'a' end
drop table table1,table2
--> 测试数据: @table1
declare @table1 table (a varchar(8),b varchar(8),c int)
insert into @table1
select 'dh001005','dh001014',10 union all
select 'dh001015','dh001024',10 union all
select 'dh001025','dh001034',10 union all
select 'dh001035','dh001044',10 union all
select 'dh001045','dh001054',10 union all
select 'dh001055','dh001064',10 union all
select 'dh001065','dh001074',10 union all
select 'dh001075','dh001084',10 union all
select 'dh001085','dh001094',10 union all
select 'dh001095','dh001104',10 union all
select 'dh001105','dh001114',10
--> 测试数据: @table2
declare @table2 table (a varchar(8))
insert into @table2
select 'dh001056' union all
select 'dh001059' union all
select 'dh001103' union all
select 'dh001099'
select * from @table1 a
order by case when exists(select 1 from @table2 where cast(right(a,len(a)-2) as int) between cast(right(a.a,len(a.a)-2) as int) and cast(right(a.b,len(a.b)-2) as int))
then 0 else 1 end