如何参数设定为使用内联SQL的报表? [英] How to parameterize the set for an IN statement using inline SQL?

查看:145
本文介绍了如何参数设定为使用内联SQL的报表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  一个参数化SQL IN子句?

您好,我有一个查询是这样的:

Hi, I have a query looks like this:

SELECT 
    CompanyId 
FROM 
    CompanyTable 
WHERE 
    CompanyName IN ('Subway', 'A & W', 'Pizzahut')

有什么办法,我可以使用SQL参数名称列表?

Is there any way I can use sql parameters for the names list?

这是不是一个存储过程(我preFER但在这个项目中不能使用)。当我说'参数',我指的是在参数化的内联SQL参数。

This is not a stored proc (which I prefer but can't use in this project). When I say 'parameter', I mean parameter in the parametrized inline sql.

我使用MS企业库,所以我的参数化的内联SQL看起来是这样的:

I use MS Enterprise Library so my parametrized inline sql looks like this:

string sql = "SELECT * FROM Company WHERE CompanyID = @companyId";
Database db = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = db.GetSqlStringCommand(sql);
db.AddInParameter(dbCommand, "companyId", DbType.Int32, 123);
...

这是pretty的直白简单的情况下,像上面。但是,当涉及到像

It is pretty straightforward for simple cases like above. But when it comes to something like

SELECT 
    CompanyId 
FROM 
    CompanyTable 
WHERE 
    CompanyName IN ('Subway','A & W','Pizzahut').

我不知道如何在这里使用的参数。

I have no idea how to use parameters here.

推荐答案

有几条路线,你可以采取

There are a few routes that you can take

  • 经典的传递一个分隔字符串参数和使用用户定义的表值函数在数据库中把字符串到一个表。然后你就可以在该表的连接过滤。
  • The "classic" pass in a delimited string parameter and use a user defined table-valued function in the database to turn the string into a table. Then you can join on that table to filter.

喜欢的东西(这将返回 INT 的数据类型的表,只是改变了code略有 VARCHAR

Something like (this will return a table of INT datatypes, just change the code slightly for VARCHAR

CREATE function [dbo].[csl_to_table] ( @list nvarchar(MAX) )
RETURNS @list_table TABLE ([id] INT)
AS
BEGIN
    DECLARE     @index INT,
                @start_index INT,
                @id INT

    SELECT @index = 1 
    SELECT @start_index = 1
    WHILE @index <= DATALENGTH(@list)
    BEGIN

        IF SUBSTRING(@list,@index,1) = ','
        BEGIN

                SELECT @id = CAST(SUBSTRING(@list, @start_index, 
                        @index - @start_index ) AS INT)
                INSERT @list_table ([id]) VALUES (@id)
                SELECT @start_index = @index + 1
        END
        SELECT @index  = @index + 1
    END
    SELECT @id = CAST(SUBSTRING(@list, @start_index, 
            @index - @start_index ) AS INT)
    INSERT @list_table ([id]) VALUES (@id)
    RETURN
END

  • 您可以使用一个循环在.NET code建条款。但是请记住,您被限制为256个参数(IIRC)。
    • You can use a loop in your .NET code to build the IN clause. Remember however that you are limited to 256 parameters (IIRC).
    • 像这样的东西(一拉这个答案

      string[] tags = new string[] { "Subway","A & W","Pizzahut" };
      string cmdText = 
          "SELECT CompanyId FROM CompanyTable WHERE CompanyName IN ({0})";
      
      string[] paramNames = tags.Select(
          (s, i) => "@tag" + i.ToString()
      ).ToArray();
      
      string inClause = string.Join(",", paramNames);
      using (SqlCommand cmd = new SqlCommand(string.Format(cmdText, inClause))) {
          for(int i = 0; i < paramNames.Length; i++) {
             cmd.Parameters.AddWithValue(paramNames[i], tags[i]);
          }
      }
      

      • 根据SQL的版本,可以使用 <一个href="http://weblogs.asp.net/jgalloway/archive/2007/02/16/passing-lists-to-sql-server-2005-with-xml-parameters.aspx"相对=nofollow> XML数据类型参数 (SQL服务器2005年起),以传递多个参数或表值参数 (SQL服务器2008年起)
      • 这篇关于如何参数设定为使用内联SQL的报表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆