id
001
001001
001002
001003
001004
001003001
001004001
001003001001
输入参数是:001003001001
怎么样能得到
001
001003
001003001
001003001001
001
001001
001002
001003
001004
001003001
001004001
001003001001
输入参数是:001003001001
怎么样能得到
001
001003
001003001
001003001001
where '001003001001' like id+'%'
insert @tb
select '001' union
select '001001' union
select '001002' union
select '001003' union
select '001004' union
select '001003001' union
select '001004001' union
select '001003001001'declare @id varchar(50)
set @id='001003001001'declare @tb1 table (id varchar(50))
declare @id_parent varchar(50)set @id_parent=@id
while(len(@id_parent)>0)
begin
insert @tb1 select @id_parent
set @id_parent=left(@id_parent,len(@id_parent)-3)
end
select * from @tb1
order by id
/*
001
001003
001003001
001003001001
*/
insert @tb
select '001' union
select '001001' union
select '001002' union
select '001003' union
select '001004' union
select '001003001' union
select '001004001' union
select '001003001001'declare @id varchar(50)
set @id='001003001001'
select * from @tb
where @id like id+'%'
insert @tb
select '001' union
select '001001' union
select '001002' union
select '001003' union
select '001004' union
select '001003001' union
select '001004001' union
select '001003001001'declare @id varchar(50)
set @id='001003001001'select * from @tb
where @id like id+'%'
where charindex(id,'001003001001')=1