存储过程的架构

我有一个程序,我想阅读程序的架构。 要检索视图模式,我使用此处显示的查询。 同样的方式我想获得存储过程的模式。 怎么弄呢? Plz显示了一些语法。

public static DataTable SchemaReader(string tableName) { string sql = string.Format("Select * from {0}", tableName); conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.CommandType = CommandType.Text; SqlDataReader reader = cmd.ExecuteReader(); DataTable schema = reader.GetSchemaTable(); reader.Close(); conn.Close(); return schema; } 

如果有任何查询请问。请提前谢谢。

你能做到的

 public static DataTable SchemaReader(string tableName) { string sql = "MySP";//replace this with your store procedure name conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.CommandType = CommandType.StoredProcedure; SqlDataReader reader = cmd.ExecuteReader(); DataTable schema = reader.GetSchemaTable(); reader.Close(); conn.Close(); return schema; } 

希望这有帮助

这是一个不会调用SP的答案 – 如果这样做,您可能会无意中影响数据

 SELECT * FROM sys.dm_exec_describe_first_result_set ('owner.sprocName', NULL, 0) ; 

这将返回结果集:

 is_hidden column_ordinal name is_nullable system_type_id system_type_name max_length precision scale collation_name user_type_id user_type_database user_type_schema user_type_name assembly_qualified_type_name xml_collection_id xml_collection_database xml_collection_schema xml_collection_name is_xml_document is_case_sensitive is_fixed_length_clr_type source_server source_database source_schema source_table source_column is_identity_column is_part_of_unique_key is_updateable is_computed_column is_sparse_column_set ordinal_in_order_by_list order_by_is_descending order_by_list_length error_number error_severity error_state error_message error_type error_type_desc 

您可以获取有关存储过程参数的信息,但是,如果不执行它,SQL Server就无法告诉您存储过程返回的数据集的结构。 由于执行存储过程可能会产生副作用,因此ADO.NET不提供一种方法来告诉您执行存储过程时结果集的外观。 此外,结果集可能会根据执行时传递给过程的参数而改变。

我没有清楚地提出你的问题我认为这对你有用

 Select * from sys.objects where type='p' and name = (procedure name) 

用这个替换您的查询,它将正常工作

我创建了各种使用存储过程输出的代码生成器。 根据我的经验,如果您使用null(DbNull.Value)作为所有参数的值来调用它们,那么大多数SELECT任何输出其模式的过程都是相同的。 您可以从系统视图中获取参数列表本身,但我发现使用INFORMATION_SCHEMA.PARAMETERS很方便。

通过在事务中执行该过程并始终回滚,即使您不知道该过程的作用,也可以安全地执行。

您可能需要一个基本的GUI并允许用户修改参数 – 或配置文件或其他方式为特定过程提供参数值。 存储过程可能会根据参数生成具有不同模式的输出,但我没有看到很多这样做。