从参数化动态查询中获取参数类型

时间:2016-02-24 16:17:56

标签: c# sql sql-server

我正在编写一个接口来提供有关查询的最终用户输入。 管理员定义查询,例如:

SELECT col1, col2, col3, col4 + col5 AS sumcol
FROM mytable
WHERE col10 = @parm1 AND somethingelse > @parm2

现在,我可以在C#中解析该查询,获取所有返回的列名

using (myReader = cmd.ExecuteReader(CommandBehavior.KeyInfo))
{
    //Retrieve column schema into a DataTable.
    schemaTable = myReader.GetSchemaTable();

    //For each field in the table...
    foreach (DataRow field in schemaTable.Rows)
    {
        // Display one-liner of important fields
        Console.WriteLine("{0} - Base Table: [{1}]  Data Type: [{2}]", 
            field[schemaTable.Columns["ColumnName"].ToString()].ToString(),
            field[schemaTable.Columns["BaseTableName"].ToString()].ToString(),
            field[schemaTable.Columns["DataType"].ToString()].ToString()
            );
    }

但我也希望获得参数@ parm1和@ parm2所期望的数据类型。我可以解析字符串ok以获取参数名称,但我想在最终用户输入框中显示2个参数,并在执行查询以获取结果之前检查它们是否是正确的类型。我没有必要检查特定的列名,例如它总是colx = @ parm1然后我可以检查colx的类型并假设@ parm1将是相同的类型,但该子句可能通常更多比这更复杂。 我无法控制管理员创建的查询,但我想尝试尽可能为最终用户提供证据,提示他们输入正确的参数类型,因为我无法确定管理员将为参数提供合理的名称,以指示查询所期望的内容。当我将查询传递给SqlServer以获取返回列类型时,它是否也可以告诉我参数类型可能是什么? 我希望这是有道理的。

1 个答案:

答案 0 :(得分:0)

如果您使用的是SQL Server 2012及更高版本,则可以使用sp_describe_undeclared_parameters存储过程来猜测参数类型。

例如,在Northwind示例数据库上执行以下查询:

EXEC sp_describe_undeclared_parameters 
        N'SELECT * FROM Customers WHERE CustomerID = @CustomerID'

给您以下结果:

+-------------------+-------------+--------------------------+----------------------------+----------------------+---------------------+-----------------+------------------------+------------------------------+----------------------------+--------------------------+----------------------------------------+-----------------------------+-----------------------------------+---------------------------------+-------------------------------+---------------------------+-----------------------------+------------------------------------+--------------------+---------------------+-----------------------+-----------------------+----------------------+
| parameter_ordinal |    name     | suggested_system_type_id | suggested_system_type_name | suggested_max_length | suggested_precision | suggested_scale | suggested_user_type_id | suggested_user_type_database | suggested_user_type_schema | suggested_user_type_name | suggested_assembly_qualified_type_name | suggested_xml_collection_id | suggested_xml_collection_database | suggested_xml_collection_schema | suggested_xml_collection_name | suggested_is_xml_document | suggested_is_case_sensitive | suggested_is_fixed_length_clr_type | suggested_is_input | suggested_is_output | formal_parameter_name | suggested_tds_type_id | suggested_tds_length |
+-------------------+-------------+--------------------------+----------------------------+----------------------+---------------------+-----------------+------------------------+------------------------------+----------------------------+--------------------------+----------------------------------------+-----------------------------+-----------------------------------+---------------------------------+-------------------------------+---------------------------+-----------------------------+------------------------------------+--------------------+---------------------+-----------------------+-----------------------+----------------------+
|                 1 | @CustomerID |                      239 | nchar(5)                   |                   10 |                   0 |               0 | NULL                   | NULL                         | NULL                       | NULL                     | NULL                                   | NULL                        | NULL                              | NULL                            | NULL                          |                         0 |                           0 |                                  0 |                  1 |                   0 | NULL                  |                   239 |                   10 |
+-------------------+-------------+--------------------------+----------------------------+----------------------+---------------------+-----------------+------------------------+------------------------------+----------------------------+--------------------------+----------------------------------------+-----------------------------+-----------------------------------+---------------------------------+-------------------------------+---------------------------+-----------------------------+------------------------------------+--------------------+---------------------+-----------------------+-----------------------+----------------------+