TSQL
TSQL
Rules:
• T-SQL Syntax:
• Query:
• Syntax:
• Syntax:
• Just like SET, we can also use SELECT to assign values to the variables, post
declaring a variable using DECLARE. Below are different ways to assign a value
using SELECT:
• Syntax:
• Syntax:
if(@x>@y)
print ' X is Greater than y';
else
print'Y is Greater than x';
• DECLARE @G AS INT = 34;
IF(@G%2 = 0)
BEGIN
PRINT 'NUMBER IS EVEN';
END
ELSE
BEGIN
PRINT 'NUMBER IS ODD';
END
• declare @avgCGPA int, @studCGPA int;
• select @avgCGPA= avg(CGPA)from Students;
• select @studCGPA = CGPA from Students where STUDENT_ID=101;
• print @studCGPA;
• print @avgCGPA;
• if(@studCGPA> @avgCGPA)
• Begin
• print 'Abc has more CGPA then the average CGPA of students’;
• End
• Else
• BEGIN
• print'Student has more CGPA than the average CGPA of abc’;
• END
Declare @total int;
select @total= sum(P_price*QTY) from product;
Print @total;
if (@total>50000)
BEGIN
select * from product where discount<1000;
END
else
BEGIN
select * from product where discount>1000
declare @a int =1;
while @a<=50
BEGIN
if(@a%2==0)
BEGIN
print cast(@a as varchar)+'is even';
END
set @a=@a+1;
END
DECLARE @R int,@REV int=0,@N1 int;
SET @N1 = 28;
WHILE (@N1 > 0)
BEGIN
SET @R = @N1%10;
SET @REV = @REV*10+@R;
SET @N1= @N1/10;
Print 'reverse number is' +cast(@REV as varchar);
END
• ----------------WAP to build 1-5 multiplication table
DECLARE @X INT =1, @PRO INT =1, @I2 INT =1;
WHILE (@X <=5)
Begin
Set @X = @X+1;
set @I2 = 1;
END
create table s_details(id varchar(20),rollno int, section varchar(10));
declare @a int =1;
while @a<=10
BEGIN
insert into s_details values('stu-'+cast((100+@a) AS
varchar),@a,'JK902');
set @a=@a+1;
END
• CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
• SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN 'The quantity is
greater than 30'
WHEN Quantity = 30 THEN 'The quantity is 30'
ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;
Case statements
• Update stu_details
• Set branch=
• Case Branch
• when 'CSE' then 'Computer '
• when 'Electrical' then 'Electro'
• else 'Civil'
• End
• update Students
set BRANCH= Case Branch
when 'CSE' then 'Computer Science'
when 'Electrical' then'Electronics'
when 'Polytechnic' then 'Chemical Engg'
End
select * from Students;
• CASE
• WHEN ComparsionCondition THEN result
• WHEN ComparsionCondition THEN result
• ELSE other
• END
• select cust_name,age,gender from customer2 order by
• So if you have an SQL query that you write over and over again, save it
as a stored procedure, and then just call it to execute it.
Benefits of using a stored procedure
• It can be easily modified: We can easily modify the code inside the stored procedure without the
need to restart or deploying the application. For example, If the T-SQL queries are written in the
application and if we need to change the logic, we must change the code in the application and re-
deploy it. SQL Server Stored procedures eliminate such challenges by storing the code in the database.
so, when we want to change the logic inside the procedure we can just do it by simple ALTER
PROCEDURE statement.
• Reduced network traffic: When we use stored procedures instead of writing T-SQL queries at the
application level, only the procedure name is passed over the network instead of the whole T-SQL
code.
• Reusable: Stored procedures can be executed by multiple users or multiple client applications without
the need of writing the code again.
• Security: Stored procedures reduce the threat by eliminating direct access to the tables. we can also
encrypt the stored procedures while creating them so that source code inside the stored procedure is
not visible. Use third-party tools like ApexSQL Decrypt to decrypt the encrypted stored procedures.
• Performance: The SQL Server stored procedure when executed for the first time creates a plan and
stores it in the buffer pool so that the plan can be reused when it executes next time.
• CREATE PROCEDURE procedure_name
• AS
• sql_statement
• GO;
• EXEC procedure_name;
• CREATE PROCEDURE p11
AS
BEGIN
DECLARE @A INT=0, @B INT=1, @C INT=0, @D INT=0
PRINT'Fibonacci Series'
Print @a
print @b
WHILE @D<18
BEGIN
SET @C=@A+@B
PRINT @C
SET @D=@D+1
SET @A=@B
SET @B=@C
END
END
GO
EXEC P11;
• CREATE PROCEDURE p6
AS
BEGIN
DECLARE @I INT=1,@R INT =1,@N INT =5
WHILE(@I<=@N)
BEGIN
SET @R=@R*@I
SET @I+=1
END
PRINT @R
END
GO
EXEC P6;
• CREATE PROCEDURE Multi @X INT
AS
DECLARE @PRO INT =1, @I2 INT =1;
go
• CREATE PROCEDURE Factorialof7 @N int
as
declare @fact int =1, @i int =1;
while (@i<=@N)
begin
set @fact= @fact * @i
set @i= @i+1
end
print'Addition: ';
---------declare @d as int = 19, @e as int =83, @f as int =7 ;
declare @add int = @d+@e+@f;
print 'The Sum is: ' + cast (@add as varchar);
print'Subtraction: ';
--------declare @s as int =83, @t as int =7 ;
declare @sub int = @e - @d - @f;
print 'The difference is: ' + cast (@sub as varchar);
print'Multiplication: ';
---declare @x as int = 19, @y as int =83, @z as int =7 ;
declare @Product int = @e* @f * @d;
print 'The Product is: ' + cast (@Product as varchar);
go
exec calculator1 @d =19, @e =83, @f =7 ;
• 11
Create procedure proc1 @limit int
AS BEGIN
select * from products where price>@limit order by QTY exec proc1
2000
• alter proc1 products who are having price more than the min price but
less than the max
create PROCEDURE ProPrice @m1 as int ,@m2 as int
AS
BEGIN
select P_id, P_Name, P_Price, Qty from Product where P_Price >@m1 AND P_price<@m2
END
declare @m3 as int, @m4 as int
select @m3= min(P_price) from Product
select @m4= max(P_price) from Product
go
exec Commission @com =100;
• https://sqlserverguides.com/advanced-stored-procedure-examples-
in-sql-server/