If you want to perform bulk insert operation on multiple tables. The tables have foreign key relationships between themselves. If an INSERT operation is done on a table with a foreign key before the referenced table is being inserted to, the operation might fail due to violation of the foreign key.Requirement
Produce a list of tables within a database ordered according to their dependencies. Tables with no dependencies (no foreign keys) will be 1st. Tables with dependencies only in the 1st set of tables will be 2nd. Tables with dependencies only in the 1st or 2nd sets of tables will be 3rd. and so on...WITH cte ( lvl ,object_id ,name ,schema_Name ) AS ( SELECT 1 ,object_id ,sys.tables.name ,sys.schemas.name AS schema_Name FROM sys.tables INNER JOIN sys.schemas ON sys.tables.schema_id = sys.schemas.schema_id WHERE type_desc = 'USER_TABLE' AND is_ms_shipped = 0 UNION ALL SELECT cte.lvl + 1 ,t.object_id ,t.name ,S.name AS schema_Name FROM cte JOIN sys.tables AS t ON EXISTS ( SELECT NULL FROM sys.foreign_keys AS fk WHERE fk.parent_object_id = t.object_id AND fk.referenced_object_id = cte.object_id ) JOIN sys.schemas AS S ON t.schema_id = S.schema_id AND t.object_id <> cte.object_id AND cte.lvl < 30 WHERE t.type_desc = 'USER_TABLE' AND t.is_ms_shipped = 0 ) SELECT schema_Name ,name ,MAX(lvl) AS dependency_level FROM cte WHERE schema_Name LIKE '%ORCA%' GROUP BY schema_Name ,name ORDER BY dependency_level ,schema_Name ,name; Reference
Showing posts with label tables. Show all posts
Showing posts with label tables. Show all posts
Tuesday, February 25, 2020
List tables on their dependency order based on foreign keys for Data Migration
Tuesday, January 24, 2017
Query to List all the partitioned tables in SQL Server database
select object_schema_name(i.object_id) as [schema],
object_name(i.object_id) as [object],
i.name as [index],
s.name as [partition_scheme]
from sys.indexes i
join sys.partition_schemes s on i.data_space_id = s.data_space_id order by [schema]
object_name(i.object_id) as [object],
i.name as [index],
s.name as [partition_scheme]
from sys.indexes i
join sys.partition_schemes s on i.data_space_id = s.data_space_id order by [schema]
Wednesday, November 16, 2016
Delete all Tables based on creation date
--Delete all Tables based on creation date
DECLARE @tname VARCHAR(100)
DECLARE @sql VARCHAR(max)
DECLARE db_cursor CURSOR FOR
SELECT name AS tname
FROM sys.objects
WHERE create_date < GETDATE() - 1-- Days old
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @tname
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = 'DROP TABLE ' + @tname
--EXEC (@sql) -- For Executing
PRINT @sql --For Printing
FETCH NEXT FROM db_cursor INTO @tname
END
CLOSE db_cursor
DEALLOCATE db_cursor
Friday, November 4, 2016
Simplest way to Truncate/Delete all tables in a given database.
Simplest way to Truncate all tables in a given database.
Use databasename
EXEC sp_MSForEachTable 'Truncate TABLE ?' --- For Truncating all tables
Simplest way to Truncate all tables in a given database.
Use databasename
EXEC sp_MSforeachtable @command1
= "DROP TABLE ?" --- For Deleting all tables
Wednesday, September 24, 2014
Script to generate drop all tables statements in a database in SQL Server
SELECT 'Drop table ' + NAME FROM sys.objects WHERE schema_name(schema_id) = 'dbo' AND type = 'u'
Tuesday, September 23, 2014
Script to find number of rows in each partition in a partitioned table in SQL Server
SELECT t.NAME [table] ,p.rows ,p.partition_number ,v.boundary_id ,v.value FROM sys.tables t INNER JOIN sys.partitions p ON p.object_id = t.object_id INNER JOIN sys.partition_range_values v ON v.boundary_id = p.partition_number WHERE is_ms_shipped = 0 ORDER BY [table]
Monday, September 22, 2014
Fastest way to row count all tables in a Database SQL Server
--Full Database SELECT QUOTENAME(SCHEMA_NAME(sOBJ.schema_id)) + '.' + QUOTENAME(sOBJ.NAME) AS [TableName] ,SUM(sPTN.Rows) AS [RowCount] FROM sys.objects AS sOBJ INNER JOIN sys.partitions AS sPTN ON sOBJ.object_id = sPTN.object_id WHERE sOBJ.type = 'U' AND sOBJ.is_ms_shipped = 0x0 AND index_id < 2 -- 0:Heap, 1:Clustered GROUP BY sOBJ.schema_id ,sOBJ.NAME ORDER BY [TableName] GO ----------------------------------------------------------- -- For Individual Schema** SELECT QUOTENAME(SCHEMA_NAME(sOBJ.schema_id)) + '.' + QUOTENAME(sOBJ.NAME) AS [TableName] ,SUM(sPTN.Rows) AS [RowCount] FROM sys.objects AS sOBJ INNER JOIN sys.partitions AS sPTN ON sOBJ.object_id = sPTN.object_id WHERE sOBJ.type = 'U' AND sOBJ.is_ms_shipped = 0x0 AND index_id < 2 -- 0:Heap, 1:Clustered AND QUOTENAME(SCHEMA_NAME(sOBJ.schema_id)) = '[YOURSCHEMANAMEHERE]' GROUP BY sOBJ.schema_id ,sOBJ.NAME ORDER BY [TableName] GO
Subscribe to:
Posts (Atom)