如何从存储过程查询中的逻辑中删除临时表
How to remove temp tables from logic in stored procedure query
我需要改进此处显示的存储过程示例代码以删除所有临时 tables,可能是通过使用一个查询代码或 CTE?
请注意,有必要创建第一个临时 table 以获取特定用户 ID 列表,然后使用另一个特定条件创建第二个临时 table,然后加入两个临时 [=15] =]s 被创建以获得最终结果 table.
SELECT
Column_1,
Column_2,
Column_3
INTO
#TempTable_1
FROM
TABLE_1
WHERE
Column_1 = 1
SELECT
Column_1,
Column_4,
Column_5,
Column_6
INTO
#TempTable_2
FROM
TABLE_2
WHERE
Column_4 = 4
SELECT
A.Column_1,
A.Column_2,
A.Column_3,
B.Column_4,
B.Column_5,
B.Column_6
FROM
#TempTable_2 B
INNER JOIN
#TempTable_1 A ON A.Column_1 = B.Column_1
除了 Dale 的 sub-query,您还可以使用 CTE
with cte1 as (
SELECT
Column_1,
Column_2,
Column_3
FROM TABLE_1
WHERE Column_1 = 1
), cte2 as (
SELECT
Column_1,
Column_4,
Column_5,
Column_6
FROM TABLE_2
WHERE Column_4 = 4
)
SELECT
A.Column_1,
A.Column_2,
A.Column_3,
B.Column_4,
B.Column_5,
B.Column_6
FROM cte2 B
INNER JOIN cte1 A ON A.Column_1 = B.Column_1
我需要改进此处显示的存储过程示例代码以删除所有临时 tables,可能是通过使用一个查询代码或 CTE?
请注意,有必要创建第一个临时 table 以获取特定用户 ID 列表,然后使用另一个特定条件创建第二个临时 table,然后加入两个临时 [=15] =]s 被创建以获得最终结果 table.
SELECT
Column_1,
Column_2,
Column_3
INTO
#TempTable_1
FROM
TABLE_1
WHERE
Column_1 = 1
SELECT
Column_1,
Column_4,
Column_5,
Column_6
INTO
#TempTable_2
FROM
TABLE_2
WHERE
Column_4 = 4
SELECT
A.Column_1,
A.Column_2,
A.Column_3,
B.Column_4,
B.Column_5,
B.Column_6
FROM
#TempTable_2 B
INNER JOIN
#TempTable_1 A ON A.Column_1 = B.Column_1
除了 Dale 的 sub-query,您还可以使用 CTE
with cte1 as (
SELECT
Column_1,
Column_2,
Column_3
FROM TABLE_1
WHERE Column_1 = 1
), cte2 as (
SELECT
Column_1,
Column_4,
Column_5,
Column_6
FROM TABLE_2
WHERE Column_4 = 4
)
SELECT
A.Column_1,
A.Column_2,
A.Column_3,
B.Column_4,
B.Column_5,
B.Column_6
FROM cte2 B
INNER JOIN cte1 A ON A.Column_1 = B.Column_1