If you are ever trying to write SQL to access SQL server's system tables, it is often useful to look at SQL Server's own system stored procedures. You can do this by simply running EXEC
sp_helptext system_proc_name (where
system_proc_name is the name of the stored procedure you want see the SQL for) from the master database.
For example, EXEC sp_helptext sp_help will show the SQL executed by the system stored procedure sp_help, which returns information about an object, such as a table, view, or stored procedure.