如何将逗号分隔的列表传递给存储过程?

发布于 2024-07-04 03:09:14 字数 819 浏览 7 评论 0原文

因此,我有一个 Sybase 存储过程,它采用 1 个参数,该参数是逗号分隔的字符串列表,并在 IN() 子句中使用 in 运行查询:

CREATE PROCEDURE getSomething @keyList varchar(4096)
AS
SELECT * FROM mytbl WHERE name IN (@keyList)

How do I call my store proc with more than 1 value in the list? 到目前为止,我已经尝试

exec getSomething 'John'         -- works but only 1 value
exec getSomething 'John','Tom'   -- doesn't work - expects two variables
exec getSomething "'John','Tom'" -- doesn't work - doesn't find anything
exec getSomething '"John","Tom"' -- doesn't work - doesn't find anything
exec getSomething '\'John\',\'Tom\'' -- doesn't work - syntax error

编辑:我实际上发现了这个页面对将数组传递给存储过程的各种方法有很好的参考

So I have a Sybase stored proc that takes 1 parameter that's a comma separated list of strings and runs a query with in in an IN() clause:

CREATE PROCEDURE getSomething @keyList varchar(4096)
AS
SELECT * FROM mytbl WHERE name IN (@keyList)

How do I call my stored proc with more than 1 value in the list?
So far I've tried

exec getSomething 'John'         -- works but only 1 value
exec getSomething 'John','Tom'   -- doesn't work - expects two variables
exec getSomething "'John','Tom'" -- doesn't work - doesn't find anything
exec getSomething '"John","Tom"' -- doesn't work - doesn't find anything
exec getSomething '\'John\',\'Tom\'' -- doesn't work - syntax error

EDIT: I actually found this page that has a great reference of the various ways to pas an array to a sproc

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

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

发布评论

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

评论(11

左秋 2024-07-11 03:09:14

如果您使用的是 Sybase 12.5 或更早版本,则无法使用函数。 解决方法可能是用这些值填充临时表并从那里读取它们。

If you're using Sybase 12.5 or earlier then you can't use functions. A workaround might be to populate a temporary table with the values and read them from there.

想你的星星会说话 2024-07-11 03:09:14

关于 Kevin 将参数传递给将文本拆分为表格的函数的想法,这是我几年前对该函数的实现。 工作是一种享受。

在 SQL 中将文本拆分为单词

Regarding Kevin's idea of passing the parameter to a function that splits the text into a table, here's my implementation of that function from a few years back. Works a treat.

Splitting Text into Words in SQL

耳根太软 2024-07-11 03:09:14

您需要使用逗号分隔列表吗? 在过去的几年里,我一直在采用这种类型的想法并传递 XML 文件。 openxml“函数”采用一个字符串并使其像 xml 一样,然后如果您使用数据创建一个临时表,则它是可查询的。

DECLARE @idoc int
DECLARE @doc varchar(1000)
SET @doc ='
<ROOT>
<Customer CustomerID="VINET" ContactName="Paul Henriot">
   <Order CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00">
      <OrderDetail OrderID="10248" ProductID="11" Quantity="12"/>
      <OrderDetail OrderID="10248" ProductID="42" Quantity="10"/>
   </Order>
</Customer>
<Customer CustomerID="LILAS" ContactName="Carlos Gonzlez">
   <Order CustomerID="LILAS" EmployeeID="3" OrderDate="1996-08-16T00:00:00">
      <OrderDetail OrderID="10283" ProductID="72" Quantity="3"/>
   </Order>
</Customer>
</ROOT>'
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT    *
FROM       OPENXML (@idoc, '/ROOT/Customer',1)
            WITH (CustomerID  varchar(10),
                  ContactName varchar(20))

Do you need to use a comma separated list? The last couple of years, I've been taking this type of idea and passing in an XML file. The openxml "function" takes a string and makes it like xml and then if you create a temp table with the data, it is queryable.

DECLARE @idoc int
DECLARE @doc varchar(1000)
SET @doc ='
<ROOT>
<Customer CustomerID="VINET" ContactName="Paul Henriot">
   <Order CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00">
      <OrderDetail OrderID="10248" ProductID="11" Quantity="12"/>
      <OrderDetail OrderID="10248" ProductID="42" Quantity="10"/>
   </Order>
</Customer>
<Customer CustomerID="LILAS" ContactName="Carlos Gonzlez">
   <Order CustomerID="LILAS" EmployeeID="3" OrderDate="1996-08-16T00:00:00">
      <OrderDetail OrderID="10283" ProductID="72" Quantity="3"/>
   </Order>
</Customer>
</ROOT>'
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT    *
FROM       OPENXML (@idoc, '/ROOT/Customer',1)
            WITH (CustomerID  varchar(10),
                  ContactName varchar(20))
尐籹人 2024-07-11 03:09:14

将逗号分隔的列表传递到返回表值的函数中。 StackOverflow 上有一个 MS SQL 示例,如果我现在能看到它就好了。

创建过程 getSomething @keyList varchar(4096) 
  作为 
  SELECT * FROM mytbl WHERE name IN (fn_GetKeyList(@keyList)) 
  

致电 -

exec getSomething 'John,Tom,Foo,Bar' 
  

我猜 Sybase 应该能够做类似的事情?

Pass the comma separated list into a function that returns a table value. There is a MS SQL example somewhere on StackOverflow, damned if I can see it at the moment.

CREATE PROCEDURE getSomething @keyList varchar(4096)
AS
SELECT * FROM mytbl WHERE name IN (fn_GetKeyList(@keyList))

Call with -

exec getSomething 'John,Tom,Foo,Bar'

I'm guessing Sybase should be able to do something similar?

掌心的温暖 2024-07-11 03:09:14

这有点晚了,但我不久前遇到了这个问题,并且找到了解决方案。

技巧是双引号,然后将整个字符串用引号括起来。

exec getSomething """John"",""Tom"",""Bob"",""Harry"""

修改您的过程以将表条目与字符串相匹配。

CREATE PROCEDURE getSomething @keyList varchar(4096)
AS
SELECT * FROM mytbl WHERE @keyList LIKE '%'+name+'%' 

我从 ASE 12.5 开始就在生产中使用它; 我们现在的版本是 15.0.3。

This is a little late, but I had this exact issue a while ago and I found a solution.

The trick is double quoting and then wrapping the whole string in quotes.

exec getSomething """John"",""Tom"",""Bob"",""Harry"""

Modify your proc to match the table entry to the string.

CREATE PROCEDURE getSomething @keyList varchar(4096)
AS
SELECT * FROM mytbl WHERE @keyList LIKE '%'+name+'%' 

I've had this in production since ASE 12.5; we're now on 15.0.3.

傲鸠 2024-07-11 03:09:14

这是一种快速但肮脏的方法,可能有用:

select  * 
from    mytbl 
where   "," + ltrim(rtrim(@keylist)) + "," like "%," + ltrim(rtrim(name)) + ",%"

This is a quick and dirty method that may be useful:

select  * 
from    mytbl 
where   "," + ltrim(rtrim(@keylist)) + "," like "%," + ltrim(rtrim(name)) + ",%"
凌乱心跳 2024-07-11 03:09:14

要触及 @Abel 提供的内容,对我有帮助的是:

我的目的是获取最终用户从 SSRS 输入的内容,并将其在我的 where 子句中用作 In (SELECT)
显然 @ICD_VALUE_RPT 将在我的数据集查询中被注释掉。

DECLARE @ICD_VALUE_RPT VARCHAR(MAX) SET @ICD_VALUE_RPT = 'Value1, Value2'
DECLARE @ICD_VALUE_ARRAY XML SET @ICD_VALUE_ARRAY = CONCAT('<id>', REPLACE(REPLACE(@ICD_VALUE_RPT, ',', '</id>,<id>'),' ',''), '</id>')

然后在我的 WHERE 中添加:

(PATS_WITH_PL_DIAGS.ICD10_CODE IN (SELECT ParamValues.ID.value('.','VARCHAR(MAX)') FROM @ICD_VALUE_ARRAY.nodes('id') AS ParamValues(ID))
OR PATS_WITH_PL_DIAGS.ICD9_CODE IN (SELECT ParamValues.ID.value('.','VARCHAR(MAX)') FROM @ICD_VALUE_ARRAY.nodes('id') AS ParamValues(ID))
)

To touch on what @Abel provided, what helped me out was:

My purpose was to take what ever the end user inputted from SSRS and use that in my where clause as an In (SELECT)
Obviously @ICD_VALUE_RPT would be commented out in my Dataset query.

DECLARE @ICD_VALUE_RPT VARCHAR(MAX) SET @ICD_VALUE_RPT = 'Value1, Value2'
DECLARE @ICD_VALUE_ARRAY XML SET @ICD_VALUE_ARRAY = CONCAT('<id>', REPLACE(REPLACE(@ICD_VALUE_RPT, ',', '</id>,<id>'),' ',''), '</id>')

then in my WHERE i added:

(PATS_WITH_PL_DIAGS.ICD10_CODE IN (SELECT ParamValues.ID.value('.','VARCHAR(MAX)') FROM @ICD_VALUE_ARRAY.nodes('id') AS ParamValues(ID))
OR PATS_WITH_PL_DIAGS.ICD9_CODE IN (SELECT ParamValues.ID.value('.','VARCHAR(MAX)') FROM @ICD_VALUE_ARRAY.nodes('id') AS ParamValues(ID))
)
风向决定发型 2024-07-11 03:09:14

不确定是否在 ASE 中,但在 SQL Anywhere 中,sa_split_list 函数从 CSV 返回一个表。 它具有可选参数来传递不同的分隔符(默认为逗号)和每个返回值的最大长度。

sa_split_list 函数

Not sure if it's in ASE, but in SQL Anywhere, the sa_split_list function returns a table from a CSV. It has optional arguments to pass a different delimiter (default is a comma) and a maxlength for each returned value.

sa_split_list function

擦肩而过的背影 2024-07-11 03:09:14

像这样的调用的问题是: exec getSomething '"John","Tom"' 是将 '"John","Tom"' 视为单个字符串,它只会匹配表中的条目 '"约翰”、“汤姆”。

如果您不想像 Paul 的回答那样使用临时表,那么您可以使用动态 sql。 (假设 v12+)

CREATE PROCEDURE getSomething @keyList varchar(4096)
AS
declare @sql varchar(4096)
select @sql = "SELECT * FROM mytbl WHERE name IN (" + @keyList +")"
exec(@sql)

您需要确保 @keylist 中的项目有引号,即使它们是单个值。

The problem with the calls like this: exec getSomething '"John","Tom"' is that it's treating '"John","Tom"' as a single string, it will only match an entry in the table that is '"John","Tom"'.

If you didn't want to use a temp table as in Paul's answer, then you could use dynamic sql. (Assumes v12+)

CREATE PROCEDURE getSomething @keyList varchar(4096)
AS
declare @sql varchar(4096)
select @sql = "SELECT * FROM mytbl WHERE name IN (" + @keyList +")"
exec(@sql)

You will need to ensure the items in @keylist have quotes around them, even if they are single values.

分分钟 2024-07-11 03:09:14

这在 SQL 中有效。 在 GetSomething 过程中声明 XML 类型的变量,如下所示:

DECLARE @NameArray XML = NULL

存储过程的主体实现以下内容:

SELECT * FROM MyTbl WHERE name IN (SELECT ParamValues.ID.value('.','VARCHAR(10)')
FROM @NameArray.nodes('id') AS ParamValues(ID))

在调用存储过程之前,从调用 SP 的 SQL 代码中声明并初始化 XML 变量过程:

DECLARE @NameArray XML

SET @NameArray = '<id><</id>id>Name_1<<id>/id></id><id><</id>id>Name_2<<id>/id></id><id><</id>id>Name_3<<id>/id></id><id><</id>id>Name_4<<id>/id></id>'

使用您的示例,对存储过程的调用将是:

EXEC GetSomething @NameArray

我之前使用过此方法,并且效果很好。 如果您想要快速测试,请将以下代码复制并粘贴到新查询中并执行:

DECLARE @IdArray XML

SET @IdArray = '<id><</id>id>Name_1<<id>/id></id><id><</id>id>Name_2<<id>/id></id><id><</id>id>Name_3<<id>/id></id><id><</id>id>Name_4<<id>/id></id>'

SELECT ParamValues.ID.value('.','VARCHAR(10)')
FROM @IdArray.nodes('id') AS ParamValues(ID)

This works in SQL. Declare in your GetSomething procedure a variable of type XML as such:

DECLARE @NameArray XML = NULL

The body of the stored procedure implements the following:

SELECT * FROM MyTbl WHERE name IN (SELECT ParamValues.ID.value('.','VARCHAR(10)')
FROM @NameArray.nodes('id') AS ParamValues(ID))

From within the SQL code that calls the SP to declare and initialize the XML variable before calling the stored procedure:

DECLARE @NameArray XML

SET @NameArray = '<id><</id>id>Name_1<<id>/id></id><id><</id>id>Name_2<<id>/id></id><id><</id>id>Name_3<<id>/id></id><id><</id>id>Name_4<<id>/id></id>'

Using your example the call to the stored procedure would be:

EXEC GetSomething @NameArray

I have used this method before and it works fine. If you want a quick test, copy and paste the following code to a new query and execute:

DECLARE @IdArray XML

SET @IdArray = '<id><</id>id>Name_1<<id>/id></id><id><</id>id>Name_2<<id>/id></id><id><</id>id>Name_3<<id>/id></id><id><</id>id>Name_4<<id>/id></id>'

SELECT ParamValues.ID.value('.','VARCHAR(10)')
FROM @IdArray.nodes('id') AS ParamValues(ID)
束缚m 2024-07-11 03:09:14

试试这个方法。 它对我有用。

@itemIds varchar(max)

CREATE PROCEDURE getSomething @keyList varchar(4096)
AS
SELECT * FROM mytbl WHERE name IN (SELECT Value FROM [Global_Split] (@itemIds,','))

Try this way. Its works for me.

@itemIds varchar(max)

CREATE PROCEDURE getSomething @keyList varchar(4096)
AS
SELECT * FROM mytbl WHERE name IN (SELECT Value FROM [Global_Split] (@itemIds,','))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文