Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Returning Recordset from Function

Author  Topic 

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2010-08-09 : 08:32:55
Hi

I 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, 10

Then 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 parameters
RETURNS @MissingVal TABLE
(MissingVal VARCHAR(16))
AS
BEGIN
DECLARE @StartNumber VARCHAR(16)
DECLARE @EndNumber VARCHAR(16)
DECLARE @Prefix VARCHAR(7)
DECLARE @Count BIGINT
DECLARE @LowVal BIGINT
DECLARE @HighVal BIGINT
DECLARE @ValueNo VARCHAR(16)
DECLARE @ValReturned VARCHAR(16)
DECLARE @Tab Table

(
ValueNo VARCHAR(16)
)

SET @StartNumber = @MinVal
SET @EndNumber = @MaxVal
SET @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 value

WHILE @Count <= @HighVal -- While the tissue number is lower than the max parameter value keep looping

BEGIN
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
END
END
RETURN
END


So 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

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-09 : 09:21:02
can you give sample of your table where you store you data and you want to check for "missing numbers".

thanks
Go to Top of Page

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2010-08-09 : 10:44:25
Do you mean the DB table or the temp table or both?
Go to Top of Page
   

- Advertisement -