gpt4 book ai didi

sql-server - 将 with cte 查询的结果插入临时表

转载 作者:行者123 更新时间:2023-12-02 09:04:52 24 4
gpt4 key购买 nike

我想将此查询的结果存储到临时表中:

WITH cOldest AS
(
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY [MyKey] ORDER BY SomeColumn DESC) AS rnDOB
FROM MyTable
)
SELECT
C.*
*** Insert into #MyTempTable *** This part doesn't work
FROM
cOldest C
WHERE
C.rnDOB = 1

提前致谢。

最佳答案

假设这适用于SQL Server:CTE 仅适用于一个 语句 - 因此您不能同时拥有SELECTINSERT - 只需使用INSERT:

WITH cOldest AS
(
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY [MyKey] ORDER BY SomeColumn DESC) AS rnDOB
FROM MyTable
)
INSERT INTO #MyTempTable(Col1, Col2, ....., ColN)
SELECT Col1, Col2, ...., ColN
FROM cOldest C
WHERE C.rnDOB = 1

这要求#MyTempTable 已存在。如果您想使用 SELECT 创建它 - 请使用以下语法:

WITH cOldest AS
(
.....
)
SELECT c.*
INTO #MyTempTable
FROM cOldest c
WHERE C.rnDOB = 1

关于sql-server - 将 with cte 查询的结果插入临时表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28825305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com