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
20
votes
1 answer

What can I use instead of #Temp table in sql function

Here is my sql query. CREATE FUNCTION UF_GetOrderProducts ( @OrderId int ) RETURNS VARCHAR(500) AS BEGIN SELECT Identity(int,1,1) ID, ProductId INTO #Temp FROM OrderProduct WHERE OrderId = @OrderId Declare @Id int, @Count int, …
cagin
  • 5,772
  • 14
  • 74
  • 130
20
votes
4 answers

T-SQL "Where not in" using two columns

I want to select all records from a table T1 where the values in columns A and B has no matching tuple for the columns C and D in table T2. In mysql “Where not in” using two columns I can read how to accomplish that using the form select A,B from T1…
Anders Lindén
  • 6,839
  • 11
  • 56
  • 109
20
votes
7 answers

How to recreate the InitCap() function in T-SQL to apply first-letter capitalization to a string?

I have a table on my database. My table's name is "Company". I want to change data "company_name" as upper case first letter. For example; "ABC COMPANY" "DEF PLASTICITY" as "Abc Company" "Def Plasticity" I know that I should use "UPDATE" command.…
cethint
  • 2,231
  • 8
  • 28
  • 31
20
votes
8 answers

How do I generate CRUD stored procedures from a table in SQL Server Management Studio

How do I take a table, and auto-gen CRUD stored procs for it in SSMS?
The Internet
  • 7,959
  • 10
  • 54
  • 89
20
votes
7 answers

SQL: Concatenate column values in a single row into a string separated by comma

Let's say I have a table like this in SQL Server: Id City Province Country 1 Vancouver British Columbia Canada 2 New York null null 3 null Adama null 4 …
Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275
20
votes
9 answers

SQL take just the numeric values from a varchar

Say i have a few fields like the following: abd738927 jaksm234234 hfk342 ndma0834 jon99322 Type: varchar. How do I take just the numeric values from this to display: 738927 234234 342 0834 99322 Have tried substring however the data varies in…
JsonStatham
  • 9,770
  • 27
  • 100
  • 181
20
votes
3 answers

TRY and RAISERROR in T-SQL

Having a small issue and wondering if I'm using these correctly. In my SQL script is have BEGIN TRY // check some information and if there are certains errors RAISERROR ('Errors found, please fix these errors and retry', 1, 2) WITH…
StevenMcD
  • 17,262
  • 11
  • 42
  • 54
20
votes
6 answers

SQL Server 2008 - Case / If statements in SELECT Clause

I have a Query that's supposed to run like this - If(var = xyz) SELECT col1, col2 ELSE IF(var = zyx) SELECT col2, col3 ELSE SELECT col7,col8 FROM . . . How do I achieve this in T-SQL without writing separate queries for each clause?…
Tejaswi Yerukalapudi
  • 8,987
  • 12
  • 60
  • 101
20
votes
3 answers

TSQL - how to execute a query as a variable?

DECLARE @query as varchar(200); SET @query = 'SELECT COUNT(*) FROM table'; How can I execute @query, and additionally, is there way to store the query result directly when assigning the variable?
Malyo
  • 1,990
  • 7
  • 28
  • 51
20
votes
3 answers

Is it possible to change the datatype of a column in a view?

Usually I run a script like this: ALTER TABLE [TABLE] ALTER COLUMN [Column] NVARCHAR(40); The result is that the field in the table gets converted to nvarchar. But what is the syntax for doing the same thing for a view? Or is that even possible?
Zolt
  • 2,761
  • 8
  • 43
  • 60
20
votes
8 answers

T-SQL 1=1 Performance Hit

For my SQL queries, I usually do the following for SELECT statements: SELECT ... FROM table t WHERE 1=1 AND t.[column1] = @param1 AND t.[column2] = @param2 This will make it easy if I need to add / remove / comment any WHERE clauses, since I…
Adrian Godong
  • 8,802
  • 8
  • 40
  • 62
20
votes
6 answers

How do I switch column values from 0 to 1 and vice versa using the update statement?

I have a table Table1 as follows col1 col2 ---- ---- A 1 B 1 C 1 D 0 E 0 F 0 I want the result table should be as follows (by using Update statement) col1 col2 ---- ---- A 0 B 0 C 0 D 1 E 1 F 1
Venkateswarlu Avula
  • 341
  • 1
  • 4
  • 14
20
votes
4 answers

T-SQL: How to use parameters in dynamic SQL?

I have the following dynamic query which is working fine without the WHERE clause, which is expecting UNIQUEIDENTIFIER. When I pass it in, I don't get a result. I tried CAST and CONVERT, but no result. I might be doing it wrong, can anybody…
Yaser Ahmed
  • 519
  • 2
  • 7
  • 15
19
votes
5 answers

SET NOCOUNT OFF or RETURN @@ROWCOUNT?

I am creating a stored procedure in Sql Server 2008 database. I want to return the number of rows affected. Which is a better option SET NOCOUNT OFF or RETURN @@ROWCOUNT? ALTER PROCEDURE [dbo].[MembersActivateAccount] @MemberId…
niaher
  • 9,460
  • 7
  • 67
  • 86
19
votes
4 answers

How to GROUP and choose lowest value in SQL

I have table consisting of these fields: id | date_from | date_to | price -------------------------------------------- CK1 22-12-2012 29-12-2012 800 CK1 22-12-2012 29-12-2012 1200 CK2 22-12-2012 …
feronovak
  • 2,687
  • 6
  • 35
  • 54