I am going to find gcd of two integer numbers by using SQL stored procedure with recursion query. But I got stuck in a loop and I really don't know why, here is my code:
create or alter procedure sp_gcd
@s1 int, @s2 int
as
begin
if (@s1 % @s2 = 0)
print @s2
else if (@s2 % @s1 = 0)
print @s1
else
begin
while (@s2 % @s1 != 0)
begin
declare @temp int = @s2 % @s1
exec sp_gcd @temp, @s1
end
print @s1
end
end
exec sp_gcd 15,21
And this is the result:
3
3
3
3
3
3
3
3
3
...
I just start working with SQL so I will very much appreciate if anyone could help, thanks a lot.
sp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!sp_prefix