INSERTING RECORDS USING SPS
create procedure Insert_Empp(
@ID int,
@name varchar(10),
@salary int
)
As
BEGIN
INSERT INTO dbo.Empp(ID,name,salary)values
(@ID,
@name,
@salary)
END
RETRIEVING RECORDS
create procedure select_Empp(
@Id int,
@name varchar(10),
@salary int
)
As
Begin
End
select * from Empp
Getting details by ID
procedure with output parameter
create procedure pro5(@AB int,@BC int,@CD int,@DE int)
as
begin
declare @abc int
set @abc=@DE+@AB+@BC+@CD
print 'RESULT AS '+cast(@abc as varchar)
end
EXEC pro5 200,300,500,600
SIMPLE EXAMPLES
--simple stored procedure
create procedure simmple_proc
as
begin
print'hi yjreddy doing first procedure'
End
Exec simmple_proc
--second one
create procedure Secondonewithparameters(@x int,@y int)
as
begin
declare @z int
set @z=@x+@y
print 'The sum of two nos is'+cast(@z as varchar)
End
EXEC Secondonewithparameters 10,20
EXEC Secondonewithparameters 50,100
create procedure third(@a int,@b int,@c int)
AS
BEGIN
DECLARE @d int
set @d=@a+@b+@c
print 'result is '+cast(@d as varchar)
END
create procedure proc4(@e int,@f int,@g int)
as
begin
declare @v int
set @v=@e+@f+@g
print 'result as '+cast(@v as varchar)
end
EXEC proc4 100,200,300
--procedure with output parameter
create procedure pro5(@AB int,@BC int,@CD int,@DE int)
as
begin
declare @abc int
set @abc=@DE+@AB+@BC+@CD
print 'RESULT AS '+cast(@abc as varchar)
end
EXEC pro5 200,300,500,600
DELETE RECORD USING STOREDPROCEDURE
--Delete recorb by id
create procedure Delete_procedure(@ID int,@title nvarchar(10),@averageSalary int)
as
begin
delete from EMPJOB where ID=@ID
end
--updating records by sps
create procedure update_emp11(@ID int,@title nvarchar(10),@averageSalary int)
as
begin
update EMPJOB set title=@title , Id=@id where ID=@ID
end
EXEC update_emp11 107,'yjreddy',50000
--create procedure for inserting checking null values
create procedure condition_nulls(@Id int,
@title nvarchar(10),
@averageSalary int
)
as
begin
if @Id is null
Begin
print'dont enter null it wont accept'
Return
end
insert into EMPJOB(ID,title,averageSalary) values(
@Id,
@title,
@averageSalary
)
end
--create procedure for inserting checking null values and restricting redundant values
--create procedure for inserting checking null values and restricting redundant values
--and salary should be more than 25000
Create procedure condition_nullsanduniquevalues(@Id int,
@title nvarchar(10),
@averageSalary int
)
as
begin
if @Id is null
Begin
print'dont enter null it wont accept'
Return
end
if EXISTS(select * from EMPJOB where ID=@Id)
begin
print 'emp id should be unique '
return
end
if @averageSalary>25000
begin
print'salary should be high'
Return
end
insert into EMPJOB(ID,title,averageSalary) values(
@Id,
@title,
@averageSalary
)
end
sp for send the amount from one customer to another in same table
create Table bank
(
B_id int,
Balance Money,
Address varchar(50)
)
alter table bank
add Name varchar(50)
select * from bank
insert into bank values(106,38000,'Gnt','Ankireddy')
--stored procedure for trnasferring amount
create procedure Funds_Transfer
(@srcid int,
@Destid int,
@Balamt money)
as
begin
update bank set Balance=Balance-@Balamt where B_id=@srcid
update bank set Balance=Balance+@Balamt where B_id=@Destid
ENd
---a procedure can cause error
create procedure proc123(@X int,@y int)
as
begin
declare @z int
set @z=@X/@y
print 'the output is '+cast(@z as varchar)
End
EXEC proc123 200,0
-- procedure with try catch block
create procedure trycatch(@x int,@y int)
as
begin
Begin try
Declare @z int
set @z=0
set @z=@x/@y
print 'the output is'+cast(@z as varchar)
end try
begin catch
print Error_message()
end catch
end
EXEC trycatch 100,20
EXEC trycatch 500,50
create procedure Insert_Empp(
@ID int,
@name varchar(10),
@salary int
)
As
BEGIN
INSERT INTO dbo.Empp(ID,name,salary)values
(@ID,
@name,
@salary)
END
RETRIEVING RECORDS
create procedure select_Empp(
@Id int,
@name varchar(10),
@salary int
)
As
Begin
End
select * from Empp
Getting details by ID
create procedure Showdetails
(
@ID int
)
As
Begin
select name,salary from Empp
where ID=@ID
End
EXEC Showdetails 3
procedure with output parameter
create procedure pro5(@AB int,@BC int,@CD int,@DE int)
as
begin
declare @abc int
set @abc=@DE+@AB+@BC+@CD
print 'RESULT AS '+cast(@abc as varchar)
end
EXEC pro5 200,300,500,600
SIMPLE EXAMPLES
--simple stored procedure
create procedure simmple_proc
as
begin
print'hi yjreddy doing first procedure'
End
Exec simmple_proc
--second one
create procedure Secondonewithparameters(@x int,@y int)
as
begin
declare @z int
set @z=@x+@y
print 'The sum of two nos is'+cast(@z as varchar)
End
EXEC Secondonewithparameters 10,20
EXEC Secondonewithparameters 50,100
create procedure third(@a int,@b int,@c int)
AS
BEGIN
DECLARE @d int
set @d=@a+@b+@c
print 'result is '+cast(@d as varchar)
END
create procedure proc4(@e int,@f int,@g int)
as
begin
declare @v int
set @v=@e+@f+@g
print 'result as '+cast(@v as varchar)
end
EXEC proc4 100,200,300
--procedure with output parameter
create procedure pro5(@AB int,@BC int,@CD int,@DE int)
as
begin
declare @abc int
set @abc=@DE+@AB+@BC+@CD
print 'RESULT AS '+cast(@abc as varchar)
end
EXEC pro5 200,300,500,600
DELETE RECORD USING STOREDPROCEDURE
--Delete recorb by id
create procedure Delete_procedure(@ID int,@title nvarchar(10),@averageSalary int)
as
begin
delete from EMPJOB where ID=@ID
end
--updating records by sps
create procedure update_emp11(@ID int,@title nvarchar(10),@averageSalary int)
as
begin
update EMPJOB set title=@title , Id=@id where ID=@ID
end
EXEC update_emp11 107,'yjreddy',50000
--create procedure for inserting checking null values
create procedure condition_nulls(@Id int,
@title nvarchar(10),
@averageSalary int
)
as
begin
if @Id is null
Begin
print'dont enter null it wont accept'
Return
end
insert into EMPJOB(ID,title,averageSalary) values(
@Id,
@title,
@averageSalary
)
end
--create procedure for inserting checking null values and restricting redundant values
create procedure condition_nullsanduniquevalues(@Id int,
@title nvarchar(10),
@averageSalary int
)
as
begin
if @Id is null
Begin
print'dont enter null it wont accept'
Return
end
if EXISTS(select * from EMPJOB where ID=@Id)
begin
print 'emp id should be unique '
return
end
insert into EMPJOB(ID,title,averageSalary) values(
@Id,
@title,
@averageSalary
)
end
--create procedure for inserting checking null values and restricting redundant values
--and salary should be more than 25000
Create procedure condition_nullsanduniquevalues(@Id int,
@title nvarchar(10),
@averageSalary int
)
as
begin
if @Id is null
Begin
print'dont enter null it wont accept'
Return
end
if EXISTS(select * from EMPJOB where ID=@Id)
begin
print 'emp id should be unique '
return
end
if @averageSalary>25000
begin
print'salary should be high'
Return
end
insert into EMPJOB(ID,title,averageSalary) values(
@Id,
@title,
@averageSalary
)
end
sp for send the amount from one customer to another in same table
create Table bank
(
B_id int,
Balance Money,
Address varchar(50)
)
alter table bank
add Name varchar(50)
select * from bank
insert into bank values(106,38000,'Gnt','Ankireddy')
--stored procedure for trnasferring amount
create procedure Funds_Transfer
(@srcid int,
@Destid int,
@Balamt money)
as
begin
update bank set Balance=Balance-@Balamt where B_id=@srcid
update bank set Balance=Balance+@Balamt where B_id=@Destid
ENd
Exec Funds_Transfer 110,104,2000
---a procedure can cause error
create procedure proc123(@X int,@y int)
as
begin
declare @z int
set @z=@X/@y
print 'the output is '+cast(@z as varchar)
End
EXEC proc123 200,0
-- procedure with try catch block
create procedure trycatch(@x int,@y int)
as
begin
Begin try
Declare @z int
set @z=0
set @z=@x/@y
print 'the output is'+cast(@z as varchar)
end try
begin catch
print Error_message()
end catch
end
EXEC trycatch 100,20
EXEC trycatch 500,50
No comments:
Post a Comment