We will write a procedure that finds given number is Prime. The procedure gets an integer parameter and a bit parameter output. If the result parameter’s value is 0 (zero) then the written number is prime, if the result parameter’s value is 1 (one) then the written number is non-prime.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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 |
After running the code above we will get succesfull message. Then we can use the procedure like below.
Example 1
1 2 3 |
Declare @result bit Execute sp_isPrime 11,@result output Select @result |
Result: 1 (Prime)
Example 2
1 2 3 |
Declare @result bit Execute sp_isPrime 9,@result output Select @result |
Result: 0 (non-Prime)