Avatar billede carnudo Nybegynder
28. august 2001 - 11:13 Der er 4 kommentarer og
1 løsning

Cleaning database

Where can I find a Sql database script to clean the database?
Avatar billede tmceu Praktikant
28. august 2001 - 11:39 #1
I don\'t think such a script exists, but you can run the following statement, which will return all user tables

select name from sysobjects where xtype=\'U\'

you can then copy/paste the DELETE FROM into the results and save it for future use.

You can create a empty copy of your database with the DTS import/export wizard, but I\'m not sure that\'s what you want.

Avatar billede torbenkoch Nybegynder
28. august 2001 - 20:15 #2
Do you want to clear absolute everything? Tables, Procedures, Views, Relationships etc.???

Why don\'t you simply drop and recreate the database??
Avatar billede rasmusmoller Nybegynder
31. august 2001 - 21:42 #3
The following vbscript should do the job:
(tried to make it using T-SQL, but realised it was much faster to do it using vbscript. Hope you don\'t mind)

Just change the constants in the top of the script.

---------------------
\'<script language=vbscript>        -- make VS6 use syntax highligting

Option Explicit

Const gcstrSqlServer = \"(SQLSERVER)\"
Const gcstrSqlDb = \"(DATABASE)\"
Const gcstrSqlUserId = \"sa\"
Const gcstrSqlPwd = \"\"


Sub Main()

    Dim cnn, rstTables

    If MsgBox(\"WARNING: This script will delete ALL data in ALL tables in database \'\" & gcstrSqlDb & \"\' on server \'\" & gcstrSqlServer & \"\'.\" & vbCrLf & _
              \"Continue?\", vbExclamation + vbYesNo + vbDefaultButton2) = vbYes Then

        Set cnn = CreateObject(\"ADODB.Connection\")
        Set rstTables = CreateObject(\"ADODB.Recordset\")

        cnn.Open \"PROVIDER=SQLOLEDB.1;INITIAL CATALOG=\" & gcstrSqlDb & \";DATA SOURCE=\" & gcstrSqlServer & \";USER ID=\" & gcstrSqlUserId & \";PASSWORD=\" & gcstrSqlPwd

        rstTables.Open \"sp_tables @table_type = \"\"\'TABLE\'\"\"\", cnn

        Do While Not rstTables.EOF

            \' execute DELETE statement on table
            cnn.Execute \"DELETE FROM [\" & rstTables(\"TABLE_NAME\") & \"]\"
            rstTables.MoveNext
           
        Loop

        rstTables.Close
        cnn.Close

        Set rstTables = Nothing
        Set cnn = Nothing
       
        MsgBox \"Done.\"
       
    End If

End Sub        \' Main()

Main
------------------------------

Best regards
Rasmus Møller Selsmark
Avatar billede carnudo Nybegynder
28. december 2001 - 10:55 #4
Closing case
I can not use vbscript.
Avatar billede tmceu Praktikant
29. december 2001 - 00:46 #5
I assume that you found that script somewhere ?
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Computerworld tilbyder specialiserede kurser i database-management

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester