查询SQL Server数据库时如何获取sysobjects的模式名称

时间:2014-05-24 01:10:31

标签: c# .net sql-server-2012

我需要在数据库中查询所有存储过程的名称和模式名称。

这是我到目前为止所得到的,但我不知道如何获取模式名称。

查询sysobjects表时架构名称的字段名是什么?

  private DataTable GetProcedures()
    {
        var table = new DataTable();

        string sql = "";

        sql = "select name,'' as type ";
        sql += " From sysobjects ";
        sql += " where type = 'P' ";
        sql += " and name not like 'dt_%'  ";

        sql += " order by name asc";

        using (SqlConnection conn = new SqlConnection(cs))
        {
            conn.Open();

            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;

                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {

                    da.Fill(table);
                }
            }
        }
        return table;
    }

2 个答案:

答案 0 :(得分:1)

使用加入sys.objects表的sys.schemas表,如下所示:

SELECT schemas.name, objects.name
FROM sys.objects
JOIN sys.schemas ON schemas.schema_id = objects.schema_id

所以要查找所有存储过程(以查询为开头),它看起来像这样:

SELECT schemas.name, objects.name
FROM sys.objects
JOIN sys.schemas ON schemas.schema_id = objects.schema_id
WHERE type = 'P'
AND objects.name NOT LIKE 'dt_%'

作为附加信息,您不应再使用sysobjects,因为它已替换为sys.objects,其他sys*表也是如此。

答案 1 :(得分:0)

也许这可以帮到你:

SELECT name AS object_name 
  ,SCHEMA_NAME(schema_id) AS schema_name
  ,type_desc
  ,create_date
  ,modify_date
FROM sys.objects
WHERE type = 'P'
ORDER BY name asc;