0

我有一个存储过程parameter1parameter2

在存储过程中,我正在创建一个临时表

CREATE TABLE [dbo].#Temp    
(
[Item_ID] [int] NOT NULL,
[Item] [nvarchar](255) NULL,    
[FIELD_1] [float] NULL, 
[FIELD_2] [float] NULL

CONSTRAINT [PK___Temp] PRIMARY KEY CLUSTERED 
(
    [Item_ID] ASC
)

然后将值插入临时表。

INSERT INTO [dbo].#Temp(
     [Item_ID]  
    ,[Item]
    ,[FIELD_1]
    ,[FIELD_2]
)SELECT DISTINCT 
             1 
            ,'Item Description'
            ,(SELECT Field_Name FROM NewTable
              WHERE Item_type = 'Middle'  
              AND param1 = @Parameter1  
              AND param2 = @Parameter2
              AND STAFF_Type = 'Staff1'
              AND Entity_Type = 'Entity1'
              )

            ,(SELECT Field_Name FROM NewTable
              WHERE Sch_type = 'High'  
              AND param1 = @Parameter1  
              AND param2 = @Parameter2
              AND STAFF_Type = 'Staff1'
              AND Entity_Type = 'Entity1'
              )

INSERT INTO [dbo].#Temp(
     [Item_ID]  
    ,[Item]
    ,[FIELD_1]
    ,[FIELD_2]
)SELECT DISTINCT 
             2 
            ,'Another Item Description'
            ,(SELECT Field_Name FROM NewTable
              WHERE Item_type = 'Middle'  
              AND param1 = @Parameter1  
              AND param2 = @Parameter2
              AND STAFF_Type = 'Staff1'
              AND Entity_Type = 'Entity2'
              )

            ,(SELECT Field_Name FROM NewTable
              WHERE Sch_type = 'High'  
              AND param1 = @Parameter1  
              AND param2 = @Parameter2
              AND STAFF_Type = 'Staff1'
              AND Entity_Type = 'Entity2'
              )

等等等等。然后从临时表中选择值并删除临时表。

我没有使用 UDF,想知道是否可以使用 UDF 将其重构为简单。非常感谢任何示例和/或建议。

4

2 回答 2

0

去餐桌的次数很重要。例如,您可以重构现有查询以将行程次数从 4 次减少到 2 次:

INSERT INTO [dbo].#Temp(
     [Item_ID]  
    ,[Item]
    ,[FIELD_1]
    ,[FIELD_2]
)
        SELECT DISTINCT 
            1 AS Item_ID
            ,'Item Description' AS Item
            ,CASE WHEN Item_type = 'Middle' THEN Field_Name ELSE NULL END AS Field_1
            ,CASE WHEN Sch_type = 'High' THEN Field_Name ELSE NULL END AS Field2
        FROM NewTable
        WHERE 
            param1 = @Parameter1  
            AND param2 = @Parameter2
            AND STAFF_Type = 'Staff1'
            AND Entity_Type = 'Entity1'
        UNION
        SELECT DISTINCT 
            2 AS Item_ID
            ,'Another Item Description' AS Item
            ,CASE WHEN Item_type = 'Middle' THEN Field_Name ELSE NULL END AS Field_1
            ,CASE WHEN Sch_type = 'High' THEN Field_Name ELSE NULL END AS Field2
        FROM NewTable
        WHERE 
            param1 = @Parameter1  
            AND param2 = @Parameter2
            AND STAFF_Type = 'Staff1'
            AND Entity_Type = 'Entity2'

或者您可以创建一个具有类似逻辑的函数:

CREATE FUNCTION dbo.GetStaff1FieldNames ( @param1 INT, @param2 INT, @Entity_Type VARCHAR(20))
RETURNS TABLE 
AS
RETURN

    SELECT
        CASE WHEN Item_type  = 'Middle' THEN Field_Name ELSE NULL END AS Field_1,
        CASE WHEN Sch_type = 'High' THEN Field_Name ELSE NULL END AS Field_2
    FROM NewTable
    WHERE 
        param1 = @Parameter1 AND  
        param2 = @Parameter2 AND
        Entity_Type = @Entity_Type AND
        STAFF_Type = 'Staff1'
    GO

然后像这样调用它:

SELECT
    1 
    ,'Item Description'
    ,Field_1
    ,Field_2
FROM dbo.GetStaff1FieldNames (@param1, @param2, 'Entity1')
UNION
SELECT
    2 
    ,'Another Item Description'
    ,Field_1
    ,Field_2
FROM dbo.GetStaff1FieldNames (@param1, @param2, 'Entity2')

可能有一些方法可以通过一次访问表来检索所有内容,但我需要更多关于数据的上下文来进一步重构。

于 2013-10-15T00:16:50.517 回答
0

为什么不为您的选择使用公用表(或“WITH”)表达式?然后你可以完全摆脱临时表。

WITH  Fields ( ID, Desc, MidField, HighField ) AS
      (
         SELECT DISTINCT 
                1 
              , 'Item Description'
              , (SELECT Field_Name FROM NewTable
                 WHERE Item_type = 'Middle'  
                 AND param1 = @Parameter1  
                 AND param2 = @Parameter2
                 AND STAFF_Type = 'Staff1'
                 AND Entity_Type = 'Entity1'
                )
              , (SELECT Field_Name FROM NewTable
                 WHERE Sch_type = 'High'  
                 AND param1 = @Parameter1  
                 AND param2 = @Parameter2
                 AND STAFF_Type = 'Staff1'
                 AND Entity_Type = 'Entity1'
                 )
         UNION
         SELECT DISTINCT 
                2 
              , 'Another Item Description'
              , (SELECT Field_Name FROM NewTable
                 WHERE Item_type = 'Middle'  
                 AND param1 = @Parameter1  
                 AND param2 = @Parameter2
                 AND STAFF_Type = 'Staff1'
                 AND Entity_Type = 'Entity2'
                )
              , (SELECT Field_Name FROM NewTable
                 WHERE Sch_type = 'High'  
                 AND param1 = @Parameter1  
                 AND param2 = @Parameter2
                 AND STAFF_Type = 'Staff1'
                 AND Entity_Type = 'Entity2'
                )
      )
SELECT *
FROM   Fields
WHERE  MidField = 'XYZ'

请注意,CTE 仅适用于第一个 select 语句。您不能对 CTE 执行多个 select 语句,尽管您可以为单个 select 语句设置多个 CTE:

WITH TempA ( Columns ) AS ( SELECT * FROM TableA )
   , TempB ( Columns ) AS ( SELECT * FROM TableB )
   , TempC ( Columns ) AS ( SELECT * FROM TempA, TempB )
SELECT * FROM TempC WHERE Columns = 1
于 2013-10-14T20:41:44.430 回答