Results 1 to 7 of 7

Thread: [RESOLVED] [sql 2k5] DROP TSQL

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Resolved [RESOLVED] [sql 2k5] DROP TSQL

    a simple question how to drop a database with the use of IF stmt to check if the object exist.

    if object_id('dbo.myDB') is not null
    drop database 'myDB'
    go

    the code returns no error but it also wont delete the database("myDB")

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [sql 2k5] DROP TSQL

    You DROP tables, not databases.

    To remove a database from disk use DELETE DATABASE dbname

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: [sql 2k5] DROP TSQL

    hi hack there was an error return when i execute the script


  4. #4
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [sql 2k5] DROP TSQL

    Try this:

    vb Code:
    1. USE [master]
    2. GO
    3.  
    4. IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'DBName')
    5. DROP DATABASE [DBName]

    Edit: All I did was right click on the Database ---> Script Database as ---> Drop To ---> Clipboard.
    Last edited by Mark Gambo; Nov 21st, 2007 at 08:22 AM. Reason: Added data
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: [sql 2k5] DROP TSQL

    why im getting this error:

    Msg 3702, Level 16, State 4, Line 4
    Cannot drop database "DBname" because it is currently in use.

    the database was not in use/design mode/open by any application

  6. #6
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [sql 2k5] DROP TSQL

    Quote Originally Posted by jlbantang
    why im getting this error:

    Msg 3702, Level 16, State 4, Line 4
    Cannot drop database "DBname" because it is currently in use.

    the database was not in use/design mode/open by any application

    Check the Activity Monitor and make sure that there aren't any connections to the database. If not, then depending upon your setup you may have to stop and then restart SQL Server.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: [sql 2k5] DROP TSQL

    sweet!

    thnx!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width