简体   繁体   中英

Drop all tables in a SQL Server database from Batch file?

I have this batch file that outputs a list of all the tables to a file. I am looking for the command that will input this list and generate a whole list of drop statements to drop all the tables with?

:: droptables.bat
set SQLVER=100
if NOT EXIST "%PROGRAMFILES%\Microsoft SQL Server\100\Tools\BINN\osql.exe" (
  @echo MS SQL Server 2008 not found.
  set SQLVER=90
  if NOT EXIST "%PROGRAMFILES%\Microsoft SQL Server\90\Tools\BINN\osql.exe" (
    @echo MS SQL Server 2005 not found.
    set SQLVER=80
    if NOT EXIST "%PROGRAMFILES%\Microsoft SQL Server\80\Tools\BINN\osql.exe" (
      @echo MS SQL Server is not yet installed.
      pause
      exit
    )
  )
)
@echo Your SQL Server version is %SQLVER% (100=2008,90=2005, and 80=2000)    
if exist "%PROGRAMFILES%\Microsoft SQL Server\%SQLVER%\Tools\BINN\osql.exe" (
  "%PROGRAMFILES%\Microsoft SQL Server\%SQLVER%\Tools\BINN\osql" -E 
  -d "%PROJECT%PD_FSDB_ECitation" -h-1 -Q "select name from sysobjects where 
  type='U' order by name;" -o tableList.txt

The above query needs to be changed to create a list of drop statements instead of just table names. The tableList.sql file is just a simple list of drop table statements.

After generating the queryList.sql, then I want to run it like so:

osql -E -h-1 -i C:\MyFolder\queryList.txt

I know there is a way to generate a list of SQL statements from a SQL statement but I don't remember how to do it.

Why don't use just use the system stored proc sp_msforeachtable ?

Run the following from your osql and you can bypass a lot of the extra work you are doing:

USE <databasename>
exec sp_msforeachtable 'DROP TABLE ?'

This proc basically builds a cursor and executes the query inside the single quotes once for each table, replacing ? with the schema-qualified table name, like dbo.table .

Run the following from your SQL. It is simple and fast to delete tables from your DB:

USE <databasename>
exec sp_msforeachtable 'DROP TABLE ?'

I don't know if there is a command to do it but you can change your select statement so that it creates the drop statement for you:

select '--DROP TABLE ' + name + CHAR(10) + 'DROP TABLE ' + name + ' GO' from sysobjects where type='U' 

EDIT: After comment with regards to schema not specified:

SELECT '--DROP TABLE ' + TABLE_NAME + CHAR(10) + 'DROP TABLE ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)  
FROM INFORMATION_SCHEMA.TABLES 

EDIT: As Remus suggested in the comments, added quotename to make it less vulnerable to sql injection

Here is how I did it:

set SQLVER=100
if NOT EXIST "%PROGRAMFILES%\Microsoft SQL Server\100\Tools\BINN\osql.exe" (
  @echo MS SQL Server 2008 not found.
  set SQLVER=90
  if NOT EXIST "%PROGRAMFILES%\Microsoft SQL Server\90\Tools\BINN\osql.exe" (
    @echo MS SQL Server 2005 not found.
    set SQLVER=80
    if NOT EXIST "%PROGRAMFILES%\Microsoft SQL Server\80\Tools\BINN\osql.exe" (
      @echo MS SQL Server is not yet installed.
      pause
      exit
    )
  )
)
@echo Your SQL Server version is %SQLVER% (100=2008,90=2005, and 80=2000)

if exist "%PROGRAMFILES%\Microsoft SQL Server\%SQLVER%\Tools\BINN\osql.exe" (
  "%PROGRAMFILES%\Microsoft SQL Server\%SQLVER%\Tools\BINN\osql" -E -h-1 
 -d MYDB -Q "select 'DROP TABLE ' + CAST(name AS 
 VARCHAR(30)) + ';' from sysobjects where type='U';" -o queries.sql
  :: remove sql result count line
  type queries.sql | findstr /V rows > queryList.sql  
  :: rename Identity table in the sql script because it uses a keyword
  type queryList.sql | findstr /V /C:"TABLE Identity" > runQueries.sql
  echo DROP TABLE [Identity]; >> runQueries.sql
  "%PROGRAMFILES%\Microsoft SQL Server\%SQLVER%\Tools\BINN\osql" -E -h-1 
 -d MYDB -i runQueries.sql -o dropResults1.txt
)
pause
:: Cleanup
del queryList.sql
del dropResults1.txt
del dropResults2.txt
del runQueries.sql
del queries.sql
exit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM