Search this blog

Showing posts with label T-SQL. Show all posts
Showing posts with label T-SQL. Show all posts

Monday, April 9, 2012

Difference Between SQL Vs. T-SQL?

SQL and T-SQL are Query languages, both helps to extract, Manipulate data with database system.

SQL – Structured Query Language:-

It is pronounced as SEQUEL, stands for… Structured English QUEry Languages. 
  • SQL Language has several language elements called clauses, phrases, predicates, queries and statements. 
  • SQL Supports  to insert/update/read/delete the data from/to the database, define the new structure, etc., 
  • SQL supports different data types like image, text, varchar, datetime,xml, etc., SQL supports only single line transactions

T-SQL – Transact-SQL:-

T-SQL is advanced/addition to the SQL developed by Microsoft. It has new features like, variable, temp tables, set of transactions, store procedures, functions, cursors etc.., T-SQL supports control flow capabilities by using the following commands: START and TERMINATE, break, CONTINUE, GOTO, RETURN, IF, While and WAITFOR. T-SQL allows the ‘FROM’ clause to be added to DELETE and UPDATE statements. T-SQL allows inserting multiple rows using single statement BULK INSERT.

Thursday, July 8, 2010

SQL Vs. T-SQL

Structured Query Language (SQL)
Transact-SQL (T-SQL)
ANSI/ISO Standard DB Language
SQL Server's Implementation of Language
Simple SQL Statements. Managing Data
Extension of SQL. Combination of Multiple Transactions sing multiple SQL Commands
Control Flow Language. It controlled by…

IF and ELSE
BEGIN And END
WAITFOR
RETURN
BREAK
WHILE
CONTINUE

DELETE OR UPDATE can be used with FROM & JOIN Clause
BULK INSERT

Monday, June 14, 2010

T-SQL: PIVOT Operator with Dynamic Columns

In My Previous post "PIVOT Operator in SQL", I explained about, How to use T-SQL Operator PIVOT.

http://blog-mstechnology.blogspot.com/2009/06/pivot-operator-in-sql.html

In Previous post, I explained using Static PIVOT Columns in the Query, which are hard coded.

This post explains with dynamic columns in PIVOT, For Example: Distinct values from columns of a table.

I used the AdventureWorks Database in my samples script

Have a look at this Query, Here I Explained in 4 steps alsong with the Query.

USE AdventureWorks
GO

#1. Declare a Variable to Store All the Column Values
DECLARE @columns VARCHAR(8000)

#2. Columns Vlaues are Storing in @columns Values In ID or QUOTED_ID Format


SELECT
@columns =
COALESCE
(
@columns + ',[' + cast(DATEPART(yyyy, OrderDate) as varchar) + ']',
'[' + cast(DATEPART(yyyy, OrderDate) as varchar)+ ']'
)
FROM
Sales.SalesOrderHeader
GROUP BY
DATEPART(yyyy, OrderDate)
ORDER BY DATEPART(yyyy, OrderDate)


@columns Contains:
#3. Building PIVOT Query with Dynamic Columns(@columns)

DECLARE @query VARCHAR(8000)
SET @query = 'SELECT *
FROM
(
SELECT
CustomerID,
DATEPART(yyyy, OrderDate) as OrderYear,
TotalDue
FROM
Sales.SalesOrderHeader
) PIV
PIVOT
(
SUM(TotalDue) FOR OrderYear in ('
+ @columns +
')
) AS chld'


#4. Executing Built Query
EXECUTE (@query)
GO
Output:
Hope it Helps you!