我有一个存储过程parameter1
和parameter2
在存储过程中,我正在创建一个临时表
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 将其重构为简单。非常感谢任何示例和/或建议。