HiI have a function where I pass two varchar values and then do a comparison with DB values to check if any of these values are missing in the DB. Using the low value passed in I created a count, adding 1 each time and this gives me the full expected records to compare against. So If I pass in lowval = 1, highval = 10 it will generate 1,2,3,4,5,6,7,8,9, 10Then I can select from DB and check what if any are missing. What I am struggling with is how to return the records all in one go like a select from a table. I currently put the values in a temp table then am trying to do a comparison with DB values but am unsure how to set this up correctly. The comparison may return more than one record so am confused as to how to do this.CREATE FUNCTION RetrieveMissingNumbers (@TMinVal VARCHAR(16), @TMaxVal VARCHAR(16)) -- Pass min and max tissue numbers in as parametersRETURNS @MissingVal TABLE (MissingVal VARCHAR(16)) AS BEGIN DECLARE @StartNumber VARCHAR(16)DECLARE @EndNumber VARCHAR(16)DECLARE @Prefix VARCHAR(7)DECLARE @Count BIGINTDECLARE @LowVal BIGINT DECLARE @HighVal BIGINTDECLARE @ValueNo VARCHAR(16)DECLARE @ValReturned VARCHAR(16)DECLARE @Tab Table (ValueNo VARCHAR(16) )SET @StartNumber = @MinValSET @EndNumber = @MaxValSET @Prefix = SUBSTRING(@StartNumber,1, 7)SET @LowVal = CONVERT(BIGINT, SUBSTRING(@StartNumber,8,6))SET @HighVal = CONVERT(BIGINT, SUBSTRING(@EndNumber,8,6))SET @Count = @LowVal -- Set the count as the min tissue valueWHILE @Count <= @HighVal -- While the tissue number is lower than the max parameter value keep loopingBEGIN SET @ValueNo = @Prefix + RIGHT('000000' + CONVERT(VARCHAR(6), @Count),6) INSERT INTO @Tab(ValueNo) VALUES (dbo.ExternalProcessFunc(@ValueNo,1)) -- For every loop insert the values into the temp table SET @Count = @Count + 1 -- Add the value of 1 onto the existing number /*Having problems working out what to do from here onwards*/ SELECT @ValReturned = ValueNo FROM @Tab WHERE ValueNo NOT IN ( SELECT DISTINCT Table1.Column FROM Table1 WHERE (SUBSTRING(Table1.Column,2,12) >= SUBSTRING(@StartNumber, 2,12) Or @StartNumber = NULL) AND (SUBSTRING(Table1.Column, 2,12) <= SUBSTRING(@EndNumber,2,12) Or @EndNumber = NULL) IF @ValReturned <> '' BEGIN INSERT INTO @MissingVal (MissingVal) SELECT ValueNo FROM @Tab ENDENDRETURN ENDSo I do not know if I have explained enough for you to understand what I am doing but am trying to return the missing values when comparing the list built in the temp table against DB values, but not sure what i'm doing at some of the bits or whether I might need to change a lot of the code to allow it to work.Any help would be appreciated.G