在同一查询中对 SELECT 使用更新
Use an UPDATE on a SELECT in the same query
我目前在 MS Access 中使用两个查询将数据插入我的 table OM_Extract。
查询 1:
INSERT INTO OM_Extract (cod, type, name, level)
SELECT table1.cod, table1.type, table2.name, table2.level
FROM table1 INNER JOIN table2 ON table1.cod = table2.cod;
查询 2:
UPDATE OM_Extract INNER JOIN table3 ON OM_Extract.level = table3.level SET OM_Extract.region = table3.region, OM_Extract.description = table3.description;
我正在从我正在使用的 VB 应用程序中的最终 table (OM_Extract) 获取数据,但我现在这样做的方式,它强制我总是在我的 Access 中 运行 一个 AutoExec 宏,以便我的 OM_Extract table 得到更新。
我想创建一个不将数据插入 table 的查询,这样我就可以在我的 VB 应用程序中直接从该查询中获取数据。
会是这样的:
UPDATE
SELECT table1.cod, table1.type, table2.name, table2.level AS OM_Extract
FROM table1 INNER JOIN table2 ON table1.cod = table2.cod
INNER JOIN table3 ON OM_Extract.level = table3.level SET OM_Extract.region = table3.region, OM_Extract.description = table3.description;
我该怎么做?
您似乎正试图从规范化的 table 结构中提取数据。在这种情况下,无需更新。如果要更新 table 2 中的数据,请使用单独的更新查询。因此,甚至不需要引用 Table2.level 或 Table2.region,我将它们排除在示例之外。
假设:
'resulting sql for saved query OM_Extract
'either just call the saved function or duplicate the sql
SELECT Table1.cod, Table1.type, Table2.table2name AS name, Table2.level, Table3.region, Table3.description
FROM (Table1 INNER JOIN Table2 ON Table1.cod = Table2.cod) INNER JOIN Table3 ON Table2.level = Table3.level;
备注:
name是access中的保留字。处理该问题的一种方法是在访问 table 中将名称更改为其他内容,稍后再转换回名称。
我们假设使用 INNER JOINS 没有丢失数据。根据实际情况,我们可能会使用其他连接和/或将列包装在 nz 函数中
我目前在 MS Access 中使用两个查询将数据插入我的 table OM_Extract。
查询 1:
INSERT INTO OM_Extract (cod, type, name, level)
SELECT table1.cod, table1.type, table2.name, table2.level
FROM table1 INNER JOIN table2 ON table1.cod = table2.cod;
查询 2:
UPDATE OM_Extract INNER JOIN table3 ON OM_Extract.level = table3.level SET OM_Extract.region = table3.region, OM_Extract.description = table3.description;
我正在从我正在使用的 VB 应用程序中的最终 table (OM_Extract) 获取数据,但我现在这样做的方式,它强制我总是在我的 Access 中 运行 一个 AutoExec 宏,以便我的 OM_Extract table 得到更新。
我想创建一个不将数据插入 table 的查询,这样我就可以在我的 VB 应用程序中直接从该查询中获取数据。
会是这样的:
UPDATE
SELECT table1.cod, table1.type, table2.name, table2.level AS OM_Extract
FROM table1 INNER JOIN table2 ON table1.cod = table2.cod
INNER JOIN table3 ON OM_Extract.level = table3.level SET OM_Extract.region = table3.region, OM_Extract.description = table3.description;
我该怎么做?
您似乎正试图从规范化的 table 结构中提取数据。在这种情况下,无需更新。如果要更新 table 2 中的数据,请使用单独的更新查询。因此,甚至不需要引用 Table2.level 或 Table2.region,我将它们排除在示例之外。 假设:
'resulting sql for saved query OM_Extract
'either just call the saved function or duplicate the sql
SELECT Table1.cod, Table1.type, Table2.table2name AS name, Table2.level, Table3.region, Table3.description
FROM (Table1 INNER JOIN Table2 ON Table1.cod = Table2.cod) INNER JOIN Table3 ON Table2.level = Table3.level;
备注: name是access中的保留字。处理该问题的一种方法是在访问 table 中将名称更改为其他内容,稍后再转换回名称。
我们假设使用 INNER JOINS 没有丢失数据。根据实际情况,我们可能会使用其他连接和/或将列包装在 nz 函数中