I have select statement that has where condition generated based on incoming parameter. `
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
i executed the statement and following errors is coming up
Incorrect syntax near 'Prosper'..
Can some one shed a light what am i missing in the following
After the suggetion and changes done , following is the select statment available at execute
SELECT LoanSecurityId FROM LoanSecurity WHERE AccountReferenceNumber = @AccountReferenceNumber AND StreetAddress1 = 15 Prosper Court AND Suburb= WODONGA AND Postcode= 1245
The string ins the whereclause is not coming proper