T-SQL:使用存储在多个单列表中的所有值组合调用存储过程

问题描述 投票:0回答:1

我通过 Microsoft SQL Server Management Studio 版本 18.9.2 使用 T-SQL。

下面,我展示了我正在使用的表格并提出了我正在尝试解决的问题。

-- Create single-column tables to hold values.
CREATE TABLE [dbo].[Attr_1_Values]
(
   [Attr_1_Value] VARCHAR(30) NOT NULL PRIMARY KEY
);

CREATE TABLE [dbo].[Attr_2_Values]
(
   [Attr_2_Value] VARCHAR(30) NOT NULL PRIMARY KEY
);

CREATE TABLE [dbo].[Attr_3_Values]
(
   [Attr_3_Value] VARCHAR(30) NOT NULL PRIMARY KEY
);

-- Insert rows (individual values in this case) into the just-created tables.
INSERT INTO [dbo].[Attr_1_Values] ([Attr_1_Value]) VALUES ('Attr_1_val_1');
INSERT INTO [dbo].[Attr_1_Values] ([Attr_1_Value]) VALUES ('Attr_1_val_2');
INSERT INTO [dbo].[Attr_1_Values] ([Attr_1_Value]) VALUES ('Attr_1_val_3');
INSERT INTO [dbo].[Attr_1_Values] ([Attr_1_Value]) VALUES ('Attr_1_val_4');

INSERT INTO [dbo].[Attr_2_Values] ([Attr_2_Value]) VALUES ('Attr_2_val_1');
INSERT INTO [dbo].[Attr_2_Values] ([Attr_2_Value]) VALUES ('Attr_2_val_2');

INSERT INTO [dbo].[Attr_3_Values] ([Attr_3_Value]) VALUES ('Attr_3_val_1');
INSERT INTO [dbo].[Attr_3_Values] ([Attr_3_Value]) VALUES ('Attr_4_val_2');

-- Create a stored procedure that will take as parameters values from the tables above.
CREATE PROC [dbo].[My_Stored_Procedure]
   @Attr_1_p AS VARCHAR(30),
   @Attr_2_p AS VARCHAR(30),
   @Attr_3_p AS VARCHAR(30)
AS
BEGIN
   -- Stored procedure logic goes here.
END;

-- Generate all combinations of values from the [Attr_1_Values], [Attr_2_Values], and [Attr_3_Values] tables.
-- Note: From this point on, pseudocode and proper T-SQL code are mixed to best try to convey my intent.
Attr_Combinations = SELECT [Attr_1_Value], [Attr_2_Value], [Attr_3_Value] FROM
                       [dbo].[Attr_1_Values] CROSS JOIN
                       [dbo].[Attr_2_Values] CROSS JOIN
                       [dbo].[Attr_3_Values];

-- Somehow, invoke [dbo].[My_Stored_Procedure] with each individual combination.
-- In this example, that would be 4*2*2 = 16 invocations of [dbo].[My_Stored-Procedure].

LOOP OVER Attr_Combinations
BEGIN
   EXEC [dbo].[My_Stored_Procedure]
      @Attr_1_p = One_Attr_Combination.Attr_1_Value,
      @Attr_2_p = One_Attr_Combination.Attr_2_Value,
      @Attr_3_p = One_Attr_Combination.Attr_3_Value;
END;

我如何完成所需的 [dbo].[My_Stored_Procedure] 调用,并且每次调用都传递如上所述的一个值组合?

sql sql-server database t-sql ssms
1个回答
0
投票

请检查以下概念示例。

SQL

DECLARE @IDVar INT, @NameVar VARCHAR(50);

DECLARE @tbl TABLE (ID INT, [Name] VARCHAR(50));
INSERT @tbl (ID, [Name])
VALUES (1, 'Employee')
   , (2, 'Department')
   , (3, 'Class');

DECLARE @RowCount INT = (SELECT COUNT(*) FROM @tbl);

WHILE @RowCount > 0 BEGIN
   SELECT @IDVar=ID, @NameVar=[Name] 
   FROM @tbl 
   ORDER BY ID DESC OFFSET @RowCount - 1 ROWS FETCH NEXT 1 ROWS ONLY;

   SELECT @IDVar, @NameVar
   
   -- do whatever needed, apply any logic, call stored procedures, etc.
   /*
   Exec usp_Employee @name = @NameVar
   Exec usp_Department @ID = @IDVar,@name = @NameVar
   */

   SET @RowCount -= 1;
END
© www.soinside.com 2019 - 2024. All rights reserved.