Here is a example to create a stored procedure for finding number is perfect square or not.
CREATE PROCEDURE SQR (@NUM int) --syntax for creating procedure and assigning a variable to accept some number.
as begin-- Stored procedure shud start with AS BEGIN and should end with END command.
DECLARE @RESULT FLOAT
SET @RESULT =0
DECLARE @DIV INT
SET @DIV =0
WHILE (@DIV!=@NUM )
BEGIN
SET @RESULT= SQRT (@NUM)
IF (@RESULT =@DIV)
BEGIN
BREAK --- BREAK COMMAND IS USED TO BREAK THE WHILE LOOP ONCE CONDITION ABOVE BECOMES TRUE.
END
ELSE
SET @DIV = @DIV +1
END
IF (@RESULT=@DIV)
BEGIN
PRINT ('THE NUMBER IS SQUARE')
END
ELSE
PRINT ('THE NUMBER IS NOT SQUARE')
END
After running the above stored Procedure, We can check the output by using
EXEC SQR 81
where we can change value of 81 as we wish to do.
Comments
Post a Comment