SELECT RP_ID,RP_PARENT_PATH,RP_NAME,RP_PATH_ID,RP_MODIFIED_TIME,DATALENGTH(RP_DATA) AS DATA_LENGTH
FROM (
SELECT * FROM (
SELECT * FROM td.REPOSITORY WHERE RP_PARENT_PATH='0/1/6/683/' AND RP_NAME='Default.xls' AND RP_PATH_ID=0
UNION ALL
SELECT td.REPOSITORY.* FROM (
SELECT td.REPOSITORY.*, RP_PARENT_PATH + convert(varchar,RP_PATH_ID)+'/' AS FILE_PPATH
FROM td.REPOSITORY
WHERE (RP_PARENT_PATH = '0/1/6/683/') AND RP_NAME ='Default.xls' AND RP_PATH_ID <> 0) FOLDER
INNER JOIN td.REPOSITORY ON td.REPOSITORY.RP_PARENT_PATH LIKE FOLDER.FILE_PPATH + '%') D
UNION ALL
SELECT * FROM (
SELECT * FROM td.REPOSITORY
WHERE RP_PARENT_PATH='0/1/6/683/' AND RP_NAME='Test.tsp' AND RP_PATH_ID=0
UNION ALL
SELECT td.REPOSITORY.*
FROM (
SELECT td.REPOSITORY.*, RP_PARENT_PATH + convert(varchar,RP_PATH_ID)+'/' AS FILE_PPATH
FROM td.REPOSITORY
WHERE (RP_PARENT_PATH = '0/1/6/683/') AND RP_NAME ='Test.tsp' AND RP_PATH_ID <> 0) FOLDER
INNER JOIN td.REPOSITORY ON td.REPOSITORY.RP_PARENT_PATH LIKE FOLDER.FILE_PPATH + '%') D
UNION ALL SELECT * FROM (
SELECT * FROM td.REPOSITORY
WHERE RP_PARENT_PATH='0/1/6/683/' AND RP_NAME='Parameters.mtr' AND RP_PATH_ID=0
UNION ALL
SELECT td.REPOSITORY.*
FROM (SELECT td.REPOSITORY.*, RP_PARENT_PATH + convert(varchar,RP_PATH_ID)+'/' AS FILE_PPATH
FROM td.REPOSITORY
WHERE (RP_PARENT_PATH = '0/1/6/683/') AND RP_NAME ='Parameters.mtr' AND RP_PATH_ID <> 0) FOLDER
INNER JOIN td.REPOSITORY ON td.REPOSITORY.RP_PARENT_PATH LIKE FOLDER.FILE_PPATH + '%') D
UNION ALL
SELECT * FROM (
SELECT * FROM td.REPOSITORY
WHERE RP_PARENT_PATH=(SELECT RP_PARENT_PATH + cast(RP_PATH_ID as varchar(255))+ '/'
FROM td.REPOSITORY
WHERE RP_NAME = 'Action0' AND RP_PARENT_PATH = '0/1/6/683/') AND RP_NAME='Resource.mtr' AND RP_PATH_ID=0
UNION ALL
SELECT td.REPOSITORY.*
FROM (SELECT td.REPOSITORY.*, RP_PARENT_PATH + convert(varchar,RP_PATH_ID)+'/' AS FILE_PPATH
FROM td.REPOSITORY
WHERE (RP_PARENT_PATH = (SELECT RP_PARENT_PATH + cast(RP_PATH_ID as varchar(255))+ '/'
FROM td.REPOSITORY
WHERE RP_NAME = 'Action0' AND RP_PARENT_PATH = '0/1/6/683/')) AND RP_NAME ='Resource.mtr' AND RP_PATH_ID <> 0) FOLDER
INNER JOIN td.REPOSITORY ON td.REPOSITORY.RP_PARENT_PATH LIKE FOLDER.FILE_PPATH + '%') D
UNION ALL SELECT * FROM (
SELECT * FROM td.REPOSITORY WHERE RP_PARENT_PATH=(
SELECT RP_PARENT_PATH + cast(RP_PATH_ID as varchar(255))+ '/'
FROM td.REPOSITORY WHERE RP_NAME = 'Action1' AND RP_PARENT_PATH = '0/1/6/683/') AND RP_NAME='Resource.mtr' AND RP_PATH_ID=0
UNION ALL
SELECT td.REPOSITORY.* FROM (
SELECT td.REPOSITORY.*, RP_PARENT_PATH + convert(varchar,RP_PATH_ID)+'/' AS FILE_PPATH
FROM td.REPOSITORY
WHERE (RP_PARENT_PATH = (
SELECT RP_PARENT_PATH + cast(RP_PATH_ID as varchar(255))+ '/'
FROM td.REPOSITORY
WHERE RP_NAME = 'Action1' AND RP_PARENT_PATH = '0/1/6/683/')) AND RP_NAME ='Resource.mtr' AND RP_PATH_ID <> 0) FOLDER INNER JOIN td.REPOSITORY ON td.REPOSITORY.RP_PARENT_PATH LIKE FOLDER.FILE_PPATH + '%') D
UNION ALL
SELECT * FROM (
SELECT * FROM td.REPOSITORY
WHERE RP_PARENT_PATH='0/1/6/683/' AND RP_NAME='default.cfg' AND RP_PATH_ID=0
UNION ALL
SELECT td.REPOSITORY.*
FROM (
SELECT td.REPOSITORY.*, RP_PARENT_PATH + convert(varchar,RP_PATH_ID)+'/' AS FILE_PPATH
FROM td.REPOSITORY
WHERE (RP_PARENT_PATH = '0/1/6/683/') AND RP_NAME ='default.cfg' AND RP_PATH_ID <> 0) FOLDER INNER JOIN td.REPOSITORY ON td.REPOSITORY.RP_PARENT_PATH LIKE FOLDER.FILE_PPATH + '%') D UNION ALL SELECT * FROM (SELECT * FROM td.REPOSITORY WHERE RP_PARENT_PATH='0/1/6/683/' AND RP_NAME='1221.usr' AND RP_PATH_ID=0 UNION ALL SELECT td.REPOSITORY.* FROM (SELECT td.REPOSITORY.*, RP_PARENT_PATH + convert(varchar,RP_PATH_ID)+'/' AS FILE_PPATH FROM td.REPOSITORY WHERE (RP_PARENT_PATH = '0/1/6/683/') AND RP_NAME ='1221.usr' AND RP_PATH_ID <> 0) FOLDER INNER JOIN td.REPOSITORY ON td.REPOSITORY.RP_PARENT_PATH LIKE FOLDER.FILE_PPATH + '%') D) D
ORDER BY RP_PARENT_PATH