日期:2014-05-17 浏览次数:20521 次
BEGIN
DECLARE @SQL AS nvarchar(max)
SET @SQL =
'WITH RecursiveCte AS
(
SELECT 1 as Level, H1.Id, H1.ParentId FROM ' + @TableName + ' H1
WHERE id = ' + CAST(@Id as Nvarchar(max)) + '
UNION ALL
SELECT RCTE.level + 1 as Level, H2.Id, H2.ParentId FROM ' + @TableName + ' H2
INNER JOIN RecursiveCte RCTE ON H2.ParentId = RCTE.Id
)
SELECT id
INTO #tmpTable
FROM RecursiveCte
if '+ @TableName +'=object_id(''数据库中指定的表名'',''U'')--这个判断条件该怎么写?
begin
---进行其他操作
end
delete from ' + @TableName + ' where id in( SELECT id
FROM #tmpTable)
'
EXEC sp_executesql @SQL;
END