存储过程可以具有要在“IN”过程中使用的动态参数吗? 条款?

发布于 2024-07-24 11:03:26 字数 278 浏览 4 评论 0原文

我想运行这样的查询:

 SELECT * FROM Studio WHERE Id IN (134, 144, 132, 138, 7432, 7543, 2566)

但是传递给 IN 子句的 Id 数量仅在运行时确定。

我必须使用动态 SQL 还是可以通过存储过程来完成?

更新: 如果任一选项可用,哪一个更好?

谢谢。

I want to run a query like this:

 SELECT * FROM Studio WHERE Id IN (134, 144, 132, 138, 7432, 7543, 2566)

but the amount of Id's passed to the IN clause is only determined at runtime.

Do I have to use dynamic SQL or can this be done with a stored procedure?

UPDATE:
If either option is available, which one is better?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

眼眸 2024-07-31 11:03:26

根据您的 Sql Server 版本,您可以通过两种不同的方式之一执行此操作。

对于 Sql 2000/2005,您可以使用具有分隔 ID 列表的参数(varchar 类型)。 创建一个 UDF 来解析 varchar 并返回包含项目的表。 然后使您的 IN 子句与表相反(即 ...IN (Select ID FROM @ReturnTable))。

下面是 UDF 内容的示例:
http://pietschsoft.com/post /2006/02/03/T-SQL-Parse-a-delimited-string.aspx

对于 Sql 2008,你可以做同样的事情; 然而,您可以直接进入正题并传入 Table 参数,而不是传入 varchar 参数。 IN 子句仍然有一个子查询,但它的工作原理是一样的。 或者,一旦有了表,您就可以对其进行内部联接并避免对 IN 子句的需要。

编辑:添加了用于解析分隔字符串链接的 UDF。

Depending on your version of Sql Server, you can do this one of two different ways.

For Sql 2000/2005, you can use a parameter (type varchar) that has a delimited list of IDs. Create a UDF that would parse the varchar and return a table containing the items. Then make your IN clause go against the table (i.e. ...IN (Select ID FROM @ReturnTable)).

Here's an example of what the contents of the UDF would look like:
http://pietschsoft.com/post/2006/02/03/T-SQL-Parse-a-delimited-string.aspx

For Sql 2008, you can do the same thing; however instead of passing in a varchar parameter you can just cut to the chase and pass in a Table parameter. The IN clause would still have a subquery but it would work all the same. Alternatively, once you have the table you can just do an Inner Join on it and circumvent the need for the IN clause.

EDIT: added UDF for parsing a delimited string link.

风吹短裙飘 2024-07-31 11:03:26

此处描述的解决方案:

SQL Server 2005 中的数组和列表

SQL Server MVP Erland Sommarskog 的 SQL 文本

http://www.sommarskog.se/arrays-in-sql-2005.html

Solution described here:

Arrays and Lists in SQL Server 2005

An SQL text by Erland Sommarskog, SQL Server MVP

http://www.sommarskog.se/arrays-in-sql-2005.html

泛泛之交 2024-07-31 11:03:26

您绝对可以在存储过程中执行此操作。

在存储过程中创建一个临时表,并插入以逗号或任何分隔符分隔的值,然后执行此操作,

SELECT * FROM Studio WHERE Id IN (select id from temptable)

然后删除该表。

You can absolutely do this in a stored procedure.

create a temp table inside the stored procedure and insert the values split on the commas or any delimiter then do this

SELECT * FROM Studio WHERE Id IN (select id from temptable)

Then delete the table.

〗斷ホ乔殘χμё〖 2024-07-31 11:03:26

这是我自 MSSQL 2000 以来一直在使用的 UDF。我在某处找到了它 - 抱歉,不记得在哪里了。

基本上,您可以对 UDF 进行联接,其中第一个参数是分隔字符串,第二个参数是分隔符。

SELECT t1.somecolumn FROM sometable t1 INNER JOIN dbo.Split(@delimitedVar, ',') t2 ON t1.ID = t2.Element

CREATE FUNCTION [dbo].[Split]
(
@vcDelimitedString varchar(max),
@vcDelimiter varchar(100)
)
RETURNS @tblArray TABLE
   (
    ElementID smallint  IDENTITY(1,1), --Array index
    Element varchar(1000) --Array element contents
   )
AS
BEGIN
    DECLARE @siIndex smallint, @siStart smallint, @siDelSize smallint
    SET @siDelSize  = LEN(@vcDelimiter)
    --loop through source string and add elements to destination table array
    WHILE LEN(@vcDelimitedString) > 0
    BEGIN
        SET @siIndex = CHARINDEX(@vcDelimiter, @vcDelimitedString)
        IF @siIndex = 0
        BEGIN
            INSERT INTO @tblArray VALUES(@vcDelimitedString)
            BREAK
        END
        ELSE
        BEGIN
            INSERT INTO @tblArray VALUES(SUBSTRING(@vcDelimitedString, 1,@siIndex - 1))
            SET @siStart = @siIndex + @siDelSize
            SET @vcDelimitedString = SUBSTRING(@vcDelimitedString, @siStart , LEN(@vcDelimitedString) - @siStart + 1)
        END
    END
    RETURN
END

Here is a UDF that I've been using since MSSQL 2000. I found this somewhere - sorry, can't remember where.

Basically, you can do a join on the UDF, where the first param is the delimited string, and the second param is the delimiter.

SELECT t1.somecolumn FROM sometable t1 INNER JOIN dbo.Split(@delimitedVar, ',') t2 ON t1.ID = t2.Element

CREATE FUNCTION [dbo].[Split]
(
@vcDelimitedString varchar(max),
@vcDelimiter varchar(100)
)
RETURNS @tblArray TABLE
   (
    ElementID smallint  IDENTITY(1,1), --Array index
    Element varchar(1000) --Array element contents
   )
AS
BEGIN
    DECLARE @siIndex smallint, @siStart smallint, @siDelSize smallint
    SET @siDelSize  = LEN(@vcDelimiter)
    --loop through source string and add elements to destination table array
    WHILE LEN(@vcDelimitedString) > 0
    BEGIN
        SET @siIndex = CHARINDEX(@vcDelimiter, @vcDelimitedString)
        IF @siIndex = 0
        BEGIN
            INSERT INTO @tblArray VALUES(@vcDelimitedString)
            BREAK
        END
        ELSE
        BEGIN
            INSERT INTO @tblArray VALUES(SUBSTRING(@vcDelimitedString, 1,@siIndex - 1))
            SET @siStart = @siIndex + @siDelSize
            SET @vcDelimitedString = SUBSTRING(@vcDelimitedString, @siStart , LEN(@vcDelimitedString) - @siStart + 1)
        END
    END
    RETURN
END
孤芳又自赏 2024-07-31 11:03:26

在 SQL 2008 中,您可以使用表值参数

在 SQL 2005 中,您必须使用动态 SQL,除非您希望将列表作为 XML 传递并在过程中使用 XML 处理将 XML 分解回表变量中。

In SQL 2008 you can use a table valued parameter.

In SQL 2005 you must use dynamic SQL unless you want to pass the list as XML and use XML processing in the procedure to shred the XML back into a table variable.

兲鉂ぱ嘚淚 2024-07-31 11:03:26

声明一个 @temp 表并将值拆分到其中。 那么你可以

从 Studio 的内部连接中选择 *
@诱惑TB
上 s.ID=tb.ID

declare a @temp table and split the values into it. then you could do

select * from Studio s inner join
@temptable tb
on s.ID=tb.ID

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文