Getting SQL Errors that should not occur because the database was dropped before reinstallation

Mark McCumber 431 Reputation points
2023-02-27T15:17:01.1833333+00:00

I have modified my SQL script to modify specific tables, but I am getting the following errors.

Beginning InstUS_Oligarchs.SQL at 27 Feb 2023 09:02:52:830 ....

Msg 50000, Level 1, State 1

Dropping existing US_Oligarchs database ....

Creating US_Oligarchs database....

Now at the create table section ....

Creating tblStatus ....

Msg 2714, Level 16, State 5, Line 94

There is already an object named 'tblStatus' in the database.

Msg 1750, Level 16, State 1, Line 94

Could not create constraint or index. See previous errors.

Inserting default data into tblCand_Status ....

Msg 1088, Level 16, State 11, Line 104

Cannot find the object "dbo.tblStatus" because it does not exist or you do not have permissions.

Msg 1088, Level 16, State 11, Line 112

Cannot find the object "dbo.tblStatus" because it does not exist or you do not have permissions.

Creating tblParty ....

Inserting Defaults data into tblParty ....

Msg 1088, Level 16, State 11, Line 164

Cannot find the object "dbo.tblOffice" because it does not exist or you do not have permissions.

Msg 1088, Level 16, State 11, Line 173

Cannot find the object "dbo.tblCand_Office" because it does not exist or you do not have permissions.

Creating tblCands table ....

Msg 1767, Level 16, State 0, Line 180

Foreign key 'FK__tblCands__Cand_S__3A81B327' references invalid table 'tblStates'.

Msg 1750, Level 16, State 1, Line 180

Could not create constraint or index. See previous errors.

Msg 1911, Level 16, State 1, Line 201

Column name 'StateID' does not exist in the target table or view.

Msg 1750, Level 16, State 0, Line 201

Could not create constraint or index. See previous errors.

Inserting Defaults data into tblStates ....

Msg 1088, Level 16, State 11, Line 213

Cannot find the object "dbo.tblStates" because it does not exist or you do not have permissions.

Completion time: 2023-02-27T09:02:53.1476785-06:00

Most of these errors occur because it says certain table objects exists in the database I am trying to recreate. How? the database has been dropped and recreated. I have run various queries checking both the tables and objects against the Master.dbo. If they don't show up in these queries how are they still in the database?

Thank you,

MRM256

SQL Server Other
{count} votes

4 answers

Sort by: Most helpful
  1. José Antonio Campero Morales 105 Reputation points
    2023-02-28T01:50:35.46+00:00

    Hi, it's possible that some objects are not being dropped or removed properly during the database drop/recreate process. Here are some suggestions to troubleshoot this issue:

    1. Check if there are any active connections to the database. You cannot drop a database if any active connection is using it. Check if there are any connections to the database and disconnect them before trying to drop the database.
    2. Double-check the SQL script you are using to recreate the database. Make sure that all the objects are being dropped and recreated properly. If you are using a separate script to drop objects, make sure it is executing correctly.
    3. Try running the SQL script on a different server or instance to see if the issue persists. If the issue occurs on multiple servers, it's likely a problem with the SQL script.
    4. Check if the database is being backed up and restored during the process. If so, make sure that the restore process is not leaving any objects behind.
    5. If the issue persists, you may need to manually drop the objects using SQL commands or a tool like SQL Server Management Studio.

    Also, make sure you are running the script with the correct user and permissions. If the user doesn't have the appropriate permissions, the script may not execute correctly.


  2. Anonymous
    2023-02-28T05:48:29.44+00:00

    Hi @Mark McCumber

    In addition to the above answer, you can refer to this official document for deleting databases.

    https://learn.microsoft.com/en-us/sql/relational-databases/databases/delete-a-database?view=sql-server-ver16

    There are some prerequisites that need to be met before the database can be dropped.

    Best regards,

    Percy Tang


    If the answer is the right solution, please click "Accept Answer". If you have extra questions about this answer, please click "Comment".


  3. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2023-02-28T22:35:34.8633333+00:00

    It had certainly helped to see the full script.

    Typically, when this happens, there is a missing USE in the script, so the objects are created in the database from where you ran the DROP DATABASE, and the second time you do that, the tables will exist.


  4. Mark McCumber 431 Reputation points
    2023-03-01T18:47:36.1833333+00:00

    Jingyang LI pointed out the problem in the CONSTRAINT part of the table definition.

    I didn't identify the primary key column in my supporting tables.

    However, another monkey has popped up.

    I will try to find an answer for this one on the internet before I post another question.

    Thanks for the help,

    MRM256


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.