在我的应用程序中,我想执行查询,如SELECT*FROM tbl WHERE col IN(@list)其中,@ list可以有变量no值.我正在使用MS SQL服务器数据库.当我谷歌这个问题,然后我找到了这个链接
http://www.sommarskog.se/arrays-in-sql-2008.html
此链接表示使用表值参数.所以我使用Microsoft SQL Server Management Studio创建了用户定义的数据类型.
CREATE TYPE integer_list_tbltype AS TABLE(n int NOT NULL PRIMARY KEY)
然后我写了存储过程
CREATE PROCEDURE get_product_names @prodids integer_list_tbltype READONLY AS
SELECT p.ProductID, p.ProductName
FROM Northwind.dbo.Products p
WHERE p.ProductID IN (SELECT n FROM @prodids)
Run Code Online (Sandbox Code Playgroud)
然后只使用管理工作室我执行了这个程序
DECLARE @mylist integer_list_tbltype
INSERT @mylist(n) VALUES(9),(12),(27),(37)
EXEC get_product_names @mylist
Run Code Online (Sandbox Code Playgroud)
它给了我正确的输出.但我想知道如何从java源代码调用此存储过程.我知道如何使用常量参数调用简单的存储过程
CallableStatement proc_stmt = null;
proc_stmt = con.prepareCall("{call test(?)}");
proc_stmt.setString(1,someValue);
Run Code Online (Sandbox Code Playgroud)
但如何在表值参数的情况下调用存储过程?
这是我的index.jsp文件:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>HOME</title>
</head>
<body>
<s:action name="getUrl"></s:action>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我的struts.xml:
<struts>
<action name="getUrl" class="UrlAction">
<result name="redirect" type="redirect">${url}</result>
</action>
</struts>
Run Code Online (Sandbox Code Playgroud)
这是我的动作类:
public class UrlAction extends ActionSupport {
private String url;
public void setUrl(String url) {
this.url = url;
}
public String getUrl(){
return url;
}
public String execute() throws Exception {
System.out.println("Entering execute() of Action");
url = "https://www.google.com/";
return "redirect";
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当我运行index.jsp时,它应该将我重定向到https://www.google.com,但我没有得到它.正在打印"输入执行的执行()".这意味着它将进入Action类.如果我做错了,请纠正我.