如何在查询结尾添加变量?

时间:2013-07-23 14:56:23

标签: sql-server-2008 tsql stored-procedures

我正在尝试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` 

但它给了我这个错误

  

'+'附近的语法不正确。

3 个答案:

答案 0 :(得分:3)

为了让您的代码按原样运行,您需要动态SQL:

DECLARE @Query NVARCHAR(MAX)
SET @Query = 'SELECT RouteName
        ,SalesManCode
        ,SalesManName
        ,CustomerCode
        ,CustomerNameEng
        ,TransactionDateTime
        ,DocumentPrefix
        ,DocumentNumber
        ,BalanceDueAmount
        ,RouteID
        ,PaymentType  FROM v_DCS_Rpt_Total_Sales '+@CONDITION
EXEC(@SQL)

现在,你真的不需要在这里使用动态SQL,你可以直接使用它而不用所有IF

SELECT   RouteName
        ,SalesManCode
        ,SalesManName
        ,CustomerCode
        ,CustomerNameEng
        ,TransactionDateTime
        ,DocumentPrefix
        ,DocumentNumber
        ,BalanceDueAmount
        ,RouteID
        ,PaymentType
FROM v_DCS_Rpt_Total_Sales
WHERE (@SALESMANCODE = 'ALL' OR SalesManCode = @SALESMANCODE)
AND (@FROM_DATE IS NULL OR TransactionDateTime >= @FROM_DATE)
AND (@TO_DATE IS NULL OR TransactionDateTime <= @TO_DATE)

答案 1 :(得分:0)

您可以使用动态SQL使用EXECUTE语句或sp_executesql执行字符串命令。整个命令需要是一个字符串;在你的情况下,你只是试图将一个字符串添加到正常命令的末尾,这不起作用。

这是一个例子......

DECLARE @someBool;
SET @someBool = true;
DECLARE @query VARCHAR(MAX);
SET @query = 'SELECT * FROM table1';
SET @condition1 = ' WHERE column1 = 1';
SET @condition2 = ' WHERE column2 = 2';

IF(@someBool)
  SET @query = @query + @condition1;
ELSE
  SET @query = @query + @condition2;

EXECUTE(@query);

这个例子虽然不成熟,但展示了如何使用EXECUTE构建命令字符串并执行它。您也可以只使用sp_executesql。

有关EXECUTE和sp_executesql之间区别的更多详细信息,请参阅此文章。

Dynamic SQL - EXEC(@SQL) versus EXEC SP_EXECUTESQL(@SQL)

答案 2 :(得分:0)

在这种简单的情况下不需要动态SQL ..

SELECT RouteName
        ,SalesManCode
        ,SalesManName
        ,CustomerCode
        ,CustomerNameEng
        ,TransactionDateTime
        ,DocumentPrefix
        ,DocumentNumber
        ,BalanceDueAmount
        ,RouteID
        ,PaymentType  
FROM v_DCS_Rpt_Total_Sales
WHERE SalesManCode = CASE WHEN @SALESMANCODE = 'ALL' THEN SalesManCode ELSE @SALESMANCODE AND
      TransactionDateTime >= ISNULL(@FROM_DATE,TransactionDateTime) AND
      TransactionDateTime <= ISNULL(@TO_DATE,TransactionDateTime)