Questions tagged [sql-server]

Microsoft SQL Server is a relational database management system (RDBMS). Use this tag for all Microsoft SQL Server editions including Compact, Express, Azure, Fast-track, APS (formerly PDW) and Azure SQL DW. Do not use this tag for other types of DBMS (MySQL, PostgreSQL, Oracle, etc.). Do not use this tag for issues on software and mobile development, unless it is directly related to the database.

How to write a good SQL Server question

There are six ingredients to this recipe:

  1. Provide a clear textual explanation of the problem at hand.
  2. Provide proper sample data, as DDL (Create table statement(s)) and DML (Insert statement(s)). The sample data should be enough to demonstrate the problem, including edge cases. Usually, a few relevant rows are enough. (You can use SQLFiddle or db-fiddle as an online tool to generate DDL and DML statements for you from tabular data using their Text To DDL button.)
  3. Provide expected output for the sample data you've posted.
  4. Show your attempts to solve the problem.
  5. Tag properly. In addition to , also provide the tag for the lowest version you need the solution for. (I.e., if you're developing on SQL Server 2016, but the production server is SQL Server 2012, use .)
  6. Do not include images of data or code!

A link to an online SQL test environment such as SQL Fiddle or DB Fiddle might help, but it is not a replacement for having all the data inside the question.

For more information, read Why should I provide an MCVE for what seems to me to be a very simple SQL query? and Help me write this query in SQL.

Questions about performance tuning should include, along with all of the above, the execution plan of the query. (How do I obtain a Query Execution Plan?). You can paste the execution plan on Brent Ozar's Paste The Plan and share a link to it in the question.

General Information:

Microsoft's SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality. It originated from the Sybase SQL Server 4.x codebase and Transact-SQL dialect (), but it has forked significantly since then.

SQL Server is available in multiple versions (typically identified by release year), each of which are subdivided into editions to distinguish between product functionality. The latest released version is SQL Server 2019 which was released on November 4th, 2019.

The SQL Server product range is split broadly into six categories:

  1. SQL Server () is the main suite of enterprise and developer server products. The primary differences are licensing costs, capacities, and components included in the product, with some minor differences supported language features. Standard components include database language and storage server, developer tools, ETL tools (), schedulers, and replication. Other components include OLAP (), reporting (), and parallel computation. Components runs as NT Services.

  2. SQL Server Express () is free for use and distribution, but it has reduced engine performance, functionality and capacity than found in its other server siblings. It is focused on small deployments and runs as an NT service.

  3. SQL Server Compact Edition () is an embeddable subset of SQL Server. Like the Express edition it has a reduced language, functionality and capacity, but it is free to distribute. It's focused on small installations and desktop applications where its small footprint and no-management-required features are a great advantage.

Note: SQL Server Compact Edition is deprecated. Customers should use SQL Server Express and possibly LocalDB).

  1. Azure SQL Database () is a completely managed, hosted, high-availability product with similar features to SQL Server, operated in Microsoft Azure data centers. Notable differences are language syntax support for federated queries, and lack of support for CLR integration.

  2. SQL Server Analytics Platform System (or APS), formerly known as SQL Server Parallel Data Warehouse (PDW), is a pre-built data warehouse appliance that offers massively parallel processing for SQL Server, allowing support for many hundreds of terabytes.

  3. Azure SQL Data Warehouse is an enterprise-class distributed database in the Azure Cloud capable of processing up to petabyte volumes of relational and non-relational data. It is the industry's first cloud data warehouse with grow, shrink, and pause in seconds.

Microsoft offers a comparison table of the different SQL Server editions.

SQL Server Release History

+-------------+------+---------------------------+----------------------+
|   Version   | Year |       Release Name        |       Codename       |
+-------------+------+---------------------------+----------------------+
| 15.0        | 2019 | SQL Server 2019           | Seattle              |
| 14.0        | 2017 | SQL Server 2017           | Helsinki             |
| 13.0        | 2016 | SQL Server 2016           | SQL16                |
| 12.0        | 2014 | SQL Server 2014           | Hekaton              |
| 11.0        | 2012 | SQL Server 2012           | Denali               |
| 10.5        | 2010 | SQL Server 2008 R2        | Kilimanjaro (aka KJ) |
| 10.25       | 2010 | SQL Azure DB              | CloudDatabase        |
| 10.0        | 2008 | SQL Server 2008           | Katmai               |
| 9.0         | 2005 | SQL Server 2005           | Yukon                |
| 8.0         | 2003 | SQL Server 2000 x64       | Liberty              |
| 8.0         | 2000 | SQL Server 2000           | Shiloh               |
| -           | 1999 | SQL Server 7.0 OLAP Tools | Palato mania         |
| 7.0         | 1998 | SQL Server 7.0            | Sphinx               |
| 6.5         | 1996 | SQL Server 6.5            | Hydra                |
| 6.0         | 1995 | SQL Server 6.0            | SQL95                |
| 4.21(WinNT) | 1993 | SQL Server 4.21           | SQLNT                |
| 1.1 (OS/2)  | 1991 | SQL Server 1.1            | - (16-bit)           |
| 1.0 (OS/2)  | 1989 | SQL Server 1.0            | Ashton-Tate (16-bit) |
+-------------+------+---------------------------+----------------------+

References

Tagging Recommendation

There are several version- and edition-specific tags. It is recommended to use the tag together with the version- and/or edition-specific tag; for example, or . Do not use this tag for other types of DBMS (, , , etc.).

331896 questions
961
votes
13 answers

Update a table using JOIN in SQL Server?

I want to update a column in a table making a join on other table e.g.: UPDATE table1 a INNER JOIN table2 b ON a.commonfield = b.[common field] SET a.CalculatedColumn= b.[Calculated Column] WHERE b.[common field]= a.commonfield AND a.BatchNO…
Manjot
  • 11,166
  • 9
  • 38
  • 49
934
votes
10 answers

How to Join to first row

I'll use a concrete, but hypothetical, example. Each Order normally has only one line item: Orders: OrderGUID OrderNumber ========= ============ {FFB2...} STL-7442-1 {3EC6...} MPT-9931-8A LineItems: LineItemGUID Order ID Quantity …
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
920
votes
17 answers

How can foreign key constraints be temporarily disabled using T-SQL?

Are disabling and enabling foreign key constraints supported in SQL Server? Or is my only option to drop and then re-create the constraints?
Ray
  • 187,153
  • 97
  • 222
  • 204
886
votes
14 answers

Should I use != or <> for not equal in T-SQL?

I have seen SQL that uses both != and <> for not equal. What is the preferred syntax and why? I like !=, because <> reminds me of Visual Basic.
Bob The Janitor
  • 20,292
  • 10
  • 49
  • 72
880
votes
17 answers

DateTime2 vs DateTime in SQL Server

Which one: datetime datetime2 is the recommended way to store date and time in SQL Server 2008+? I'm aware of differences in precision (and storage space probably), but ignoring those for now, is there a best practice document on when to use…
Mikeon
  • 10,499
  • 6
  • 27
  • 31
862
votes
15 answers

How to drop a table if it exists?

The table name is Scores. Is it correct to do the following? IF EXISTS(SELECT * FROM dbo.Scores) DROP TABLE dbo.Scores
tmaster
  • 9,345
  • 6
  • 24
  • 27
858
votes
13 answers

Count(*) vs Count(1) - SQL Server

Just wondering if any of you people use Count(1) over Count(*) and if there is a noticeable difference in performance or if this is just a legacy habit that has been brought forward from days gone past? The specific database is SQL Server 2005.
super9
  • 29,181
  • 39
  • 119
  • 172
840
votes
5 answers

Multiple Indexes vs Multi-Column Indexes

What is the difference between creating one index across multiple columns versus creating multiple indexes, one per column? Are there reasons why one should be used over the other? For example: Create NonClustered Index IX_IndexName On…
GateKiller
  • 74,180
  • 73
  • 171
  • 204
800
votes
13 answers

SQL update query using joins

I have to update a field with a value which is returned by a join of 3 tables. Example: select im.itemid ,im.sku as iSku ,gm.SKU as GSKU ,mm.ManufacturerId as ManuId ,mm.ManufacturerName ,im.mf_item_number …
Shyju
  • 214,206
  • 104
  • 411
  • 497
796
votes
11 answers

Rename column SQL Server 2008

I am using SQL Server 2008 and Navicat. I need to rename a column in a table using SQL. ALTER TABLE table_name RENAME COLUMN old_name to new_name; This statement doesn't work.
Serhio g. Lazin
  • 9,442
  • 6
  • 25
  • 33
783
votes
16 answers

Check if a temporary table exists and delete if it exists before creating a temporary table

I am using the following code to check if the temporary table exists and drop the table if it exists before creating again. It works fine as long as I don't change the columns. If I add a column later, it will give an error saying "invalid column".…
Sridhar
  • 8,874
  • 4
  • 26
  • 37
771
votes
15 answers

How do I create a unique constraint that also allows nulls?

I want to have a unique constraint on a column which I am going to populate with GUIDs. However, my data contains null values for this columns. How do I create the constraint that allows multiple null values? Here's an example scenario. Consider…
Stuart
  • 11,775
  • 6
  • 33
  • 31
753
votes
19 answers

Get top 1 row of each group

I have a table which I want to get the latest entry for each group. Here's the table: DocumentStatusLogs Table |ID| DocumentID | Status | DateCreated | | 2| 1 | S1 | 7/29/2011 | | 3| 1 | S2 | 7/30/2011 | | 6| 1 …
kazinix
  • 28,987
  • 33
  • 107
  • 157
749
votes
24 answers

What is the best way to auto-generate INSERT statements for a SQL Server table?

We are writing a new application, and while testing, we will need a bunch of dummy data. I've added that data by using MS Access to dump excel files into the relevant tables. Every so often, we want to "refresh" the relevant tables, which means…
JosephStyons
  • 57,317
  • 63
  • 160
  • 234
739
votes
5 answers

C# Equivalent of SQL Server DataTypes

For the following SQL Server datatypes, what would be the corresponding datatype in C#? Exact Numerics bigint numeric bit smallint decimal smallmoney int tinyint money Approximate Numerics float real Date and…
George Stocker
  • 57,289
  • 29
  • 176
  • 237