T/SQL Part - One

In this article, we will learn about T/SQL and the differences between SQL and T/SQL.

Introduction

T/SQL stands for Transact Extension of Structure Query Language.

Differences Between SQL and T/SQL

T/SQL is a procedural language whereas SQL is a non-procedural language.

SQL supports the single line stint execution process only, whereas T/SQL supports the multiple stints execution process.

SQL doesn’t support conditional and control statements like if, if-else, multiple if else, nested if-else and while loop control statement. But these are possible to implement under T/SQL.

We can’t implement Try and Catch handling methods in SQL, whereas we can implement  these in T/SQL.

Declaration of variables are not supported in SQL but they are supported In T/SQL.

SQL doesn’t provide re-usability and security facilities to the query information, whereas T/SQL provides re-usability and security facilities by using the database objects which are stored procedures, stored function, and database triggers.

In T/SQL the user will write the stints under a Block.

Block

Block is a collection of stints which are executed as a single unit by the server.

Query Execution Process in SQL 

In this case, every SQL stint is executed individually by the server so that the burden of the server will be increased and application performance will be reduced.

 

Query Execution Process in T/SQL

In this case, the SQL stint is executed as a single unit by the server so that the burden of the server will be reduced and application performance will be increased.

 

Types of Blocks

In T/SQL the user will interact with two types of blocks,

  1. Anonymous Block
  2. Sub Block
Anonymous BlockSub Block
It is unnamed blockIt is named block
These are temporary blocksThese are permanent blocks
It will not be saved into databaseThese are saved into the database
Every time compilation and execution of the programThese are pre-compiled program
These consume more timeThese are time-saving blocks
It will not provide re-usability and securityIt will provide re-usability and security

Working With Anonymous Blocks

When we work with Anonymous Blocks we should follow the following steps.

Step 1

Declare Variable: When we declare variables under T/SQL Blocks we should use Declare keyword and prefix symbol (@)

Syntax

Declare @<variable Name> <Datatype>[Size]

Example

Declare @a int,@name varchar(50)

Step 2

Assigning the values to the variables: When we assign the values to the variables we should use the Set keyword as below:

Syntax

set @<variable name >=value

Example

set @a=10;
Set @name='Sam'

Step 3

To display the variable values or message on the screen: When we display the variable values and message then we use the print statement.

Syntax 

Print <variable name>

Example

Print @a;
Print @name

Example

Write a program to perform arithmetic operation on given values,

  1. Declare @a int,@b int,@c int  
  2. Set @a=12;set @b=4;  
  3. Set @c=@a+@b;  
  4. Print 'Add is:'+cast(@c as char)  
  5. Set @c=@a-@b;  
  6. Print 'Sub is:'+cast(@c as char)  
  7. Set @c=@a*@b;  
  8. Print 'Multiple is:'+cast(@c as char)  
  9. Set @c=@a/@b;  
  10. Print 'Div is:'+cast(@c as char)  
Result


Summary

In this article, we have learned about T/SQL and the difference between SQL and T/SQL. We also learned about the block and query execution process. In the next article of this series, we will learn about cursor, store procedure, store function, and triggers.