delete all tables from the database that start with "HR"

You can delete all tables from the database that start with "HR" using the following SQL script:


 

sql

Copy code

DECLARE @TableName NVARCHAR(128) DECLARE @Sql NVARCHAR(MAX) -- Cursor to iterate over all tables starting with 'HR' DECLARE table_cursor CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME LIKE 'HR%' OPEN table_cursor FETCH NEXT FROM table_cursor INTO @TableName WHILE @@FETCH_STATUS = 0 BEGIN -- Build the DROP TABLE command SET @Sql = 'DROP TABLE [' + @TableName + ']' EXEC sp_executesql @Sql FETCH NEXT FROM table_cursor INTO @TableName END CLOSE table_cursor DEALLOCATE table_cursor

Explanation:

  • Cursor: Iterates over all table names in the database that start with "HR".
  • INFORMATION_SCHEMA.TABLES: This system view provides information about all tables in the database.
  • DROP TABLE: Deletes each table found by the cursor.

Running this script will delete all tables starting with "HR" from your database.

Share This with your friend by choosing any social account


Upcoming Articles
You may also read following recent Post
Copyright Future Minutes © 2015- 2024 All Rights Reserved.   Terms of Service  |   Privacy Policy |  Contact US|  Pages|  Whats new?
Update on: Dec 20 2023 05:10 PM