简体   繁体   English

来自存储过程的架构

[英]Schema from stored procedure

I have a procedure, I want to read schema of the procedure.我有一个程序,我想读取程序的架构。 To retrieve view schema I use the query shown here.要检索视图架构,我使用此处显示的查询。 Same way I want to get schema of stored procedure.我想以同样的方式获取存储过程的架构。 How to get it?如何得到它? Plz show some syntax.请展示一些语法。

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;
}       

If have any query plz ask.Thanks in advance.如果有任何疑问请询问。提前致谢。

you could do你可以做

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; 
}

Hope this help希望这有帮助

This is an answer that does not call the SP - if you do, you may inadvertently affect data :这是一个不调用 SP 的答案 -如果这样做,您可能会无意中影响数据

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

This returns the result set:这将返回结果集:

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

You could get information about a stored procedure's parameters but, without executing it, SQL Server cannot tell you the structure of the dataset(s) returned by the stored procedure.您可以获得有关存储过程参数的信息,但如果不执行它,SQL 服务器无法告诉您存储过程返回的数据集的结构。 Since executing a stored procedure can have side effects, ADO.NET doesn't provide a method for telling you what the result set(s) would look like were the stored procedure to be executed.由于执行存储过程可能会产生副作用,因此 ADO.NET 没有提供一种方法来告诉您要执行的存储过程会是什么样子的结果集。 Furthermore, the result set(s) might change depending on the parameters passed to the procedure when it is executed.此外,结果集可能会根据执行时传递给过程的参数而改变。

I've created various code generators that use the output of stored procs.我创建了各种使用存储过程的 output 的代码生成器。 In my experience, most procedures that SELECT anything output their schema just the same if you call them with null (DbNull.Value) as the value for all parameters.根据我的经验,大多数程序 SELECT 任何 output 如果您使用 null (DbNull.Value) 作为所有参数的值来调用它们,它们的架构就相同。 You can get the parameter list itself from system views, though I find it convenient to use INFORMATION_SCHEMA.PARAMETERS.您可以从系统视图中获取参数列表本身,但我发现使用 INFORMATION_SCHEMA.PARAMETERS 很方便。

By executing the procedure in a transaction and always rolling back you can safely execute stuff even when you have no idea what the procedure does.通过在事务中执行该过程并始终回滚,即使您不知道该过程的作用,您也可以安全地执行内容。

You'll probably need a basic GUI and allow the user to modify the parameters - or a config file or some other way to provide parameter values for specific procedures.您可能需要一个基本的 GUI 并允许用户修改参数 - 或配置文件或其他方式来为特定过程提供参数值。 A stored proc may produce output with different schemas depending on the parameters, though I haven't seen many that do.存储过程可能会根据参数生成具有不同架构的 output,尽管我没有看到很多这样做的。

I am not getting your question clearly I think this would work with you我没有清楚地得到你的问题我认为这对你有用

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

Replace your query with this and it will work fine用这个替换你的查询,它会正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM