1

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.

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

USE <databasename>
exec sp_msforeachtable 'DROP TABLE ?'

3 Comments

It is faster than writing a script yes, but once the script is created, there is nothing faster than just double clicking a script on your desktop.
@djangofan What prevents you from putting these lines into a script?
Just in case you ever need to drop the views too, here is how this works: stackoverflow.com/a/11689661/79485
1

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

10 Comments

Thanks for the answer . I posted my finished script after getting your help.
-1 for being not schema correct (script does not consider table schema, only name) and, far worse, being SQL injection prone
@Remus Rusanu Where would the sql injection be introduced?
The fix is trivial, just use proper quotes, with QUOTENAME(TABLE_SCHEMA) and QUOTENAME(TABLE_NAME).
And as a generic rule: 'I don think someone will exploit this' is never an excuse to leave known defects in the code. Fix it, and then you'll know no one will ever exploit it.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.