I am using an SQL function to return entries, which are in two tables. Table A contains information about an UserId
, FileID
and Created
DateTime.
Table B contains information about an UserID
, FileID
and Transmission State
.
The function should return the entries of UserID
, FileID
, Created
and State
The function looks like this:
WITH FindNewestVersion as
(
Select Created AS cDate, FileId AS fId, UserId AS uId, ROW_NUMBER() over (partition by FileId, UserId ORDER BY Created DESC)rn from TableA
where Created <= @dueDate
)
SELECT * from TableB AS B, TableA AS A
INNER JOIN (Select cDate, userId, FileId from FindNewestVersion where rn = 1) AS x
ON A.Created = cDate AND A.UserId = uId AND A.FileId = fId
WHERE A.UserId = ISNULL(@userId, A.UserId) AND A.FileId = ISNULL(@fileId, q.FileId)
AND B.TaskType = 'Transmission'
And the call of the function I am using in C# looks like this:
select A.UserId, A.FileId, A.Created, A.VersionId,
case when B.variableColumn1 is null then null else B.variable1 end AS TransState
from dbo.LSFN_GetMatrixAtDueDate([PARAMETERS]) AS A left join TableB AS B
ON A.VersionId = B.variableColumn2 AND A.VersionId = B.variableColumn3
I am using this call, because the Columns in Table B can contain different information for various tasks and I have to determine the correct column with the help of a config file for the "Transmission Task". But I don't wanted to use a dynamic SQL, so I can use columnNames as parameters within the function itself.
So, this function and the call are almost working correctly. Let's do an example.
Table A contains:
UserID | FileID | Created
--------------------------------
UserA | FileA | 01.01.2011
UserA | FileB | 01.01.2011
UserC | FileC | 01.01.2011
Table B contains:
Variable1 (State) | Variable2 (Version/File) | Variable3 (UserId)
------------------------------------------------------------------
Completed | FileA | UserA
Completed | FileC | UserC
So, lets say, I want to get all entries of both tables, which belong to UserA the result should look like this:
UserID | FileID | Created | State
----------------------------------------
UserA | FileA | 01.01.2011 | Completed
UserA | FileB | 01.01.2011 | NULL
However, I am getting multple entries for each results, so the result looks like this:
UserID | FileID | Created | State
----------------------------------------
UserA | FileA | 01.01.2011 | Completed
UserA | FileA | 01.01.2011 | Completed
UserA | FileB | 01.01.2011 | NULL
UserA | FileB | 01.01.2011 | NULL
It is somehow related to the amount of entries in Table B. If I delete the entry of UserC, I will get all result entries correct, but if I add another row to TableB (so that there are 3 entries), I will get each result three times. I guess it has something to do with the select, where I am calling the function, but I don't know what is wrong there...