declare @sql varchar(1000),
@i int
select @sql=isnull(@sql,'')+other from product_info where id=8
select @i=count(*) from ddl where sort=15
select cou=identity(int,1,1),col1=0 into # from sysobjects a,syscolumns b
select col1=case when charindex(','+cast(cou as varchar)+',',','+@sql+',')>0 then 1 else 0 end
from # where cou<=@i order by cou
drop table #
--创建存储过程
create procedure p_t(@s varchar(2000))
as
begin
declare @sql1 varchar(2000)
declare @sql varchar(2000)
declare @other varchar(20)
declare @index int
declare @max int
set @other = @s+','
select @index = charindex(',',@other)
set @sql= ''
set @sql1= ''
while (@index > 0)
begin
set @sql = @sql + ' union select '+ substring(@other,@index-1,1) +' as [other]'
set @max = convert(int,substring(@other,@index-1,1))
select @index = charindex(',',@other,@index+1)
end
while (@Max > 0)
begin
set @sql1 = @sql1 + ' union select '+ convert(varchar,@max) +' as [other1]'
set @max = @max - 1
end
set @sql ='select other from (' +substring(@sql,7,len(@sql) ) +') a'
set @sql1 ='select other1 from (' +substring(@sql1,7 ,len(@sql1)) +') b'
--exec (@sql)
--exec (@sql1)
--print ('select * from ('+@sql1+')c left join ('+@sql+')d on c.other1 = d.other ')
exec ('select other1,case when other is null then 0 else 1 end as other from ('+@sql1+')c left join ('+@sql+')d on c.other1 = d.other order by other1')
end
go
--调用
p_t '1,3,4'
go
--结果
/*
other1 other
----------- -----------
1 1
2 0
3 1
4 1
修改一下
declare @sql varchar(1000),@i int
select @sql=isnull(@sql,'')+other from product_info where id=8
set @i=reverse(substring(reverse(@sql),1,charindex(',',reverse(@sql))-1))
select cou=identity(int,1,1),col1=0 into # from sysobjects a,syscolumns b
select col1=case when charindex(','+cast(cou as varchar)+',',','+@sql+',')>0 then 1 else 0 end
from # where cou<=@i order by cou
drop table #
declare @sql varchar(1000),@i int
select @sql=isnull(@sql,'')+other from product_info where id=8
set @i=cast(right(@sql,1) as int)
select cou=identity(int,1,1),col1=0 into # from sysobjects a,syscolumns b
select col1=case when charindex(','+cast(cou as varchar)+',',','+@sql+',')>0 then 1 else 0 end
from # where cou<=@i order by cou
drop table #
declare @t table(other int)
declare @TT varchar(1000)
declare @T_SQL varchar(1000)
declare @Max int
declare @l int
declare @i int
set @i=0
set @Max=0
set @T_SQL='1,3,4'
set @TT=@T_SQL
set @l=len(@T_SQL)-len(REPLACE(@T_SQL,',',''))
--print @l
while @i<=@l
begin
if @i=@l
begin
if cast(@T_SQL as int)>@Max
set @Max=cast(@T_SQL as int)
end
else
begin
if cast(left(@T_SQL,charindex(',',@T_SQL)-1) as int)>@Max
set @Max=cast(left(@T_SQL,charindex(',',@T_SQL)-1) as int)
set @T_SQL=right(@T_SQL,len(@T_SQL)-charindex(',',@T_SQL))
end
set @i=@i+1
end
set @i=1
while @i<=@Max
begin
insert @t select @i
set @i=@i+1
end
select case when charindex(cast(other as varchar),@TT)>0 then 1 else 0 end as other from @t
请教名位:
select other from product_info where id=8;
的结果是:
other
1,3,4
怎么样才能得到如下的结果:
other
1 --第1行--
0 --第2行--
1 --第3行--
1 --第4行--
即出现的数字所在行的值为1(真)
需求就是在出现的一串数字中。
以其中select count(*) from ddl where sort=15的结果为记录行数,
并且出现的数字所在的行的值为1
比如出现:1,3,4时且select count(*) from ddl where sort=15的结果为4时
就是第1,3,4行的值为1
其余的行也就是第2行的值为0