Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

TSQL:

Transact SQL deals with set of statements that are grouped in the form of a block
and submitted at
server at once for execution.

Difference between SQL & TSQL

-->SQL is a NON PROCEDURAL LANGUAGE since it deals with WHAT data to be extracted.
-->T-SQL is a PROCEDURAL LANGUAGE, it deals with WHAT Data to be extracted and HOW
it should be displayed.

T-SQL supports to submit a Block to server for execution in two ways.

-->Anonymous block/nameless block – it supports to store a block in SQL File.

Declare @Action char(1)=null


if @Action='A'
Begin
select * from Emp where deptno=10
End
else if @Action='B'
begin
select * from emp where deptno=30
End
else
Begin
select * from emp
End

-->Stored Block /named block– it stores the blocks in DataBase Server.


-->Stored Procedures
-->Stored Functions
-->Stored Triggers

SP:

Types
1.Predefined SP--SP_help,SP_helptext,SP_Refresh etc
2.User Defined SP---It can be Created User
3.CLR SP

User Defined Procedures:


------------------------
-->It is DB objects .It Resides in DB Server
-->Contain the Set of Sql Statement+Proc lag
Advantage
-->Improve the Performance
-->High Productivity
-->Ease to Use
-->High Scalibility
-->High Maintability
-->High Security
-->It resuability
-->Reduce netwrok traffic
-->Reduce Complexity
Why we Use SPS:
-->Allow input and output ars upto 1024 aruguments
-->Call the SP into another SP upto 32 levels
-->Call the SF into SP
-->Easy error handling
-->Perform DML and DDL Opertions
-->It allow Temp table and Table variables

Disavantage
-->Tesing
-->Debugging
-->Vesioning
-->Cost is high--Need to expert developers
-->High portability

Syntax:

Create Proc/Procedure dbo.USP_name(no of args)


as
begin
declaration part;
Execution part;
Exection Handling;
End

---Execution

Exec USP_name(no of args)


---Stores

select * from sys.objects where type='P'

select * from sys.procedures


---alter

Alter Proc/Procedure dbo.USP_name(no of args)


as
begin
declaration part;
Execution part;
Exection Handling;
End

---Drop
drop procedure USP_name

Different ways of written SP;


================================
1.with out input args

/**
Created By: Prasad
Created Date:12-08-2021
Description: we get the employee details
Modified SP:
Modified Date:
exec USP_EmployeeDetails

**/
Alter Proc USP_EmployeeDetails
As
Begin
select * From EMP
End

select * from sys.Procedures

sp_help USP_EmployeeDetails---Information for create date ,scheme and parametres


etc

SP_helptext USP_EmployeeDetails---display the text code

/**
Created By: Prasad
Created Date:12-08-2021
Description: we get the employee details
Modified SP:
Modified Date:

exec USP_EmployeeDetails

**/
CREATE Proc USP_EmployeeDetails
As
Begin
select * From EMP
End

2.With Input args

Create Proc USP_Employee(@Deptno int,@sal int)


as
Begin

select * from EMp where deptno=@deptno and sal>@sal

End

exec USP_Employee @deptno=20,@sal=2000


exec USP_Employee 20,1500
exec USP_Employee @sal=2000, @deptno=20

exec USP_Employee 1500,20

Create Proc USP_addition @A int,@B int


As
Begin
Declare @C int
set @c =@A+@B
select @C as Addition

End

exec USP_addition 10,30

Create Proc USP_Multi @A int,@B int


As
Begin
Declare @C int
set @c =@A*@B

select @C as Multiplication

End

3.With Return Keyword

CReate Proc USP_ReturnValue(@A int,@B int)


as
Begin

declare @C int
Set @C=@A+@B

Return @C
End
/**
declare @C int
exec @C=USP_ReturnValue 10,20
select @C
***/

4.With Output keyword

Create Proc USP_OutputKeyword(@A int,@B int,@C int output)


As
Begin
set @c=@A+@B

End
/**

Declare @C int
Exec USP_OutputKeyword 333,7487 ,@C output
select @c

**/

5.Call SP in another SP

alter Proc USP_subtraction(@A INT,@B int)


As
Begin

Select @A-@B as Subtraction

Exec USP_addition @A,@B


Exec USP_Multi @a,@B

End

/***

exec USP_subtraction 777,74737

**/

You might also like