根据来自其他表的结果从某些表中选择
我有一个小表TBL_A,类似于
ID | fieldName | tableName |
---|---|---|
1 | field1 | tbl_1 |
2 | field2 | tbl_1 |
3 | field3 field3 | tbl_2 |
,我希望能够提出一个函数或proc或我可以从tbl_a指定fieldid的东西,然后查询fieldid正确的字段和表格。 之类的
选择 *来自my_function(3)
最终应该等同于
从tbl_2
选择field3
我一直在研究动态SQL和用户功能,但似乎无法弄清楚如何将查询结果馈入另一个问题。
编辑:
正如@larnu正确地推测,在原始问题中隐藏了一个更大的任务。前提是:
- tblarchive存储带有日期的某些“静态”字段(在其他表中找到)的值。如果/当这些字段在其原始表中更改时,则将记录插入到tblarchive中。或多或少的审核桌。
例如:在tbl_accounts中,AccountFactor字段(FIELDID = 3),用于AccountID = 1在'2022-06-10上从1.0更改为0.5。 插入tblarchive(fieldID,accountId,date,value)值(3,1,'2022-06-10',0.5)
- tblarchive仅在2019年创建。填写2017年以来的记录。也就是说,要插入如果在2017年存在TBLARCHIVE的记录。
- 为了回填,我必须研究真实的审核表(例如,为上一个示例,这将是该特定fieldID的Tblaccountsaudit)。
- 感兴趣的领域及其各自的表格在TBLFields中给出。 tblfields将是原始问题的tbl_a,在给定的示例中,我们将拥有类似
ID | fieldName | tableName |
---|---|---|
3 | AdjundmentFactor | tbl_accounts |
之类的东西,也假设设计就是它的本质,我没有能力大修数据库的设计/结构。
I have a small table tbl_a that is something like
id | fieldName | tableName |
---|---|---|
1 | field1 | tbl_1 |
2 | field2 | tbl_1 |
3 | field3 | tbl_2 |
and I want to be able to come up with a function or proc or something where I can specify the fieldId from tbl_a and then query the correct field and table from that. something like
select * from my_function(3)
should end up being equivalent to
select field3 from tbl_2
I've been looking into dynamic sql and user functions but can't seem to figure out how to feed the results of the query into another.
EDIT:
As @Larnu correctly surmised, there is a larger task hiding behind the one posed in the original question. The premise is this:
- tblArchive stores the values of certain "static" fields (found in other tables) with a Date attached. If/when these fields are changed in their original table, then a record is inserted into tblArchive. More-or-less an audit table.
eg: in tbl_accounts, AdjustmentFactor field (fieldId=3) for accountId=1 changes from 1.0 to 0.5 on '2022-06-10'.Insert into tblArchive (fieldId, accountId, date, value) values (3,1,'2022-06-10',0.5)
- tblArchive was only created in 2019. I've been tasked with back-filling records from 2017 on. That is, to insert records that would have been inserted had tblArchive existed in 2017.
- In order to backfill, I have to look into the real audit tables (for previous example this would be tblAccountsAudit for that particular fieldId).
- The fields of interest and their respective tables are given in tblFields. tblFields would be tbl_a from the original question and for the example given we'd have something like
id | fieldName | tableName |
---|---|---|
3 | AdjustmentFactor | tbl_accounts |
Assume also that the design is what it is and I have no power to overhaul the design/structure of the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您需要这样的东西。
这将使您通过功能返回您所追求的表格。
It sounds like you need something like this.
This will give return the table you are after through a function.