glgcag1991
Programmer
I'm trying to build a stored procedure that I can reuse when I'm building and testing databases. Often I want to drop all tables in a database that I specify but I'm not sure of the syntax. Here's my current proc:
If I add a parameter to this sp for a database name, I would assume the database name would go in this part of the proc "SELECT TABLE NAME FROM sys.tables", but I'm not sure how to get to the databases- is it sys.databases."name".tables or something like it?
Thanks for the help!
Code:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_DropTables]
AS
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE @TableName varchar(800);
DECLARE @strSQL varchar(8000);
DECLARE c1 CURSOR FOR
SELECT TABLE_NAME
FROM sys.tables
FOR READ ONLY
OPEN c1
FETCH NEXT FROM c1
INTO @TableName
-- loop until we run out of tables
WHILE @@FETCH_STATUS = 0
IF LEFT (@TableName, 1) != 'V'
BEGIN
SET @strSQL = 'DROP TABLE ' + @TableName
EXECUTE (@strSQL)
END
FETCH NEXT FROM c1
INTO @TableName
CLOSE c1
DEALLOCATE c1
END
If I add a parameter to this sp for a database name, I would assume the database name would go in this part of the proc "SELECT TABLE NAME FROM sys.tables", but I'm not sure how to get to the databases- is it sys.databases."name".tables or something like it?
Thanks for the help!