[dba-SQLServer] Recursive Queries in Microsoft SQL Server 2008

Gustav Brock gustav at cactus.dk
Mon Oct 28 08:28:18 CDT 2013


Hi all

I'm a noob when it comes to T-SQL - try to limit myself to tables and views
- so could someone please explain what is going on here?
What does this WITH mean?
How would you retrieve the data from Access?
Is "vParent = null" valid code in T-SQL? Why not "vParent is null"?

<SQL>
WITH security_menu_Recursive(Parent,MenuId,MenuName,LEVEL)
AS
(
    SELECT vparent,vmenuid,vmenuname,0 AS LEVEL FROM dbo.SecurityMenu WHERE
vParent = null
    UNION ALL
    SELECT vparent,vmenuid,vmenuname,Level + 1 AS LEVEL FROM
dbo.SecurityMenu
    INNER JOIN security_menu_Recursive AS smr ON smr.menuid =
dbo.SecurityMenu.vParent
)
SELECT parent,menuid,menuname,LEVEL FROM security_menu_Recursive
</SQL>

The full tip is here:

http://www.codeproject.com/Articles/674287/Recursive-Queries-in-Microsoft-SQ
L-Server-2008

/gustav



More information about the dba-SQLServer mailing list