Ive seen this posted around this forum and others but havent been able to find a solution to my problem.i have the following SPALTER PROCEDURE [dbo].[supervisorSearch] -- Add the parameters for the stored procedure here @statusIdInt Int,@sectionIdInt Int,@staff varchar(20) As DECLARE @SQL nvarchar(1000)SET @SQL = 'SELECT tbl_Customers.surnameStr, tbl_Customers.ninoStr, tbl_Claim_Status.statusStr, tbl_Claims.targetDateDate, tbl_Claims.BfDateDate, tbl_Claims.claimIdInt, tbl_Customers.firstNameStr, tbl_Customers.dobDate FROM tbl_Customers INNER JOIN tbl_Claims ON tbl_Customers.customerIdInt = tbl_Claims.customerIdInt INNER JOIN tbl_Claim_Status ON tbl_Claims.claimStatusInt = tbl_Claim_Status.claimStatusIdInt WHERE tbl_Claims.claimStatusInt = @statusIdInt'IF @sectionIDInt <> 0SET @SQL = @SQL + ' And sectionIdInt = @sectionIdInt'IF @staff <> 'ALL'SET @SQL = @SQL + ' And allocatedToStr = @staff'EXEC (@SQL)
I understand this wont work as the variables are outside of the dynamic sql statement. So i changed to this based on what i have found on hereALTER PROCEDURE [dbo].[supervisorSearch] -- Add the parameters for the stored procedure here @statusIdInt Int,@sectionIdInt Int,@staff varchar(20) As DECLARE @SQL nvarchar(1000)SET @SQL = 'SELECT tbl_Customers.surnameStr, tbl_Customers.ninoStr, tbl_Claim_Status.statusStr, tbl_Claims.targetDateDate, tbl_Claims.BfDateDate, tbl_Claims.claimIdInt, tbl_Customers.firstNameStr, tbl_Customers.dobDate FROM tbl_Customers INNER JOIN tbl_Claims ON tbl_Customers.customerIdInt = tbl_Claims.customerIdInt INNER JOIN tbl_Claim_Status ON tbl_Claims.claimStatusInt = tbl_Claim_Status.claimStatusIdInt WHERE tbl_Claims.claimStatusInt = '+@statusIdIntIF @sectionIDInt <> 0SET @SQL = @SQL + ' And sectionIdInt = '+@sectionIdIntIF @staff <> 'ALL'SET @SQL = @SQL + ' And allocatedToStr = '+@staffEXEC (@SQL)
But this now brings up an error along the lines of "Conversion failed when converting the varchar value...(sql statement here)...to data type int.My knowledge of SQL is pretty basic and this is my first attempt at a dynamic sql SP.