我想创建一个至少包含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?):
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?):
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
答案 0 :(得分:2)
您可能需要考虑制作包含XML的第三个参数。然后你可以根据需要输入尽可能多的信息。
答案 1 :(得分:0)
这就是我解决它的方法。
我没有使用无限参数,而是将自己限制在最多4
CREATE PROCEDURE [dbo].[SPListValue]
@listName nvarchar(100) = null,
@key1Val nvarchar(100) = null,
@key2Val nvarchar(100) = null,
@key3Val nvarchar(100) = null,
@key4Val nvarchar(100) = null
AS
BEGIN
SET NOCOUNT ON;
if @key4Val is not null
SELECT fourCE.[value_string] from [tablenames] as fourCE
JOIN [keyvalues] as fourLS ON fourCE.[object_id] = fourLS.[parent_id]
JOIN [tablenames] as threeCE ON threeCE.[object_id] = fourCE.[parent_id]
JOIN [keyvalues] as threeLS on threeCE.[object_id] = threeLS.[parent_id]
JOIN [tablenames] as twoCE ON twoCE.[object_id] = threeCE.[parent_id]
JOIN [keyvalues] as twoLS on twoCE.[object_id] = twoLS.[parent_id]
JOIN [tablenames] as oneCE ON oneCE.[object_id] = threeCE.[parent_id]
JOIN [keyvalues] as oneLS on oneCE.[object_id] = oneLS.[parent_id]
JOIN [Cvl] on oneCE.[parent_cvl_id] = [Cvl].[object_id]
WHERE oneLS.[text] = @key1Val
AND twoLS.[text] = @key2Val
AND threeLS.[text] = @key3Val
AND fourLS.[text] = @key4Val
AND [Cvl].[display_name] = @listName
else if @key3val is not null
SELECT threeCE.[value_string] from [tablenames] as threeCE
JOIN [keyvalues] as threeLS ON threeCE.[object_id] = threeLS.[parent_id]
JOIN [tablenames] as twoCE ON twoCE.[object_id] = threeCE.[parent_id]
JOIN [keyvalues] as twoLS on twoCE.[object_id] = twoLS.[parent_id]
JOIN [tablenames] as oneCE ON oneCE.[object_id] = twoCE.[parent_id]
JOIN [keyvalues] as oneLS on oneCE.[object_id] = oneLS.[parent_id]
JOIN [Cvl] on oneCE.[parent_cvl_id] = [Cvl].[object_id]
WHERE oneLS.[text] = @key1Val
AND twoLS.[text] = @key2Val
and threeLS.[text] = @key3Val
AND [Cvl].[display_name] = @listName
else if @key2Val is not null
SELECT twoCE.[value_string] from [tablenames] as twoCE
JOIN [keyvalues] as twoLS ON twoCE.[object_id] = twoLS.[parent_id]
JOIN [tablenames] as oneCE ON oneCE.[object_id] = twoCE.[parent_id]
JOIN [keyvalues] as oneLS on oneCE.[object_id] = oneLS.[parent_id]
JOIN [Cvl] on oneCE.[parent_cvl_id] = [Cvl].[object_id]
WHERE oneLS.[text] = @key1Val AND twoLS.[text] = @key2Val
AND [Cvl].[display_name] = @listName
else
SELECT [value_string] from [tablenames]
JOIN [keyvalues] ON [tablenames].[object_id] = [keyvalues].[parent_id]
JOIN [Cvl] on [tablenames].[parent_cvl_id] = [Cvl].[object_id]
WHERE [keyvalues].[text] = @key1Val
AND [Cvl].[display_name] = @listName
END
: - )