简体   繁体   English

SQL连接1个表和2个表值函数返回的表

[英]SQL join 1 table and 2 table-valued function returned tables

I have a database with a table, which I will call Table1.我有一个带有表的数据库,我将其称为 Table1。 The database also has 2 table-valued functions.该数据库还有 2 个表值函数。 The first function returns a table which I will call Table2 and the second function returns a table which I will call Tabel3.第一个函数返回一个表,我将调用 Table2,第二个函数返回一个表,我将调用 Tabel3。 There is also a 4th table, which I will call Table4, which I want to insert records into.还有第 4 个表,我将其称为 Table4,我想将记录插入其中。

Table1 has the following fields:表 1 有以下字段:

Fileno
Description
GeneralCode

The first function takes in 2 parameters (Table1.Fileno, Table1.Description) and returns Table2, which has the following fields:第一个函数接受 2 个参数(Table1.Fileno,Table1.Description)并返回 Table2,它具有以下字段:

FileNum
Desc1
Desc2
Desc3

The second function takes in 2 parameters (Table1.Fileno, Table1.GeneralCode) and returns Table3, which has the following fields:第二个函数接受 2 个参数(Table1.Fileno,Table1.GeneralCode)并返回 Table3,它具有以下字段:

FileNum
Code1
Code2

The forth table, Table4, has the following fields:第四个表 Table4 具有以下字段:

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.此查询运行,调用函数(返回一个表),并将数据插入 Table4。

Now, I need to modify the query to call a second function, which will return another table, and insert that data into Table4.现在,我需要修改查询以调用第二个函数,该函数将返回另一个表,并将该数据插入 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. Intellisense 会突出显示我传递给函数的字段,所以显然我不能以这种方式引用这些字段,但我不确定如何解决这个问题。

Any help is greatly appreciated.任何帮助是极大的赞赏。 Thanks!谢谢!

Your query should probably look something like the following.您的查询应该类似于以下内容。

You can apply multiple table expressions or TVFs, you're showing an outer-join in your sample query so use outer apply where you might (presumably) not get a row returned, and then handle the NULLs, if necessary.您可以应用多个表表达式或 TVF,您在示例查询中显示了一个外部联接,因此在您可能(大概)没有返回行的地方使用外部应用,然后在必要时处理 NULL。

select m.Fileno, 
    f1.Desc1, f1.Desc2, f1.Desc3, 
    IsNull(f2.Code1,''), IsNull(f2.Code2,'')
from Table1 m
cross apply function1(m.FileNo, m.Description)f1
outer apply function2(m.FileNo, m.GeneralCode)f2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM