«

»

Sep
26

Den Inhalt aller Tabellen einer Microsoft SQL-Datenbank löschen

Mit folgendem TSQL-Script wird der Inhalt aller in der aktuellen Datenbank vorhandenen Benutzer-Tabellen gelöscht:

Sollte TRUNCATE TABLE wg. Referenzen oä. nicht funktionieren kann es durch das etwas langsamere DELETE FROM ersetzt werden.

Bevor man das Script ausführt sollte man mehrfach Prüfen das man in der richtigen Datenbank / auf dem richtigen SQL Server ist.

DECLARE @table varchar(128)

DECLARE CUR CURSOR FOR
SELECT sysobjects.name FROM sysobjects WHERE type = 'U'

OPEN CUR

FETCH NEXT FROM CUR INTO @table

WHILE @@FETCH_STATUS = 0
BEGIN
	EXECUTE ('TRUNCATE TABLE [' + @table + ']')
	FETCH NEXT FROM CUR INTO @table
END

CLOSE CUR
DEALLOCATE CUR

Hinterlasse eine Antwort

Ihre E-Mail-Adresse wird nicht veröffentlicht.

Sie können diese HTML-Tags verwenden: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

What is 2 + 2 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)