34,593
社区成员
发帖
与我相关
我的任务
分享
select * from tb where (type='D' and filepath=@filepath) or (type='f' and len(filepath)-len(replace(filepath,'\',''))=len(@filepath)-len(replace(@filepath,'\','')))
DECLARE @a table(id int, filepath varchar(100), type varchar(20))
INSERT @a SELECT 1 ,'D:\test\' ,'D'
UNION ALL SELECT 2 ,'D:\test\a.txt' ,'f'
UNION ALL SELECT 2 ,'D:\test\b.txt' ,'f'
UNION ALL SELECT 2 ,'D:\test\c.txt' ,'f'
UNION ALL SELECT 2 ,'D:\test\subtest1' ,'D'
UNION ALL SELECT 2 ,'D:\test\subtest2' ,'D'
UNION ALL SELECT 2 ,'D:\test\subtest1\dfe.txt' ,'f'
UNION ALL SELECT 2 ,'D:\test\subtest2\fewf.txt' ,'f'
DECLARE @t varchar(100)
SET @t='D:\test'
SET @t=CASE WHEN RIGHT(@t,1)='\' THEN @t ELSE @t+'\' END
SELECT * FROM @a
WHERE
charindex('\',replace(
CASE WHEN RIGHT(filepath,1)='\' THEN filepath ELSE
CASE WHEN charindex('.',filepath)>0 THEN filepath
ELSE filepath+'\' END END ,@t,''))=0
--result
/*id filepath type
----------- ------------------------------ --------------------
1 D:\test\ D
2 D:\test\a.txt f
2 D:\test\b.txt f
2 D:\test\c.txt f
(所影响的行数为 4 行)
*/
declare @t table (id int,filepath varchar(100),type varchar(2))
insert into @t
select
1, 'D:\test\', 'D'
union all select
2, 'D:\test\a.txt', 'f'
union all select
2, 'D:\test\b.txt', 'f'
union all select
2, 'D:\test\c.txt', 'f'
union all select
2, 'D:\test\subtest1', 'D'
union all select
2, 'D:\test\subtest2', 'D'
union all select
2, 'D:\test\subtest1\dfe.txt' , 'f'
union all select
2, 'D:\test\subtest2\fewf.txt', 'f'
declare @s varchar(100)
set @s='D:\test\subtest2'
select *
from @t
where (type='D' AND (filepath =@s OR filepath=@s+'\'))
or
(
type='f'
and
charindex('\',replace(filepath,@s+'\',''))=0
)
IF OBJECT_ID('TB') IS NOT NULL DROP TABLE TB
CREATE TABLE TB(ID INT,FILEPATH VARCHAR(50),TYPE VARCHAR(2))
INSERT INTO TB SELECT 1,'D:\test\','D'
UNION ALL SELECT 2,'D:\test\a.txt','f'
UNION ALL SELECT 2,'D:\test\b.txt','f'
UNION ALL SELECT 2,'D:\test\c.txt','f'
UNION ALL SELECT 2,'D:\test\subtest1','D'
UNION ALL SELECT 2,'D:\test\subtest2','D'
UNION ALL SELECT 2,'D:\test\subtest1\dfe.txt','f'
UNION ALL SELECT 2,'D:\test\subtest2\fewf.txt','f'
DECLARE @A VARCHAR(50)
SET @A='D:\test\'
SELECT * FROM TB WHERE FILEPATH = @A AND TYPE='D'
UNION ALL
SELECT * FROM TB WHERE FILEPATH LIKE @A+'%' AND (LEN(REPLACE(FILEPATH,'\',''))=LEN(FILEPATH)-2) AND TYPE='F'
/*
ID FILEPATH TYPE
----------- -------------------------------------------------- ----
1 D:\test\ D
2 D:\test\a.txt f
2 D:\test\b.txt f
2 D:\test\c.txt f
(所影响的行数为 4 行)
*/
if object_id('tb') is not null drop table tb
create table tb(id int,filepath varchar(100),type varchar(100))
insert into tb
select 1,'D:\test\','D' union all
select 2,'D:\test\a.txt','f' union all
select 2,'D:\test\b.txt','f' union all
select 2,'D:\test\c.txt','f' union all
select 2,'D:\test\subtest1','D' union all
select 2,'D:\test\subtest2','D' union all
select 2,'D:\test\subtest1\dfe.txt','f' union all
select 2,'D:\test\subtest2\fewf.txt','f'
go
select * from tb where filepath like 'D:\test\%' and (len(filepath)-len(replace(filepath,'\','')))=2
(所影响的行数为 8 行)
id filepath type
----------- ---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
1 D:\test\ D
2 D:\test\a.txt f
2 D:\test\b.txt f
2 D:\test\c.txt f
2 D:\test\subtest1 D
2 D:\test\subtest2 D
(所影响的行数为 6 行)
declare @filepath varchar(999)
set @filepath='D:\test\ '--待查询的目录
select * from FileList where filepath=@filepath --查目录
union
select * from FileList where filepath like @filepath+'%' and type = 'f' --查目录下的文件
declare @filepath varchar(999)
set @filepath='D:\test\ '--待查询的目录
select * from FileList where filepath like @filepath+'%'
if object_id('tb') is not null drop table tb
create table tb(id int,filepath varchar(30),type varchar(10))
insert into tb
select 1,'D:\test\','D' union all
select 2,'D:\test\a.txt','f' union all
select 2,'D:\test\b.txt','f' union all
select 2,'D:\test\c.txt','f' union all
select 2,'D:\test\subtest1','D' union all
select 2,'D:\test\subtest2','D' union all
select 2,'D:\test\subtest1\dfe.txt','f' union all
select 2,'D:\test\subtest2\fewf.txt','f'
go
select * from tb where filepath like 'D:\test\_%' and (len(filepath)-len(replace(filepath,'\','')))=2 order by type
(所影响的行数为 8 行)
id filepath type
----------- ------------------------------ ----------
2 D:\test\subtest1 D
2 D:\test\subtest2 D
2 D:\test\a.txt f
2 D:\test\b.txt f
2 D:\test\c.txt f
(所影响的行数为 5 行)
select * from tb where filepath like 'D:\test\_%' and (len(filepath)-len(replace(filepath,'\','')))=2