The Queries in this page solved at the library database. It is a sample database generated by randomly records. You can download it from the link. You will see more than 500 examples in the future. You can filter from the search panel above by selecting sql statements. I hope you will like it

SQL Declare Examples

1-) Calling a Stored Procedure To Add Two Numbers With Input Output Parameters Example

Solution 1)

Create Procedure AddTwoNumber(@p1 int,@p2 int,@Result int output)
as
Begin
	Set @Result = @p1+ @p2
End
--To Execute The Procedure
Declare @r int
Execute AddTwoNumber 20,25,@r output
Select @r as Result


ETİKETLER

Procedure - Declare -
2-) The Stored Procedure That Selects Random Number Between the Two Numbers Entered as Parameters In Sql

Solution 1)

Create Procedure Sp_Random_Value 
@first int,
@second int,
@result int output
As
Begin
     Set @result =Floor(RAND() * (@second-@first))+@first
End
--To Execute the Procedure
Declare @r int
Execute Sp_Random_Value 20,30,@r output
Select @r


ETİKETLER

Procedure - Declare -
3-) Generate Unique Random Numbers In Sql With Stored Procedure

Solution 1)

Create procedure randomGenerate(@unit int,@min int,@max int)
as 
Begin
	Declare @numbers table(number int)
	Declare @i int = 0
	Declare @number int
	while (@i<@unit)
	Begin
		Set @number = floor(rand()*(@max-@min+1))+@min
		if(not exists(Select * from @numbers where number = @number))
		begin
			insert into @numbers values(@number)
			Set @i = @i + 1 
		end
	end
	Select * from @numbers order by 1
End
--To Execute Stored Procedure
Execute randomGenerate 5,20,30


ETİKETLER

Select - Insert - Procedure - Declare -
4-) Calculating Factorial of Given Number with Stored Procedure

Solution 1)

Create procedure factor(@number int)
as begin
Declare @i int = 1,@result int=1
while (@i<=@number)
Begin
	Set @result = @result * @i
	Set @i += 1
End
Select @result
End


ETİKETLER

Procedure - Declare -
5-) Create A Stored Procedure Calculate Power Of A Number In Sql

Solution 1)

Create Procedure myPower(@num int,@pow int, @result int output) As
Begin 
	Declare @i int = 0;
	Set @result = 1
	while(@i<@pow)
	Begin
		Set @result = @result * @num
		Set @i += 1
	End
End


ETİKETLER

Procedure - Declare -
6-) Finding The Sum Of The Three Numbers With Stored Procedure

Solution 1)

Create Procedure sumThree
@n1 int,
@n2 int,
@n3 int,
@result int output as
Begin
    Set @result = @n1+@n2+@n3
End
--To Execute The Procedure
Declare @result int 
Execute sumThree 5,30,12,@result output
Select @result


ETİKETLER

Procedure - Declare -
7-) Stored Procedure To Find A Number Is Prime In Sql

Solution 1)

Create Procedure sp_isPrime (@number int,@result bit output) as
Begin
	Set @result = 1
	Declare @i int = 2
	While (@i<@number)
	Begin
		if(@number % @i = 0)
		Begin
			Set @result = 0
			break
		End
		Set @i += 1
	End
	return @result
End
--To Execute The Stroed Procedure
Declare @result bit
Execute sp_isPrime 11,@result output
Select @result


ETİKETLER

Procedure - Declare -
8-) Stored Procedure To Divide Numbers And Get Divided Value And Remaining Value

Solution 1)

Create Procedure sp_Devide(@n1 int,@n2 int,@division int output,@remaining int output) as
Begin
	SEt @division = 0
	While(@n2<=@n1)
	Begin
		Set @n1 = @n1-@n2
		Set @division+=1
	End
	Set @remaining = @n1
End
--To Execute the Stored Procedure
Declare @d int,@r int
Execute sp_Devide 34,7,@d output,@r output
Select @d Division,@r Remaining


ETİKETLER

Procedure - Declare -
Library Database
Database