How to check if database table exists when the table has 0 rows?

EDITED FOLLOWING FEEDBACK.

EDITED FOLLOWING FEEDBACK.

In the following code "exist" should == 1 if the table exists in the database. However, it is returning 0 for me, even though the table exists in the database. I am not sure why this is happening?

Thanks:

foreach (var row in calcSched)
            {
                var dif3 = new DataInterfaceFactory(DatabaseTypes.SqlServer, row.DatabaseName, row.ServerName);
                //var dbName = m_api.Database.ExecuteQuery<CALCULATION_SCHEDULE>("SELECT CONCAT(c.SERVER_NAME, '.', c.DATABASE_NAME)");// concat server name and DB name
            using (DataReader dr2 = dif3.GetDataReader())
            {
                dr2.ExecuteReader("SELECT COUNT(1) AS TABLECOUNT2 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'PMS_CAL_SCHEDULE'");
                var exists = Convert.ToInt32(dr2["TABLECOUNT2"]);
                CommandExecutor CE = dif3.GetCommandExecutor();

                try
                {
                    if (exists == 0)//if table doesn't exist create it
                    {
                        string sql = "CREATE TABLE dbo.PMS_CAL_SCHEDULE(CALCULATION_SCHEDULE_UID INT, USER_UID INT, SCHEDULE_DATE DATE, PMS_CALC_STATUS_UID INT, CALCULATION_START_DATE DATE, CALCULATION_END_DATE DATE, STATUS_MESSAGE CHAR(100), PROGRESS CHAR(20)";
                        CE.ExecuteNonQuery(sql);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: could not create PMS_CALC_SCHEDULE");
                }
            }
        }


SQL Server Tutorial For Beginners - Learn MS SQL Server with Examples

SQL Server Tutorial For Beginners - Learn MS SQL Server with Examples

SQL Server Tutorial For Beginners, Microsoft SQL Server Tutorial, SQL Server Training. Microsoft SQL Server explains all the fundamentals of MS SQL Server with examples. What is DBMS? What is SQL? What is SQL Server? SQL Server Installation

This Edureka tutorial video on Microsoft SQL Server explains all the fundamentals of MS SQL Server with examples.
The following are the topics covered in this tutorial:

  • What is DBMS?
  • Types of DBMS
  • What is SQL?
  • What is SQL Server?
  • Features of SQL Server
  • SQL Server Installation
  • SQL Server Command Categories
  • SQL Server Data Types
  • DDL Commands
  • Keys in Database
  • Constraints in Database
  • DML Commands
  • Operators
  • Nested Queries
  • Joins
  • Stored Procedures
  • DCL Commands
  • TCL Commands
  • Exception Handling

How Containerized SQL Server Makes Development Easier

How Containerized SQL Server Makes Development Easier

Managing development databases can be tricky, especially if members of the team want to develop on a Mac or Linux device. In this presentation, we learn how running SQL Server in Docker containers can help ensure a consistent development & testing experience for the entire team.

How Containerized SQL Server Makes Development Easier

Managing development databases can be tricky, especially if members of the team want to develop on a Mac or Linux device. In this presentation, we learn how running SQL Server in Docker containers can help ensure a consistent development & testing experience for the entire team.

How to Restore SQL Server Master Database With or Without Backup

<strong>To reduce failed attempts from users, we will talk about restoring the SQL master database in this post.</strong>

To reduce failed attempts from users, we will talk about restoring the SQL master database in this post.

Despite being a popular relational database management system, SQL Server often encounters corruption and other issues. For this reason, SQL users often complain about being in trouble and unable to perform some simple tasks. However, it is not always the fault of SQL Server that the users cannot restore the SQL Server master database seamlessly. Sometimes, users do not have sufficient knowledge about the process either, and that is what stops them from successful database restoration. To reduce failed attempts from users, we will talk about restoring the SQL master database in this post. Continue reading to learn about the processes in detail.

Why Do You Need to Restore SQL Master Database?

Master database file (A.K.A. MDF file) is the main user database file that contains all the major data in it. If the database gets infected by a virus or encounters some other severe issue, it is the database file that gets affected in the beginning. In case of database corruption or some other errors, master database restoration is the only solution users are left with.

How to Restore SQL Server Master Database

Users who face the need to restore MDF file may or may not have a backup file with them. No matter how strange it may seem, many database admins do not regularly back up their SQL database. Hence, they do not have any backup files to restore. We will talk about both situations and how users can restore their master database with and without a backup file.

Restore SQL Server Master Database from Backup File

**User’s Query: **“Hello all. I am in urgent need to restore SQL master database. But sadly, when I try out the manual technique, I fail to perform the restoration process. I started the process by putting the Server in Single user mode, but after performing one to two more steps, I encountered some error. Not sure where I made the mistake that the process got aborted. Can you suggest how to restore master database from backup manually? I am using SQL Server Enterprise 2005 version and my operating system is Windows Server 2008 R2.”

If you have a backup of the master database, then you can easily restore the MDF file using the manual method. Make sure the backup does not contain any corruption issues. The process is short and does not need the help ofany extra tool. Here is the guide to manually restorethe master database in SQL Server.

  • Put your SQL Server instance into the single user Mode.
  • Run this TSQL command to restore the master database:

“RESTORE DATABASE master FROM<backup_device>WITH REPLACE”

The “replace” command means the restoration process will continue even if there is a database with the same name, and the existing database will be removed.

Note: When the restoration process is complete, SQL Server instance will close down. You have to switch the single-user parameter before starting the Server again.

Restart SQL Server and perform other recovery tasks like database attachment, other database file restoration, etc.

Restore Master Database Without Backup File

User’s Query: “I am a SQL Server 2016 users and my database has become corrupt severely. The database contains a lot of business-critical data, so I cannot do away with the MDF file. Is it possible to restore SQL Server Master database without backup file? Please tell me as I am okay with both the manual and the automated method.”

It is possible that you may not have the MDF file backup with you. Earlier, users had a troublesome time without a backup, but not anymore. Now, they can easily restore the master database from their corrupt or damaged MDF file with the help of SysTools SQL Recovery. The process of restoration is simple and straight forward. Just follow the steps mentioned below and get your task done.

  • Select the Advance Scan option. Also, add the SQL Server version. If you do not know the version of the SQL MDF file, then it has an option to Auto-detect the version. Click on OK.

  • The application will scan the MDF file to fix all the corruption issues and show the scanning report.

  • You can preview any item of the master database by clicking on the tree structure of the left panel and clicking the Export button.

  • Choose Export as an option, whether you want to export as SQL Server Database or as SQL .csv scipt.

  • Enter SQL Server credentials like its server name, username, and password, and select the destination database in which you want to export your recovered database objects.

  • Select the With Schema & Data option and click the Export button, and the restoration of the master database of SQL Server will be done effortlessly.

  • You can visit the SQL Server and locate the restored database there.

Conclusion

Various reasons can lead you to restore the SQL Server master database. While the manual approach is fine, if you have a backup, the software will let you restore the master database without backup. Choose the solution based on your demand and restore without any trouble.