具有无限可选参数的 SQL 存储过程

发布于 2024-10-06 11:16:14 字数 1829 浏览 4 评论 0原文

我想创建一个至少有 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] 具有以下列:idtableidparentkeyidkeyvalue.当对值进行分组时,使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋竹姑娘 2024-10-13 11:16:15

您可能需要考虑创建包含 XML 的第三个参数。然后您可以输入任意数量的信息。

You may want to consider making a third parameter that contains XML. Then you can put as much info as you want.

浅浅 2024-10-13 11:16:15

我就是这样解决的。

我没有使用无限参数,而是将自己限制为最多 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

:-)

This is how I solved it.

Instead of having infinite parameters, I restricted myself to a maximum of 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

:-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文