具有无限可选参数的 SQL 存储过程
我想创建一个至少有 2 个必需参数的存储过程,但也可以使用 2、3、4、5...等参数调用它。 原因:我有多个具有键值对的表,但是这个键值可以是另一个键值对列表的组。这样第一个列表就是下一个列表的父键。 这是可以使用相同过程调用的 2 个表的示例,稍后将详细介绍:
--MyListTableAlpha
+- Key1 (ValueA)
+- Key2 (ValueB)
+- Key3 (ValueC)
+- Key4 (ValueD)
--MyListTableBravo
+- Parent Uno
+- Key1 (Value1A)
+- Key2 (Value1B)
+- Parent Dos
+- Key1 (Value2A)
+- Key2 (Value2B)
+- Key3 (Value3C)
该代码必须适用于 SQL Server 2008。
这就是我为 2 个参数存储过程所做的:
CREATE PROCEDURE [dbo].[SPListValue]
-- Add the parameters for the stored procedure here
@listName nvarchar(100) = null,
@keyVal nvarchar(100) = null
-- optional parameters go here?!?
AS
BEGIN
SET NOCOUNT ON;
SELECT [value_string] from [tablenames]
JOIN [keyvalues] on [tablenames].[id] = [keyvalues].[tableid]
WHERE [dbo].[keyvalues].[key] = @keyVal
END
表 [keyvalues]
具有以下列:id
、tableid
、parentkeyid
、key
、value.当对值进行分组时,使用
parentkeyid
来了解它们属于哪一个。
这就是我想从我的 Java 代码中调用 MyListTableAlpha
的方式(注意 2?s):
CallableStatement cs1 = conn1.prepareCall("{call SPListValue(?,?}"); //notice 2 ?s
cs1.setString(1, "MyListTableAlpha");
cs1.setString(2, "Key1");
ResultSet rs1 = cs1.executeQuery();
rs1.next();
value = rs1.getString("value_string"); // Prints ValueA
这就是我想从我的 Java 代码中调用 MyListTableBravo
的方式(注意 3 ?s):
CallableStatement cs1 = conn1.prepareCall("{call SPListValue(?,?,?}"); //notice 3 ?s
cs1.setString(1, "MyListTableBravo");
cs1.setString(2, "Parent Uno");
cs1.setString(3, "Key2");
ResultSet rs1 = cs1.executeQuery();
rs1.next();
value = rs1.getString("value_string"); // Prints Value1B
I want to make a Stored Procedure that has a minimum of 2 required parameters, but that it can also be called with 2,3,4,5... and so on parameters.
The Reason: I have multiple tables that have Key-Value pairs, but then this Key-Value can be a group to another list of Key-Value pairs. So that the first one is the parent key to the next list.
This is an example of 2 Tables that can be called with the same procedure, which is detailed afterwards:
--MyListTableAlpha
+- Key1 (ValueA)
+- Key2 (ValueB)
+- Key3 (ValueC)
+- Key4 (ValueD)
--MyListTableBravo
+- Parent Uno
+- Key1 (Value1A)
+- Key2 (Value1B)
+- Parent Dos
+- Key1 (Value2A)
+- Key2 (Value2B)
+- Key3 (Value3C)
The code has to be for SQL Server 2008.
This is what I have for the 2 paremeter Stored Procedure:
CREATE PROCEDURE [dbo].[SPListValue]
-- Add the parameters for the stored procedure here
@listName nvarchar(100) = null,
@keyVal nvarchar(100) = null
-- optional parameters go here?!?
AS
BEGIN
SET NOCOUNT ON;
SELECT [value_string] from [tablenames]
JOIN [keyvalues] on [tablenames].[id] = [keyvalues].[tableid]
WHERE [dbo].[keyvalues].[key] = @keyVal
END
Table [keyvalues]
has the columns: id
,tableid
,parentkeyid
,key
,value
. Where parentkeyid
is used when the values are grouped to know which one they belong to.
This is how I would like to call MyListTableAlpha
from my Java code (notice 2 ?s):
CallableStatement cs1 = conn1.prepareCall("{call SPListValue(?,?}"); //notice 2 ?s
cs1.setString(1, "MyListTableAlpha");
cs1.setString(2, "Key1");
ResultSet rs1 = cs1.executeQuery();
rs1.next();
value = rs1.getString("value_string"); // Prints ValueA
This is how I would like to call MyListTableBravo
from my Java code (notice 3 ?s):
CallableStatement cs1 = conn1.prepareCall("{call SPListValue(?,?,?}"); //notice 3 ?s
cs1.setString(1, "MyListTableBravo");
cs1.setString(2, "Parent Uno");
cs1.setString(3, "Key2");
ResultSet rs1 = cs1.executeQuery();
rs1.next();
value = rs1.getString("value_string"); // Prints Value1B
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要考虑创建包含 XML 的第三个参数。然后您可以输入任意数量的信息。
You may want to consider making a third parameter that contains XML. Then you can put as much info as you want.
我就是这样解决的。
我没有使用无限参数,而是将自己限制为最多 4 个
:-)
This is how I solved it.
Instead of having infinite parameters, I restricted myself to a maximum of 4
:-)