如何将逗号分隔的列表传递给存储过程?
因此,我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您使用的是 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.
关于 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
您需要使用逗号分隔列表吗? 在过去的几年里,我一直在采用这种类型的想法并传递 XML 文件。 openxml“函数”采用一个字符串并使其像 xml 一样,然后如果您使用数据创建一个临时表,则它是可查询的。
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.
将逗号分隔的列表传递到返回表值的函数中。 StackOverflow 上有一个 MS SQL 示例,如果我现在能看到它就好了。
致电 -
我猜 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.
Call with -
I'm guessing Sybase should be able to do something similar?
这有点晚了,但我不久前遇到了这个问题,并且找到了解决方案。
技巧是双引号,然后将整个字符串用引号括起来。
修改您的过程以将表条目与字符串相匹配。
我从 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.
Modify your proc to match the table entry to the string.
I've had this in production since ASE 12.5; we're now on 15.0.3.
这是一种快速但肮脏的方法,可能有用:
This is a quick and dirty method that may be useful:
要触及 @Abel 提供的内容,对我有帮助的是:
我的目的是获取最终用户从 SSRS 输入的内容,并将其在我的 where 子句中用作 In (SELECT)
显然 @ICD_VALUE_RPT 将在我的数据集查询中被注释掉。
然后在我的
WHERE
中添加: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.
then in my
WHERE
i added:不确定是否在 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
像这样的调用的问题是: exec getSomething '"John","Tom"' 是将 '"John","Tom"' 视为单个字符串,它只会匹配表中的条目 '"约翰”、“汤姆”。
如果您不想像 Paul 的回答那样使用临时表,那么您可以使用动态 sql。 (假设 v12+)
您需要确保 @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+)
You will need to ensure the items in @keylist have quotes around them, even if they are single values.
这在 SQL 中有效。 在
GetSomething
过程中声明 XML 类型的变量,如下所示:存储过程的主体实现以下内容:
在调用存储过程之前,从调用 SP 的 SQL 代码中声明并初始化 XML 变量过程:
使用您的示例,对存储过程的调用将是:
我之前使用过此方法,并且效果很好。 如果您想要快速测试,请将以下代码复制并粘贴到新查询中并执行:
This works in SQL. Declare in your
GetSomething
procedure a variable of type XML as such:The body of the stored procedure implements the following:
From within the SQL code that calls the SP to declare and initialize the XML variable before calling the stored procedure:
Using your example the call to the stored procedure would be:
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:
试试这个方法。 它对我有用。
Try this way. Its works for me.