I'm trying to learn advanced sql and how to use system queries (sql server). The below query is a little confusing.
CREATE PROC dbo.ShowHierarchy
(
@Root int
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @EmpID int, @EmpName varchar(30)
SET @EmpName = (SELECT EmpName FROM dbo.Emp WHERE EmpID = @Root)
PRINT REPLICATE('-', @@NESTLEVEL * 4) + @EmpName
SET @EmpID = (SELECT MIN(EmpID) FROM dbo.Emp WHERE MgrID = @Root)
WHILE @EmpID IS NOT NULL
BEGIN
EXEC dbo.ShowHierarchy @EmpID
SET @EmpID = (SELECT MIN(EmpID) FROM dbo.Emp WHERE MgrID = @Root AND EmpID > @EmpID)
END
END
GO
Taken from here:
http://vyaskn.tripod.com/hierarchies_in_sql_server_databases.htm
Every time the query runs, how is the @EmpId parameter incremented? Does it do itself? Also, does the @root increment on every recursion? E.g. CEO is root, go to immediate subordinate, that immediate subordinate is now @root, etc.
Thanks