运行具有动态生成where条件的存储过程时出错

时间:2019-07-11 02:49:41

标签: sql-server

我有一个select语句,该语句具有基于传入参数生成的 where 条件。 `

DECLARE @ApplicationNumber  int = 0;
DECLARE @AccountReferenceNumber  int= 4820829;
DECLARE @SecurityNumber int = 1;
DECLARE @StreetAddress1  varchar(250)= '15 Prosper Court';
DECLARE @StreetAddress2 varchar(250) = NULL;
DECLARE @Suburb varchar(250)= 'wong';
DECLARE @State varchar(250) = NULL;
DECLARE @Postcode varchar(250) = '1245';
DECLARE @IsDeleted  bit = 0;
DECLARE @IsClass bit  = 1;
declare    @BaseQuery nvarchar(max) = ' ';
IF @IsClass = 1 
 DECLARE @tableid INT = 0;


  DECLARE @WhereClause VARCHAR(max) = '';

        --SET @WhereClause =  @AccountReferenceNumber ; 

          IF @StreetAddress1 IS NOT NULL 
            BEGIN 
                SET @WhereClause = @WhereClause + ' AND  StreetAddress1 = '+ @StreetAddress1; 

            END 

          IF @StreetAddress2 IS NOT NULL 
            BEGIN 
                SET @WhereClause = @WhereClause 
                                   + ' AND StreetAddress2 = ' + @StreetAddress2; 
            END 

          IF @Suburb IS NOT NULL 
            BEGIN 
                SET @WhereClause = @WhereClause + ' AND Suburb= ' + @Suburb; 
            END 

          IF @Postcode IS NOT NULL 
            BEGIN 
                SET @WhereClause = @WhereClause + ' AND Postcode= '+ @Postcode; 
            END 


            SET  @BaseQuery = 'SELECT LoanSecurityId FROM LoanSecurity WHERE AccountReferenceNumber = @AccountReferenceNumber' 
          + @WhereClause

EXEC sp_executesql @BaseQuery, N'@AccountReferenceNumber int', @AccountReferenceNumber

我执行了该语句,随后出现了以下错误

  

“繁荣”附近的语法不正确。

有人可以阐明以下内容中我缺少的内容

建议和更改完成后,以下是执行时可用的选择语句

  

从LoanSecurity中选择LoanSecurityId,其中AccountReferenceNumber = @AccountReferenceNumber AND St​​reetAddress1 = 15 Prosper Court和Suburb = WODONGA AND Postcode = 1245

字符串在哪里子句不正确

4 个答案:

答案 0 :(得分:0)

您不能仅将字符串附加到查询where子句中。您需要使用动态SQL

SET  @sql = 'SELECT LoanSecurityId FROM LoanSecurity WHERE AccountReferenceNumber = @AccountReferenceNumber' 
          + @WhereClause

EXEC sp_executesql @sql, N'@AccountReferenceNumber int', @AccountReferenceNumber

答案 1 :(得分:0)

具有int值时,应使用cast(@Param as nvarchar(n)) 例如:

IF @Postcode IS NOT NULL 
            BEGIN 
                SET @WhereClause = @WhereClause + ' AND Postcode =cast(@Postcode as nvarchar(50)) '; 
            END 

答案 2 :(得分:0)

调整输入字符串并更改以下代码后,它可以正常工作

DECLARE @ApplicationNumber  int = 0;
DECLARE @AccountReferenceNumber  int;
set @AccountReferenceNumber= 820829;
DECLARE @SecurityNumber int = 1;
DECLARE @StreetAddress1  varchar(250)= '''15 prprpCourt''';
DECLARE @StreetAddress2 varchar(250) = NULL;
DECLARE @Suburb varchar(250)= '''tester''';
DECLARE @State varchar(250) = NULL;
DECLARE @Postcode varchar(250) = '''5589''';
DECLARE @IsDeleted  bit = 0;
DECLARE @IsClass bit  = 1;
declare    @BaseQuery nvarchar(max) = N'SELECT LoanSecurityId
            FROM LoanSecurity WHERE AccountReferenceNumber = ';
IF @IsClass = 1 
 DECLARE @tableid INT = 0;


  DECLARE @WhereClause VARCHAR(max) = '';

        SET @WhereClause =  @AccountReferenceNumber ; 

          IF @StreetAddress1 IS NOT NULL 
            BEGIN 
                SET @WhereClause = @WhereClause + ' AND  StreetAddress1 = ' + @StreetAddress1; 

            END 

          IF @StreetAddress2 IS NOT NULL 
            BEGIN 
                SET @WhereClause = @WhereClause 
                                   + ' AND StreetAddress2 = ' + @StreetAddress2; 
            END 

          IF @Suburb IS NOT NULL 
            BEGIN 
                SET @WhereClause = @WhereClause + ' AND Suburb= ' + @Suburb; 
            END 

          IF @Postcode IS NOT NULL 
            BEGIN 
                SET @WhereClause = @WhereClause + ' AND Postcode= ' + @Postcode; 
            END 


          SET @BaseQuery = @BaseQuery + @WhereClause;

         -- SELECT @BaseQuery
          execute sp_executesql @BaseQuery;

答案 3 :(得分:0)

如果您动态构建SQL语句,则您使用的SQL错误。仅有极少数情况需要这样做,通常是在执行时不知道表或视图名称的情况下。

您可以执行查询而无需使用ISNULL动态构造SQL语句。

DECLARE @ApplicationNumber int = 0;
DECLARE @AccountReferenceNumber int = 820829;
DECLARE @SecurityNumber int = 1;
DECLARE @StreetAddress1  varchar(250) = '15 prprpCourt';
DECLARE @StreetAddress2 varchar(250) = NULL;
DECLARE @Suburb varchar(250)= 'tester';
DECLARE @State varchar(250) = NULL;
DECLARE @Postcode varchar(250) = '5589';
DECLARE @IsDeleted  bit = 0;
DECLARE @IsClass bit  = 1;

-- Not sure what this is for ...
IF @IsClass = 1 
    DECLARE @tableid INT = 0;

SELECT
    LoanSecurityId
FROM
    LoanSecurity
WHERE
    AccountReferenceNumber = @AccountReferenceNumber AND
    StreetAddress1 = ISNULL(@StreetAddress1, StreetAddress1) AND
    StreetAddress2 = ISNULL(@StreetAddress2, StreetAddress2) AND
    Suburb = ISNULL(@Suburb, Suburb) AND
    Postcode = ISNULL(@Postcode, Postcode)

P.S。 ...如果您使用本机SQL而不是动态创建SQL语句,则执行性能通常会更好。