我已经使用这种方法,它对我来说非常好。
让您的存储过程接收三个变量,每个变量为varchar(max)
- 如果知道大小,则可以编写数字而不是max
。例如:
create procedure usp_testSP
@v1 varchar(max),
@v2 varchar(max),
@v3 varchar(max)
as
begin
declare @v1Values table (number int);
insert into @v1values
select * from dbo.fnSplit(@v1,','); -- the fnSplit function is given below
-- this way you can retrieve all values for other two variables
-- then you can use the corresponding tables, i.e.: @v1Values to complete the steps you need to.
end
下面是dbo.fnSplit(@inputList, @delimiter)
代码:
CREATE FUNCTION [dbo].[fnSplit](@sInputList VARCHAR(max), @sDelimiter VARCHAR(10) = ',')
RETURNS @List TABLE (item varchar(100))
BEGIN
DECLARE @sItem varchar(100)
WHILE CHARINDEX(@sDelimiter, @sInputList, 0) <> 0
BEGIN
SELECT @sItem = RTRIM(LTRIM(SUBSTRING(@sInputList, 1, CHARINDEX(@sDelimiter, @sInputList,0) - 1))),
@sInputList = RTRIM(LTRIM(SUBSTRING(@sInputList, CHARINDEX(@sDelimiter, @sInputList, 0) + LEN(@sDelimiter),LEN(@sInputList))))
IF LEN(@sItem) > 0
INSERT INTO @List SELECT @sItem
END
IF LEN(@sInputList) > 0
BEGIN
INSERT INTO @List SELECT @sInputList
END
RETURN
END
最后,在Java代码中,你可以将列表转换为字符串,并将其传递到存储过程调用。
List<Integer> v1; //suppose this is the list that contains the values.
String s = String.join("," /*this is the delimiter. It should be the same as the one you use when you call the dbo.fnSplit() function.*/, v1);
你尝试过什么吗?创建一个存储过程?试图循环变量?更新表格? – Forklift
什么数据库?您使用哪种数据访问提供程序? –
是的,我是新的存储过程,但我刚写入更新查询,更新一行的值。我可以很容易地使用Java或任何其他编程语言,但我正在使用的应用程序只允许我编写存储过程...这就是为什么我想了解,是否有可能通过存储过程来循环arraylist变量? – user3384231