首页 > 解决方案 > 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

标签: sql-server

解决方案


推荐阅读