Hello everyone,
I've created a stored procedure that accepts various input parameters and builds a dynamic SQL statement from them. Here is the procedure:
CREATE PROCEDURE dbo.sp_LPXCustomerLimitBrokerSearch
@Status VARCHAR(2),
@RegionCode VARCHAR(3),
@LincsCode VARCHAR(5),
@LocationCode VARCHAR(5),
@DailyLockLimit VARCHAR(10),
@OvernightLockLimit VARCHAR(10),
@BrokerName VARCHAR(50)
AS
DECLARE
@Sql VARCHAR(1000),
@Where VARCHAR(1000)
SET @Sql = 'SELECT [Broker Name], [Broker ID],
[Maximum Best Efforts Loan Lock Caps by Price File],
[Maximum Best Efforts Loan Lock Caps],
[Loan Lock Caps Start Time], [Loan Lock Caps End Time],
[Window Time Zone], [Seller Status], [Enable Second Liens Y/N], [FHA Loan Type ID], [VA Loan Type ID], [Warehouse Seller] FROM tblBrokerLPX'
SET @Where = ' WHERE [Seller Status] = ' + @Status
SET @Where = @Where + CASE WHEN @RegionCode IS NULL THEN '' ELSE ' AND [Branch ID] = ' + @RegionCode END
SET @Where = @Where + CASE WHEN @LincsCode IS NULL THEN '' ELSE ' AND LEFT([Broker ID], 5) = ' + @LincsCode END
SET @Where = @Where + CASE WHEN @LocationCode IS NULL THEN '' ELSE ' AND SUBSTRING([Broker ID], 6, 5) =' + @LocationCode END
SET @Where = @Where + CASE WHEN @DailyLockLimit IS NULL THEN '' ELSE ' AND [Maximum Best Efforts Loan Lock Caps by Price File] = ' + @DailyLockLimit END
SET @Where = @Where + CASE WHEN @OvernightLockLimit IS NULL THEN '' ELSE ' AND [Maximum Best Efforts Loan Lock Caps] = ' + @OvernightLockLimit END
SET @Where = @Where + CASE WHEN @BrokerName IS NULL THEN ''
ELSE ' AND [Broker Name] LIKE ' + @BrokerName + '%' END
SET @Sql = @Sql + @Where
Exec (@Sql)
GO
when I execute '1', null, null, null, null, null, 'Allied' in the query analyzer (for a partial BrokerName search) I get the following error:
Incorrect syntax near '%'.
I know the problem is with the final SET = @Where statement, but I can't figure out what the issue is. Can anyone please help me? Thank you very much!
I've created a stored procedure that accepts various input parameters and builds a dynamic SQL statement from them. Here is the procedure:
CREATE PROCEDURE dbo.sp_LPXCustomerLimitBrokerSearch
@Status VARCHAR(2),
@RegionCode VARCHAR(3),
@LincsCode VARCHAR(5),
@LocationCode VARCHAR(5),
@DailyLockLimit VARCHAR(10),
@OvernightLockLimit VARCHAR(10),
@BrokerName VARCHAR(50)
AS
DECLARE
@Sql VARCHAR(1000),
@Where VARCHAR(1000)
SET @Sql = 'SELECT [Broker Name], [Broker ID],
[Maximum Best Efforts Loan Lock Caps by Price File],
[Maximum Best Efforts Loan Lock Caps],
[Loan Lock Caps Start Time], [Loan Lock Caps End Time],
[Window Time Zone], [Seller Status], [Enable Second Liens Y/N], [FHA Loan Type ID], [VA Loan Type ID], [Warehouse Seller] FROM tblBrokerLPX'
SET @Where = ' WHERE [Seller Status] = ' + @Status
SET @Where = @Where + CASE WHEN @RegionCode IS NULL THEN '' ELSE ' AND [Branch ID] = ' + @RegionCode END
SET @Where = @Where + CASE WHEN @LincsCode IS NULL THEN '' ELSE ' AND LEFT([Broker ID], 5) = ' + @LincsCode END
SET @Where = @Where + CASE WHEN @LocationCode IS NULL THEN '' ELSE ' AND SUBSTRING([Broker ID], 6, 5) =' + @LocationCode END
SET @Where = @Where + CASE WHEN @DailyLockLimit IS NULL THEN '' ELSE ' AND [Maximum Best Efforts Loan Lock Caps by Price File] = ' + @DailyLockLimit END
SET @Where = @Where + CASE WHEN @OvernightLockLimit IS NULL THEN '' ELSE ' AND [Maximum Best Efforts Loan Lock Caps] = ' + @OvernightLockLimit END
SET @Where = @Where + CASE WHEN @BrokerName IS NULL THEN ''
ELSE ' AND [Broker Name] LIKE ' + @BrokerName + '%' END
SET @Sql = @Sql + @Where
Exec (@Sql)
GO
when I execute '1', null, null, null, null, null, 'Allied' in the query analyzer (for a partial BrokerName search) I get the following error:
Incorrect syntax near '%'.
I know the problem is with the final SET = @Where statement, but I can't figure out what the issue is. Can anyone please help me? Thank you very much!