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
1391
votes
6 answers

Difference between JOIN and INNER JOIN

Both these joins will give me the same results: SELECT * FROM table JOIN otherTable ON table.ID = otherTable.FK vs SELECT * FROM table INNER JOIN otherTable ON table.ID = otherTable.FK Is there any difference between the statements in performance…
driis
  • 161,458
  • 45
  • 265
  • 341
1371
votes
43 answers

How can I remove duplicate rows?

I need to remove duplicate rows from a fairly large SQL Server table (i.e. 300,000+ rows). The rows, of course, will not be perfect duplicates because of the existence of the RowID identity field. MyTable RowID int not null identity(1,1) primary…
Seibar
  • 68,705
  • 38
  • 88
  • 99
1360
votes
14 answers

Altering a column: null to not null

I have a table that has several nullable integer columns. This is undesirable for several reasons, so I am looking to update all nulls to 0 and then set these columns to NOT NULL. Aside from changing nulls to 0, data must be preserved. I am looking…
Karmic Coder
  • 17,569
  • 6
  • 32
  • 42
1355
votes
15 answers

How to get the identity of an inserted row?

How am I supposed to get the IDENTITY of an inserted row? I know about @@IDENTITY and IDENT_CURRENT and SCOPE_IDENTITY, but don't understand the implications or impacts attached to each. Can someone please explain the differences and when I would be…
Oded
  • 489,969
  • 99
  • 883
  • 1,009
1338
votes
14 answers

How do I escape a single quote in SQL Server?

I am trying to insert some text data into a table in SQL Server 9. The text includes a single quote '. How do I escape that? I tried using two single quotes, but it threw me some errors. eg. insert into my_table values('hi, my name''s tim.');
tim_wonil
  • 14,970
  • 6
  • 29
  • 42
1278
votes
12 answers

What do Clustered and Non-Clustered index actually mean?

I have a limited exposure to DB and have only used DB as an application programmer. I want to know about Clustered and Non clustered indexes. I googled and what I found was : A clustered index is a special type of index that reorders the way …
P.K
  • 18,587
  • 11
  • 45
  • 51
1196
votes
29 answers

SQL Update from One Table to Another Based on a ID Match

I have a database with account numbers and card numbers. I match these to a file to update any card numbers to the account number so that I am only working with account numbers. I created a view linking the table to the account/card database to…
Boerseun
1120
votes
15 answers

When should I use CROSS APPLY over INNER JOIN?

What is the main purpose of using CROSS APPLY? I have read (vaguely, through posts on the Internet) that cross apply can be more efficient when selecting over large data sets if you are partitioning. (Paging comes to mind) I also know that CROSS…
Jeff Meatball Yang
  • 37,839
  • 27
  • 91
  • 125
1105
votes
47 answers

Exclude a column using SELECT * [except columnA] FROM tableA?

We all know that to select all columns from a table, we can use SELECT * FROM tableA Is there a way to exclude column(s) from a table without specifying all the columns? SELECT * [except columnA] FROM tableA The only way that I know is to manually…
uuɐɯǝʃǝs
1077
votes
24 answers

Search text in stored procedure in SQL Server

I want to search a text from all my database stored procedures. I use the below SQL: SELECT DISTINCT o.name AS Object_Name, o.type_desc FROM sys.sql_modules m INNER JOIN sys.objects o ON m.object_id =…
DharaPPatel
  • 12,035
  • 9
  • 31
  • 49
1063
votes
20 answers

How do I get list of all tables in a database using TSQL?

What is the best way to get the names of all of the tables in a specific database on SQL Server?
Ray
  • 187,153
  • 97
  • 222
  • 204
1005
votes
23 answers

How can I get column names from a table in SQL Server?

I want to query the name of all columns of a table. I found how to do this in: Oracle MySQL PostgreSQL But I also need to know: how can this be done in Microsoft SQL Server (2008 in my case)?
odiseh
  • 25,407
  • 33
  • 108
  • 151
982
votes
19 answers

Function vs. Stored Procedure in SQL Server

When should I use a function rather than a stored procedure in SQL, and vice versa? What is the purpose of each?
Tarik
  • 79,711
  • 83
  • 236
  • 349
973
votes
25 answers

Reset identity seed after deleting records in SQL Server

I have inserted records into a SQL Server database table. The table had a primary key defined and the auto increment identity seed is set to “Yes”. This is done primarily because in SQL Azure, each table has to have a primary key and identity…
xorpower
  • 17,975
  • 51
  • 129
  • 180
967
votes
31 answers

How can I list all foreign keys referencing a given table in SQL Server?

I need to remove a highly referenced table in a SQL Server database. How can I get a list of all the foreign key constraints I will need to remove in order to drop the table? (SQL answers preferable over clicking about in the GUI of the management…
chillitom
  • 24,888
  • 17
  • 83
  • 118