SQL Server 2005存储过程问题

时间:2011-06-20 06:40:12

标签: sql-server-2005

我正在编写这样的存储过程:

create PROC uspInvCustomerLineItemGetList1(@CustomerID varchar(50))
AS
BEGIN   
    SELECT LineItemID, LineItemName 
    from tblInvoiceLineItems
    where CustomerID = @CustomerID 
       or CustomerID = '' 
       and AccountNumber = (select AccountNumber from tblInvAccountDetails 
                            where AccountTypeID = 10 and 20)

END

我的问题是子查询传递了2个或更多值(select AccountNumber from tblInvAccountDetails where AccountTypeID = 10 and 20),因此我将根据accountnumbers (1000, 10003, 1006)比较AccountTypeID = 10 and 20

我该如何编写存储过程?

谢谢

hemanth

1 个答案:

答案 0 :(得分:2)

使用IN而不是=

...
where
    CustomerID=@CustomerID
    or
    CustomerID=''
    and
    AccountNumber IN  (select AccountNumber from tblInvAccountDetails where AccountTypeID IN (10, 20))

注意:

  • AccountTypeID不能同时为10和20:我假设您的意思是OR。 IN是多个OR的简写
  • AND优先于OR,因此请检查您是否需要()某处