I have a database with a table, which I will call Table1. The database also has 2 table-valued functions. The first function returns a table which I will call Table2 and the second function returns a table which I will call Tabel3. There is also a 4th table, which I will call Table4, which I want to insert records into.
Table1 has the following fields:
Fileno
Description
GeneralCode
The first function takes in 2 parameters (Table1.Fileno, Table1.Description) and returns Table2, which has the following fields:
FileNum
Desc1
Desc2
Desc3
The second function takes in 2 parameters (Table1.Fileno, Table1.GeneralCode) and returns Table3, which has the following fields:
FileNum
Code1
Code2
The forth table, Table4, has the following fields:
CaseNum
Desc1
Desc2
Desc3
Code1
Code2
I wrote the following query which works:
DELETE FROM Table4
INSERT INTO Table4(CaseNum, Desc1, Desc2, Desc3)
SELECT Fileno, Desc1, Desc2, Desc3
FROM Table1
CROSS APPLY function1(FileNo, Description)
This query runs, calls the function (which returns a table), and inserts data into Table4.
Now, I need to modify the query to call a second function, which will return another table, and insert that data into Table4. I have the following code:
DELETE FROM Table4
INSERT INTO Table4(CaseNum, Desc1, Desc2, Desc3, Code1, Code2)
SELECT m.Fileno, d.Desc1, d.Desc2, d.Desc3, c.Code1, c.Code2
FROM Table1 m
INNER JOIN function1(m.Fileno, m.Description) d ON d.FileNum = m.Fileno
LEFT OUTER JOIN function2(m.Fileno, m.GeneralCode) c ON c.FileNum = m.Fileno;
But this code does not work. Intellisense highlights the fields that I am passing to the functions so obviously I can't reference these fields in this manner but I'm not sure how to resolve that.
Any help is greatly appreciated. Thanks!