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
60
votes
17 answers

Round *UP* to the nearest 100 in SQL Server

Is it possible to easily round a figure up to the nearest 100 (or 1000, 500, 200 etc.) in SQL Server? So: 720 -> 800 790 -> 800 1401 -> 1500
joshcomley
  • 28,099
  • 24
  • 107
  • 147
60
votes
7 answers

Rename SQL Server Schema

How can I rename a schema using SQL Server?
Dawid
60
votes
13 answers

SQL Server 2005 drop column with constraints

I have a column with a "DEFAULT" constraint. I'd like to create a script that drops that column. The problem is that it returns this error: Msg 5074, Level 16, State 1, Line 1 The object 'DF__PeriodSce__IsClo__4BCC3ABA' is dependent on column…
Julien N
  • 3,880
  • 4
  • 28
  • 46
60
votes
4 answers

SQL Server 2008 - How do i return a User-Defined Table Type from a Table-Valued Function?

Here's my user-defined table type... CREATE TYPE [dbo].[FooType] AS TABLE( [Bar] [INT], ) This is what ive had to do in my table-valued function to return the type: CREATE FUNCTION [dbo].[GetFoos] RETURN @FooTypes TABLE ([Bar] [INT]) INSERT INTO…
60
votes
9 answers

What's the fastest way to bulk insert a lot of data in SQL Server (C# client)

I am hitting some performance bottlenecks with my C# client inserting bulk data into a SQL Server 2005 database and I'm looking for ways in which to speed up the process. I am already using the SqlClient.SqlBulkCopy (which is based on TDS) to speed…
Andrew
  • 2,810
  • 2
  • 27
  • 14
60
votes
1 answer

Which SQL data type to store Base64 encoded file?

nvarchar(max), varchar(max), or should I really decode the string and put it in an image or blob or something? The longer version : (with all the juicy details) I've got a .Net SOAP web service, which saves records in a SQL 2008 DB. The service is…
Andrew M
  • 9,149
  • 6
  • 44
  • 63
60
votes
9 answers

Scripting SQL Server permissions

I want to copy all the permission I've set on stored procedures and other stuff from my development database to my production database. It's incredibly cumbersome, not to mention error prone, to do this all by hand through the SSMS GUI tool. So I'm…
Chris Wuestefeld
  • 3,266
  • 2
  • 23
  • 23
60
votes
2 answers

Multiple SQL statements in one roundtrip using Dapper.NET

There is a nice feature in ADO.NET that allows you to send multiple SQL statements to database in one roundtrip and receive results for all statements: var command = new SqlCommand("SELECT count(*) FROM TableA; SELECT count(*) FROM TableB;",…
user1224129
  • 2,759
  • 3
  • 27
  • 29
60
votes
4 answers

Scope of temporary tables in SQL Server

I wrote a stored procedure to import and transform data from one database to another. Each import would take a single company ID and import all data related to this company. To help with the transformation step I use temporary tables. As part of…
Cogslave
  • 2,513
  • 3
  • 25
  • 35
60
votes
3 answers

Eliminating duplicate values based on only one column of the table

My query: SELECT sites.siteName, sites.siteIP, history.date FROM sites INNER JOIN history ON sites.siteName = history.siteName ORDER BY siteName,date First part of the output: How can I remove the duplicates in siteName column? I want to…
Ned
  • 1,055
  • 9
  • 34
  • 58
60
votes
6 answers

My Select SUM query returns null. It should return 0

I'm trying to sum up Customer balances using the following query: select sum(balance) from mytable where customer = 'john' However, if the customer has no balance (i.e. no matching rows in the mytable table), my query returns null and not 0.…
nec tso
  • 649
  • 1
  • 5
  • 6
60
votes
6 answers

SQL list of all the user defined functions in a database

I am looking for a SQL query that outputs the function definitions for all of the user defined functions in a database catalog. I have found as far as SELECT OBJECT_DEFINITION (OBJECT_ID(N'dbo.UserFunctionName')) AS [Object Definition] and SELECT…
stackuser83
  • 2,012
  • 1
  • 24
  • 41
60
votes
3 answers

How to use a CTE statement in a table-valued function in SQL Server

I have come to understand that some versions of Microsoft OLE DB Provider for SQL Server (mostly on Windows XP) do not support WITH statement. So, I decided to move my SQL statement into a table-valued function, and call it from my application. Now,…
iMan Biglari
  • 4,674
  • 1
  • 38
  • 83
60
votes
5 answers

Create a one to many relationship using SQL Server

How do you create a one to many relationship using SQL Server?
Tim
  • 1,197
  • 3
  • 11
  • 7
60
votes
2 answers

How do I determine if a database role exists in SQL Server?

I'm trying to figure out how I can check if a database role exists in SQL Server. I want to do something like this: if not exists (select 1 from sometable where rolename='role') begin CREATE ROLE role AUTHORIZATION MyUser; end What table/proc…
Jon Kruger
  • 4,009
  • 4
  • 33
  • 46
1 2 3
99
100