尝试使用参数和多个连接创建存储过程
Trying to create stored procedure with parameter and multiple joins
我正在尝试创建一个带有一个参数并连接三个表的存储过程:OrdersOrders
、Ordered_ProductsOrdered_Products
和 ProductsProducts
。我想要所有没有匹配表的表中的列。该参数在 java 中使用,以便客户能够根据其用户 ID 搜索订单。
这是我想出的:
CREATE PROCEDURE sp_seeOrder
@user_id
AS
SELECT
O.order_id, O.order_quantity, O.user_id, O.Status, O.order_date,
O_P.orderID, O_P.product_code,
P.product_id, P.product_name, P.price
FROM
Orders AS O
FULL JOIN
Ordered_Products ON O.order_id = O_P.orderID,
RIGHT JOIN
Products P ON O_P.product_code = P.product_id
WHERE
user_id = @user_id;
我收到多个错误:
Msg 156, Level 15, State 1, Procedure sp_seeOrder, Line 4 [Batch Start Line 213]
Incorrect syntax near the keyword 'SELECT'.
Msg 156, Level 15, State 1, Procedure sp_seeOrder, Line 9 [Batch Start Line 213]
Incorrect syntax near the keyword 'RIGHT'.
我做错了什么?
将数据类型放入@user_id 参数中。我假设它是一个 INT,因为它是一个 ID。并删除“ON O.order_id = O_P.orderID”之后多余的 , 。更正后的查询应如下所示:
CREATE PROC sp_seeOrder
@user_id INT
AS
SELECT
O.order_id, O.order_quantity, O.user_id, O.Status, O.order_date, O_P.orderID,
O_P.product_code, P.product_id, P.product_name, P.price
FROM Orders AS O
FULL JOIN Ordered_Products AS O_P
ON O.order_id = O_P.orderID
RIGHT JOIN Products P
ON O_P.product_code = P.product_id
WHERE user_id = @user_id;
我正在尝试创建一个带有一个参数并连接三个表的存储过程:OrdersOrders
、Ordered_ProductsOrdered_Products
和 ProductsProducts
。我想要所有没有匹配表的表中的列。该参数在 java 中使用,以便客户能够根据其用户 ID 搜索订单。
这是我想出的:
CREATE PROCEDURE sp_seeOrder
@user_id
AS
SELECT
O.order_id, O.order_quantity, O.user_id, O.Status, O.order_date,
O_P.orderID, O_P.product_code,
P.product_id, P.product_name, P.price
FROM
Orders AS O
FULL JOIN
Ordered_Products ON O.order_id = O_P.orderID,
RIGHT JOIN
Products P ON O_P.product_code = P.product_id
WHERE
user_id = @user_id;
我收到多个错误:
Msg 156, Level 15, State 1, Procedure sp_seeOrder, Line 4 [Batch Start Line 213]
Incorrect syntax near the keyword 'SELECT'.Msg 156, Level 15, State 1, Procedure sp_seeOrder, Line 9 [Batch Start Line 213]
Incorrect syntax near the keyword 'RIGHT'.
我做错了什么?
将数据类型放入@user_id 参数中。我假设它是一个 INT,因为它是一个 ID。并删除“ON O.order_id = O_P.orderID”之后多余的 , 。更正后的查询应如下所示:
CREATE PROC sp_seeOrder
@user_id INT
AS
SELECT
O.order_id, O.order_quantity, O.user_id, O.Status, O.order_date, O_P.orderID,
O_P.product_code, P.product_id, P.product_name, P.price
FROM Orders AS O
FULL JOIN Ordered_Products AS O_P
ON O.order_id = O_P.orderID
RIGHT JOIN Products P
ON O_P.product_code = P.product_id
WHERE user_id = @user_id;