Search the Blog

Friday, August 27, 2021

SQl Server Query to delete the All Object execpt the specific schema

 USE Temp_data


DECLARE @Procedure_name NVARCHAR(MAX)

DECLARE @QUERY NVARCHAR(MAX)

DECLARE @Table_name NVARCHAR(MAX)


SELECT  CONCAT(a.name,'.',b.name)  as proc_name into #Temp_proc_name FROM SYS.schemas  A

INNER JOIN SYS.procedures B ON A.schema_id = B.schema_id

WHERE A.name != 'dbo'


WHILE EXISTS ( SELECT 1 FROM #Temp_proc_name )

BEGIN

SET @Procedure_name =  (SELECT top 1 proc_name from #Temp_proc_name order by proc_name desc)

SET @QUERY = N'DROP PROCEDURE ' + @Procedure_name;

EXECUTE sp_executesql @QUERY


DELETE FROM #Temp_proc_name WHERE proc_name = @Procedure_name

END



SELECT  CONCAT(a.name,'.',b.name)  as table_name into #Temp_table_name FROM SYS.schemas  A

INNER JOIN SYS.tables B ON A.schema_id = B.schema_id

WHERE A.name != 'dbo'


WHILE EXISTS ( SELECT 1 FROM #Temp_table_name )

BEGIN

SET @Table_name =  (SELECT top 1 table_name from #Temp_table_name order by table_name desc)

SET @QUERY = N'DROP TABLE ' + @Table_name;

EXECUTE sp_executesql @QUERY


DELETE FROM #Temp_table_name WHERE table_name = @Table_name

END

DROP TABLE #Temp_table_name

DROP TABLE #Temp_proc_name






No comments:

Post a Comment

Translate