Questions tagged [tsql]

T-SQL (Transact Structured Query Language) is the extension of SQL functionality supported by Sybase ASE and Microsoft SQL Server. Do not use this tag for MySQL, PostgreSql, Oracle(Pl/SQL) related queries. Please note that SQL code which is being written using LINQ will not also be the part of this tag. This tag specifically created for advanced SQL programming using Microsoft SQL Server.

When to use this tag:
Use this tag for SQL questions for or .
Do not use this tag for questions about other database vendors, since T-SQL is only used by these two products.

Questions about T-SQL should be tagged appropriately with either or , and include the version as well.

How to write a good T-SQL question:
There are five 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 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 product tag (either or ), and 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 .)

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

General Information

T-SQL (Transact Structured Query Language) is the extension of SQL functionality supported by Sybase ASE and Microsoft SQL Server. They share the name because Microsoft SQL Server was based on Sybase ASE code in a partnership that later dissolved. While the name is shared, functionality is not necessarily identical. With the introduction of Microsoft SQL Server 2005, the SQL Server code was completely re-written to provide integration with .Net.

Transact-SQL is central to using Microsoft SQL Server. All applications that communicate with an instance of SQL Server do so by sending Transact-SQL statements to the server, regardless of the user interface of the application.

It adheres to nearly all SQL standards, yet supplies an additional set of functionality not a part of the SQL standard, with concepts such as stored procedure, triggers , common table expressions, index hints, and the ability to SELECT scalars.

More information can be found in SQL Server Transact-SQL Reference and Sybase Transact-SQL User's Guide.

Free online T-SQL courses

72684 questions
19
votes
2 answers

Returning multiple rows from querying XML column in SQL Server 2008

I have a table RDCAlerts with the following data in a column of type XML called AliasesValue: AKA Pramod Singh AKA
Hugo Forte
  • 5,718
  • 5
  • 35
  • 44
19
votes
2 answers

How to include an sql file within another sql file in MS SQL?

We have several SQL scripts which are generated from an Entity Model. They need to be run in a specific order. Additionally there are several filling scripts which insert test data into the database. Currently I need to open each script in Visual…
Jiří Herník
  • 2,412
  • 1
  • 25
  • 26
19
votes
1 answer

SQL UNION FOR XML name output column

I'm trying to generate an XML output from SQL and need to use a UNION statement and also name the output column. I had this working before when I didn't need to use a UNION statement using: select( SELECT [CompanyName], [Address1], …
Jammer
  • 2,330
  • 11
  • 48
  • 77
19
votes
5 answers

TRY CATCH on a CONVERT in a Select Statement

Is it possible to use TRY CATCH blocks in SQL Selects? For stuff similar to this for example: select order, CONVERT(DATETIME, orderDate) from orders What's the best way of handling this scenario?
JohnIdol
  • 48,899
  • 61
  • 158
  • 242
19
votes
4 answers

Truncate or Drop and Create Table

I have this table in a SQL Server 2008 R2 instance which I have a scheduled process that runs nightly against it. The table can have upward to 500K records in it at any one time. After processing this table I need to remove all rows from it so I am…
Mark Kram
  • 5,672
  • 7
  • 51
  • 70
19
votes
9 answers

TSQL: Try-Catch Transaction in Trigger

I am trying to put a try-catch statement inside a trigger using Microsoft Server 2005. BEGIN TRANSACTION BEGIN TRY --Some More SQL COMMIT TRANSACTION END TRY BEGIN CATCH IF (XACT_STATE()) = -1 BEGIN ROLLBACK TRANSACTION; …
Eldila
  • 15,426
  • 23
  • 58
  • 62
19
votes
2 answers

T-SQL Get percentage of character match of 2 strings

Let's say I have a set of 2 words: Alexander and Alecsander OR Alexander and Alegzander Alexander and Aleaxnder, or any other combination. In general we are talking about human error in typing of a word or a set of words. What I want to achieve is…
Dragos Durlut
  • 8,018
  • 10
  • 47
  • 62
19
votes
7 answers

T-SQL to list all the user mappings with database roles/permissions for a Login

I am looking for a t-sql script which can list the databases and and the respective roles/privileges mapped for a particular user. Using SQL Server 2008 R2.
muddu83
  • 465
  • 2
  • 7
  • 17
19
votes
7 answers

What does ORDER BY 5 DESC mean?

SELECT Departamentos.Nome_Dep, Funcionarios.Nome AS Funcionario, Funcionarios.Salario, AVG(Funcionarios.Salario) OVER(PARTITION BY Departamentos.Nome_Dep) "Média por Departamento" Salario - AVG(Funcionarios.Salario)…
dotnetN00b
  • 5,021
  • 13
  • 62
  • 95
19
votes
8 answers

TSQL interview questions you ask

Google search turns up some links to tsql questions. I was wondering what would SO experts would ask in an interview for TSQL.
dotnet-practitioner
  • 13,968
  • 36
  • 127
  • 200
19
votes
4 answers

How do you test if something is older than 3 months?

I have been having some trouble to select the rows of my table which has a date of 3 months prior of today. I tried using DATE(NOW() - INTERVAL 3 MONTH) in my where clause, but no luck. How do I check in SQL Server if a item is older than 3 months? …
Eon
  • 3,833
  • 10
  • 46
  • 75
19
votes
2 answers

How to periodically flush dapper.net cache when used with SQL Server

Can someone please explain what this means (from the Dapper.net website) Limitations and caveats Dapper caches information about every query it runs, this allow it to materialize objects quickly and process parameters quickly. The current…
Gullu
  • 3,477
  • 7
  • 43
  • 70
19
votes
8 answers

Check for leap year

How do I check if a year is a leap year? I have this code: declare @year int set @year = 1968 SELECT CASE WHEN @YEAR = THEN 'LEAP YEAR' ELSE 'NORMAL YEAR' END Expected result: LEAP YEAR
t-clausen.dk
  • 43,517
  • 12
  • 59
  • 92
19
votes
5 answers

Why is this an Index Scan and not a Index Seek?

Here's the query: SELECT top 100 a.LocationId, b.SearchQuery, b.SearchRank FROM dbo.Locations a INNER JOIN dbo.LocationCache b ON a.LocationId = b.LocationId WHERE a.CountryId = 2 AND a.Type = 7 Location …
RPM1984
  • 72,246
  • 58
  • 225
  • 350
19
votes
11 answers

Is there anyway to reset the identity of a Table Variable?

Say I have a table variable: DECLARE @MyTableVar TABLE (ID INT IDENTITY(1,1), SomeData NVARCHAR(300)) After I have inserted 250 rows, I need to "Start Over" with the table. I do this: DELETE FROM @MyTableVar Is there anything I can do to the…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
1 2 3
99
100