我正在尝试SELECT
从 SQL Server 2008 获取特定记录,这是我正在使用的代码
ALTER PROCEDURE [dbo].[usp_GetTotalSalesReport]
(@FROM_DATE nchar(20),
@TO_DATE nchar(20),
@SALESMANCODE nchar(8)
)
AS
BEGIN
DECLARE @CONDITION TEXT
IF (@FROM_DATE IS NULL AND @TO_DATE IS NULL AND @SALESMANCODE = 'ALL') BEGIN
SET @CONDITION = NULL
END
IF (@FROM_DATE IS NULL AND @TO_DATE IS NULL AND @SALESMANCODE != 'ALL') BEGIN
SET @CONDITION = ' WHERE SalesManCode= @SALESMANCODE'
END
IF @FROM_DATE IS NOT NULL AND @SALESMANCODE = 'ALL' BEGIN
SET @CONDITION = ' WHERE TransactionDateTime >= @FROM_DATE AND TransactionDateTime <= @TO_DATE'
END
IF @FROM_DATE IS NOT NULL AND @SALESMANCODE != 'ALL' BEGIN
SET @CONDITION = ' WHERE TransactionDateTime >= @FROM_DATE AND TransactionDateTime <= @TO_DATE AND SalesManCode = @SALESMANCODE'
END
IF @FROM_DATE IS NOT NULL AND @TO_DATE IS NOT NULL AND @SALESMANCODE = 'ALL' BEGIN
SET @CONDITION = ' WHERE TransactionDateTime >= @FROM_DATE AND TransactionDateTime <= @TO_DATE'
END
IF @FROM_DATE IS NOT NULL AND @TO_DATE IS NOT NULL AND @SALESMANCODE != 'ALL' BEGIN
SET @CONDITION =' WHERE TransactionDateTime >= @FROM_DATE AND TransactionDateTime <= @TO_DATE AND SalesManCode = @SALESMANCODE'
END
SELECT RouteName
,SalesManCode
,SalesManName
,CustomerCode
,CustomerNameEng
,TransactionDateTime
,DocumentPrefix
,DocumentNumber
,BalanceDueAmount
,RouteID
,PaymentType FROM v_DCS_Rpt_Total_Sales @CONDITION
END
如果我尝试@CONDITION
在查询末尾添加变量,例如
PaymentType FROM v_DCS_Rpt_Total_Sales @CONDITION
我收到此错误
'@CONDITION' 附近的语法不正确
到目前为止,我尝试过的是:
PaymentType FROM v_DCS_Rpt_Total_Sales +" "+ @CONDITION`
但它给了我这个错误
'+' 附近的语法不正确。