xxxxxxxxxx
-- Drop all tables from a database
USE your_database_name; -- Replace "your_database_name" with the actual name of the database you want to delete tables from
DECLARE @sql NVARCHAR(MAX) = N'';
SELECT @sql += N'DROP TABLE ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) + ';
'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE';
EXEC(@sql);
xxxxxxxxxx
EXEC sp_MSForEachTable 'DISABLE TRIGGER ALL ON ?'
GO
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable 'SET QUOTED_IDENTIFIER ON; DELETE FROM ?'
GO
EXEC sp_MSForEachTable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable 'ENABLE TRIGGER ALL ON ?'
GO
xxxxxxxxxx
-- To drop all tables in a database, do this:
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
-- If all of your tables are in a single schema, this approach could work
-- (assumes that the name of your schema is public)
xxxxxxxxxx
USE Databasename
SELECT 'DROP TABLE [' + name + '];'
FROM sys.tables
xxxxxxxxxx
-- Generate drop table commands for each table in the database
SELECT 'DROP TABLE ' || table_name || ';' AS drop_table_command
FROM information_schema.tables
WHERE table_schema = 'public' -- Specify the schema name if needed
AND table_type = 'BASE TABLE';
-- Execute the generated drop table commands to drop all tables
DO $$ DECLARE
drop_table_command text;
BEGIN
FOR drop_table_command IN (
SELECT 'DROP TABLE ' || table_name || ';' AS drop_table_command
FROM information_schema.tables
WHERE table_schema = 'public' -- Specify the schema name if needed
AND table_type = 'BASE TABLE'
)
LOOP
EXECUTE drop_table_command;
END LOOP;
END $$;
xxxxxxxxxx
DECLARE @sql NVARCHAR(max)=''
SELECT @sql += ' Drop table ' + QUOTENAME(TABLE_SCHEMA) + '.'+ QUOTENAME(TABLE_NAME) + '; '
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
Exec Sp_executesql @sql
xxxxxxxxxx
DO $$
DECLARE
v_table_name text;
BEGIN
FOR v_table_name IN (SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE')
LOOP
EXECUTE 'DROP TABLE IF EXISTS ' || v_table_name || ' CASCADE';
END LOOP;
END $$;
xxxxxxxxxx
BY LOVE
EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"
DECLARE @sql NVARCHAR(max)=''
SELECT @sql += ' Drop table ' + QUOTENAME(TABLE_SCHEMA) + '.'+ QUOTENAME(TABLE_NAME) + '; '
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
Exec Sp_executesql @sql