存储过程SYNTAX错误附近' WHERE'

时间:2014-10-23 00:44:09

标签: sql syntax-error

是否有人知道造成错误的原因:"关键字附近的语法错误' WHERE'"

CREATE PROC proc_ProductInf
    @SalesOrderID int,
    @SalesOrderOut int OUTPUT,
    @OrderDate datetime OUTPUT,
    @ShipDate int OUTPUT,
    @CityState varchar(100) OUTPUT
AS
BEGIN
    SET NOCOUNT ON;

SET @SalesOrderOut = @SalesOrderID

SET @OrderDate = (SELECT OrderDate FROM sALES.SalesOrderHeader )

SET @CityState = (
    SELECT          City, State
    FROM            Sales.SalesOrderHeader s     
    LEFT OUTER JOIN Person.Address a
    ON              s.ShipToAddressID = a.AddressID
    LEFT OUTER JOIN Person.StateProvince st
    WHERE           SalesOrderID = @SalesOrderID
    )

END

DECLARE @OrderNum int, @Date datetime, @Qty int
EXEC proc_ProductInf 63936, @SalesHeaderOut=@OrderNum OUTPUT,
    @OrderDate=@Date OUTPUT,
    @NumItems=@Qty OUTPUT

4 个答案:

答案 0 :(得分:2)

您没有指定 ON 将Person.Address加入Person.StateProvince的内容。

SET @CityState = (
    SELECT          City, State
    FROM            Sales.SalesOrderHeader s     
    LEFT OUTER JOIN Person.Address a
    ON              s.ShipToAddressID = a.AddressID
    LEFT OUTER JOIN Person.StateProvince st
    ON              a.AddressStateProvinceID = st.StateProvinceID
    WHERE           SalesOrderID = @SalesOrderID
    )

END

答案 1 :(得分:1)

它应该是on条款。更重要的是,您不能一次设置两个值。所以,将它们连接在一起:

SET @CityState = (
    SELECT          City + ', ' + State
    FROM            Sales.SalesOrderHeader s     
    LEFT OUTER JOIN Person.Address a
    ON              s.ShipToAddressID = a.AddressID
    LEFT OUTER JOIN Person.StateProvince st
    ON           SalesOrderID = @SalesOrderID
    );

或者,您可以在没有set的情况下编写它:

    SELECT @CityState = City + ', ' + State
    FROM Sales.SalesOrderHeader s LEFT OUTER JOIN
         Person.Address a
         ON s.ShipToAddressID = a.AddressID LEFT OUTER JOIN
         Person.StateProvince st
         ON SalesOrderID = @SalesOrderID;

使用分号结束每个语句也是一种好习惯。

答案 2 :(得分:1)

最好写一下,而不是有两个表点击,而你提取一些随机日期,这将是一个更好的选择!!

    SELECT        @CityState =  a.City+' '+a.State,@OrderDate = s.OrderDate
    FROM            Sales.SalesOrderHeader s     
    LEFT OUTER JOIN Person.Address a
    ON              s.ShipToAddressID = a.AddressID
    LEFT OUTER JOIN Person.StateProvince st
   ON              a.AddressStateProvinceID = st.StateProvinceID
    WHERE           SalesOrderID = @SalesOrderID

答案 3 :(得分:0)

对于最后一个ON的条件,您没有LEFT OUTER JOIN子句。

根据您使用的RDBMS(由于@变量,我猜测它是Microsoft),可能需要ON子句。