- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在这里已经有了答案:
Combining INSERT INTO and WITH/CTE
(4 个回答)
6年前关闭。
我已经想出如何使用通用表表达式递归查找所有向某个经理汇报的员工(感谢 StackOverflow!)。
这是对我有用的代码:
WITH MyCTE AS
(
SELECT [WWID] FROM [x500]..[WorkerPublicExtended]
WHERE [MgrWWID] = '10624529' AND ([StatCode] = 'A') AND ([BadgeType] = 'BB')
UNION ALL
SELECT [WorkerPublicExtended].[WWID] FROM [x500]..[WorkerPublicExtended]
INNER JOIN MyCTE ON [WorkerPublicExtended].[MgrWWID] = MyCTE.WWID
WHERE [WorkerPublicExtended].[MgrWWID] IS NOT NULL
AND ([BadgeType] = 'BB') AND ([StatCode] = 'A')
)
SELECT *, 'MGR+10624529' AS [source] FROM MyCTE
INSERT INTO [LTDtraining].[dbo].[pop00001]
WITH MyCTE AS
(
SELECT [WWID] FROM [x500]..[WorkerPublicExtended]
WHERE [MgrWWID] = '10624529' AND ([StatCode] = 'A') AND ([BadgeType] = 'BB')
UNION ALL
SELECT [WorkerPublicExtended].[WWID] FROM [x500]..[WorkerPublicExtended]
INNER JOIN MyCTE ON [WorkerPublicExtended].[MgrWWID] = MyCTE.WWID
WHERE [WorkerPublicExtended].[MgrWWID] IS NOT NULL
AND ([BadgeType] = 'BB') AND ([StatCode] = 'A')
)
SELECT *, 'MGR+10624529' AS [source] FROM MyCTE
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'MyCTE'.
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'WITH'.Msg 319, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.Msg 102, Level 15, State 1, Line 9
Incorrect syntax near ')'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ';'.
最佳答案
通过移动 INSERT
可以轻松修复该错误。 WITH
下方的声明像这样:
WITH MyCTE AS
(
SELECT [WWID] FROM [x500]..[WorkerPublicExtended]
WHERE [MgrWWID] = '10624529' AND ([StatCode] = 'A') AND ([BadgeType] = 'BB')
UNION ALL
SELECT [WorkerPublicExtended].[WWID] FROM [x500]..[WorkerPublicExtended]
INNER JOIN MyCTE ON [WorkerPublicExtended].[MgrWWID] = MyCTE.WWID
WHERE [WorkerPublicExtended].[MgrWWID] IS NOT NULL
AND ([BadgeType] = 'BB') AND ([StatCode] = 'A')
)
INSERT INTO [LTDtraining].[dbo].[pop00001]
SELECT *, 'MGR+10624529' AS [source] FROM MyCTE
关于sql - INSERT INTO WITH 公用表表达式 - SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34426097/
我是一名优秀的程序员,十分优秀!