Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Drop & Recreate the DB

Drop & Recreate the DB

Scheduled Pinned Locked Moved Database
databasesql-servertoolshelpquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I'm starting a new script. This is local on my machine and I'm the only user:

    -- IF THE DATABASE EXISTS, DROP AND RE-CREATE IT
    IF EXISTS (SELECT Name FROM master.dbo.sysdatabases WHERE name = N'Dashboard')
    BEGIN
    ALTER DATABASE Dashboard SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    DROP DATABASE Dashboard
    END
    CREATE DATABASE Dashboard

    -- CREATE THE TABLES
    GO
    USE Dashboard
    CREATE TABLE DashboardInfo
    (Id INT PRIMARY KEY NOT NULL,
    SiteId INT NOT NULL,
    InstrumentId INT NOT NULL,
    TowerLocation INT NOT NULL)

    The first time I run it all works fine. Any time I run it after that I get

    Msg 3702, Level 16, State 3, Line 5
    Cannot drop database "Dashboard" because it is currently in use.
    Msg 1801, Level 16, State 3, Line 7
    Database 'Dashboard' already exists. Choose a different database name.
    Msg 2714, Level 16, State 6, Line 12
    There is already an object named 'DashboardInfo' in the database.

    If I close and re-open SSMS then it works ok again. WTF is wrong here??????????

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    C S 2 Replies Last reply
    0
    • K Kevin Marois

      I'm starting a new script. This is local on my machine and I'm the only user:

      -- IF THE DATABASE EXISTS, DROP AND RE-CREATE IT
      IF EXISTS (SELECT Name FROM master.dbo.sysdatabases WHERE name = N'Dashboard')
      BEGIN
      ALTER DATABASE Dashboard SET SINGLE_USER WITH ROLLBACK IMMEDIATE
      DROP DATABASE Dashboard
      END
      CREATE DATABASE Dashboard

      -- CREATE THE TABLES
      GO
      USE Dashboard
      CREATE TABLE DashboardInfo
      (Id INT PRIMARY KEY NOT NULL,
      SiteId INT NOT NULL,
      InstrumentId INT NOT NULL,
      TowerLocation INT NOT NULL)

      The first time I run it all works fine. Any time I run it after that I get

      Msg 3702, Level 16, State 3, Line 5
      Cannot drop database "Dashboard" because it is currently in use.
      Msg 1801, Level 16, State 3, Line 7
      Database 'Dashboard' already exists. Choose a different database name.
      Msg 2714, Level 16, State 6, Line 12
      There is already an object named 'DashboardInfo' in the database.

      If I close and re-open SSMS then it works ok again. WTF is wrong here??????????

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      C Offline
      C Offline
      Chris Quinn
      wrote on last edited by
      #2

      Try putting USE [Master] at the top of the script

      ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

      K 1 Reply Last reply
      0
      • C Chris Quinn

        Try putting USE [Master] at the top of the script

        ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        I did. No change

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        L 1 Reply Last reply
        0
        • K Kevin Marois

          I did. No change

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          What about that ALTER statement on line 4?

          K 1 Reply Last reply
          0
          • L Lost User

            What about that ALTER statement on line 4?

            K Offline
            K Offline
            Kevin Marois
            wrote on last edited by
            #5

            What about it?

            If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

            1 Reply Last reply
            0
            • K Kevin Marois

              I'm starting a new script. This is local on my machine and I'm the only user:

              -- IF THE DATABASE EXISTS, DROP AND RE-CREATE IT
              IF EXISTS (SELECT Name FROM master.dbo.sysdatabases WHERE name = N'Dashboard')
              BEGIN
              ALTER DATABASE Dashboard SET SINGLE_USER WITH ROLLBACK IMMEDIATE
              DROP DATABASE Dashboard
              END
              CREATE DATABASE Dashboard

              -- CREATE THE TABLES
              GO
              USE Dashboard
              CREATE TABLE DashboardInfo
              (Id INT PRIMARY KEY NOT NULL,
              SiteId INT NOT NULL,
              InstrumentId INT NOT NULL,
              TowerLocation INT NOT NULL)

              The first time I run it all works fine. Any time I run it after that I get

              Msg 3702, Level 16, State 3, Line 5
              Cannot drop database "Dashboard" because it is currently in use.
              Msg 1801, Level 16, State 3, Line 7
              Database 'Dashboard' already exists. Choose a different database name.
              Msg 2714, Level 16, State 6, Line 12
              There is already an object named 'DashboardInfo' in the database.

              If I close and re-open SSMS then it works ok again. WTF is wrong here??????????

              If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

              S Offline
              S Offline
              Simon_Whale
              wrote on last edited by
              #6

              because of the use Dashboard line, it creates a connection to the database in question.

              if exists (select name from sys.databases where name='YourDBName')
              alter database YourDBName set single_user with rollback immediate
              go
              if exists (select name from sys.databases where name='YourDBName')
              drop database YourDBName

              Dropping and recreating databases in Microsoft SQL Server - Stack Overflow[^]

              Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

              K 1 Reply Last reply
              0
              • S Simon_Whale

                because of the use Dashboard line, it creates a connection to the database in question.

                if exists (select name from sys.databases where name='YourDBName')
                alter database YourDBName set single_user with rollback immediate
                go
                if exists (select name from sys.databases where name='YourDBName')
                drop database YourDBName

                Dropping and recreating databases in Microsoft SQL Server - Stack Overflow[^]

                Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

                K Offline
                K Offline
                Kevin Marois
                wrote on last edited by
                #7

                Thanks. That helped... Working fine now

                If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups