sql-server - SqlException:关键字“INNER”和“AS”和“ORDER”附近的语法不正确
问题描述
我正在使用 microsoft visual studio 2017 和 sql server 2017。当我在 Visual Studio 中运行此语句时,我没有收到任何错误:
@"
DECLARE @items table (item_id int, item_count int, item_type smallint)
INSERT INTO @items (item_id, item_count, item_type)
SELECT TOP 10 oi.item_id, COUNT(1) as item_count, 1 as item_type
FROM t_ord_item oi
INNER JOIN (
SELECT oi.basket_id
FROM t_ord_item oi
INNER JOIN t_ord_order o ON o.ord_id = oi.ord_id
WHERE oi.item_id = @item_id
AND oi.status_code <> 'del'
AND o.status_code <> 'del'
AND o.status_code <> 'can'
GROUP BY oi.basket_id
) as bsk ON bsk.basket_id = oi.basket_id
WHERE oi.item_id IS NOT NULL
AND oi.item_id <> @item_id
GROUP BY oi.item_id
ORDER BY item_count DESC
INSERT INTO @items (item_id, item_count, item_type)
SELECT TOP 10 oi.item_id, COUNT(1) as item_count, 2 as item_type
FROM t_ord_item oi
INNER JOIN (
SELECT login_name_created
FROM t_ord_item
WHERE item_id = @item_id
AND status_code <> 'del'
and login_name_created is not null
GROUP BY login_name_created
) as login ON login.login_name_created = oi.login_name_created
WHERE oi.item_id IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM @items temp WHERE temp.item_id = oi.item_id)
AND oi.item_id <> @item_id
GROUP BY oi.item_id
ORDER BY item_count DESC";
但是当我运行网络应用程序时,我收到了这个错误:
System.Data.SqlClient.SqlException:
Incorrect syntax near the keyword 'INNER'.
Incorrect syntax near the keyword 'AS'.
Incorrect syntax near the keyword 'ORDER'.
Incorrect syntax near the keyword 'AS'.
Incorrect syntax near the keyword 'AS'.
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.
Incorrect syntax near the keyword 'CASE'.
Invalid usage of the option NEXT in the FETCH statement.
我以为我缺少空格,所以我试图添加,但仍然是同样的问题。我已经像以前一样发布了代码,以便您可以帮助我检测问题strong text
解决方案
推荐阅读
- javascript - 在 ASP.NET MVC razor 视图中使用 HTML 助手的 Id 连接 javascript 动态变量
- html - 如何在同一个文件中运行两个匿名结构?
- reactjs - react js tsx文件中的图像标签出错
- kotlin - 第一次调用 ArraysKt.contains 很慢
- node.js - MongoDB/Mongoose:如何计算一个集合中的文档并根据 id 将其添加到另一个集合中?
- javascript - setInterval,单击某物然后清除Interval,然后几秒钟再次setInterval
- rust - 如何将选项映射到字符串以进行格式化,同时提供 &'static str 作为默认值?
- azure-devops - 如何在 Azure DevOps 中检索发布和构建数据
- charts - Nivo 树图工具提示未呈现
- aws-systems-manager - Amazon SSM 代理 - 无法启动消息总线