Is it correct to do the following?
IF EXISTS(SELECT * FROM dbo.Scores) DROP TABLE dbo.Scores
No. That will drop the table only if it contains any rows (and will raise an error if the table does not exist).
Instead, for a permanent table you can use
IF OBJECT_ID('dbo.Scores', 'U') IS NOT NULL DROP TABLE dbo.Scores;
Or, for a temporary table you can use
IF OBJECT_ID('tempdb.dbo.#TempTableName', 'U') IS NOT NULL DROP TABLE #TempTableName;
SQL Server 2016+ has a better way, using DROP TABLE IF EXISTS …
. See the answer by @Jovan.
Related Posts:
- Selecting COUNT(*) with DISTINCT
- What is a stored procedure?
- LEFT JOIN vs. LEFT OUTER JOIN in SQL Server
- Inserting multiple rows in a single SQL query? [duplicate]
- LEFT JOIN vs. LEFT OUTER JOIN in SQL Server
- SQL Server: Difference between PARTITION BY and GROUP BY
- Self Join to get employee manager name
- How do I UPDATE from a SELECT in SQL Server?
- SQL query to select dates between two dates
- When should I use CROSS APPLY over INNER JOIN?
- How can I do an UPDATE statement with JOIN in SQL Server?
- T-SQL split string
- Function to Calculate Median in SQL Server
- Function to Calculate Median in SQL Server
- How do I escape a single quote in SQL Server?
- T-SQL split string based on delimiter
- What is the use of GO in SQL Server Management Studio & Transact SQL?
- How do I perform an IF…THEN in an SQL SELECT?
- How do I escape a single quote in SQL Server?
- IDENTITY_INSERT is set to OFF – How to turn it ON?
- Each GROUP BY expression must contain at least one column that is not an outer reference
- SQL Server : Arithmetic overflow error converting expression to data type int
- How to calculate percentage with a SQL statement
- Column name or number of supplied values does not match table definition
- SQL Server FOR EACH Loop
- What is the meaning of the prefix N in T-SQL statements and when should I use it?
- Query error with ambiguous column name in SQL
- Each GROUP BY expression must contain at least one column that is not an outer reference
- Sql Server string to date conversion
- Check if table exists in SQL Server
- Operand type clash: int is incompatible with date + The INSERT statement conflicted with the FOREIGN KEY constraint
- Keeping it simple and how to do multiple CTE in a query
- Why use a READ UNCOMMITTED isolation level?
- Find all tables containing column with specified name – MS SQL Server
- DateTime2 vs DateTime in SQL Server
- How to group by month from Date field using sql
- SQL – The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
- How can I get column names from a table in SQL Server?
- Is there a combination of “LIKE” and “IN” in SQL?
- How to get a date in YYYY-MM-DD format from a TSQL datetime field?
- SQL Server String or binary data would be truncated
- How to parse XML data in SQL server table
- What is the equivalent of ‘describe table’ in SQL Server?
- Error converting data type varchar to float
- What is the Oracle equivalent of SQL Server’s IsNull() function?
- MS SQL compare dates?
- MS SQL compare dates?
- SQL Server Installation – What is the Installation Media Folder?
- I want to use CASE statement to update some records in sql server 2005
- What represents a double in sql server?
- Get size of all tables in database
- Using RegEx in SQL Server
- Conversion failed when converting the varchar value ‘simple, ‘ to data type int
- SQL-Server: The backup set holds a backup of a database other than the existing
- Must declare the scalar variable
- SELECT DISTINCT on one column
- Exclude a column using SELECT * [except columnA] FROM tableA?
- Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 4 (Year)
- Search all tables, all columns for a specific value SQL Server [duplicate]
- vs shell installation has failed with exit code 1638
- TSQL PIVOT MULTIPLE COLUMNS
- How to format a numeric column as phone number in SQL
- SQL “select where not in subquery” returns no results
- Equivalent of Oracle’s RowID in SQL Server
- How do I format a number with commas in T-SQL?
- Format number as percent in MS SQL Server
- XOR in SQL Server
- SQL: IF clause within WHERE clause
- Is it possible to specify condition in Count()?
- SQL Server dynamic PIVOT query?
- How to write a foreach in SQL Server?
- Error converting data type varchar
- Temporary table in SQL server causing ‘ There is already an object named’ error
- I want to use CASE statement to update some records in sql server 2005
- To add server using sp_addlinkedserver
- How to calculate age (in years) based on Date of Birth and getDate()
- ORDER BY items must appear in the select list if SELECT DISTINCT is specified
- Text was truncated or one or more characters had no match in the target code page including the primary key in an unpivot
- How can I group by date time column without taking time into consideration
- What is the data type for Currency in SQL Server?
- Comma separated results in SQL
- “select * into table” Will it work for inserting data into existing table
- SUM OVER PARTITION BY
- Update query using Subquery in Sql Server
- Varchar invalid for Sum operator
- How to truncate string using SQL server
- An explicit value for the identity column in table can only be specified when a column list is used and IDENTITY_INSERT is ON SQL Server
- Pad a string with leading zeros so it’s 3 characters long in SQL Server 2008
- Case in Select Statement
- Must declare the scalar variable
- INSERT statement conflicted with the FOREIGN KEY constraint – SQL Server
- Update statement to update multiple rows
- SQL Server : error converting data type varchar to numeric
- Determine ROW that caused “unexpected end of file” error in BULK INSERT?
- What is the maximum characters for the NVARCHAR(MAX)?
- ListAGG in SQLSERVER
- T-SQL: Selecting rows to delete via joins
- Append table to an existing one: SQL Server
- SQL Server – An expression of non-boolean type specified in a context where a condition is expected, near ‘RETURN’
- Using Excel VBA to run SQL query