根据来自其他表的结果从某些表中选择

发布于 2025-02-06 08:41:17 字数 1373 浏览 2 评论 0原文

我有一个小表TBL_A,类似于

IDfieldNametableName
1field1tbl_1
2field2tbl_1
3field3 field3tbl_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,在给定的示例中,我们将拥有类似
IDfieldNametableName
3AdjundmentFactortbl_accounts

之类的东西,也假设设计就是它的本质,我没有能力大修数据库的设计/结构。

I have a small table tbl_a that is something like

idfieldNametableName
1field1tbl_1
2field2tbl_1
3field3tbl_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
idfieldNametableName
3AdjustmentFactortbl_accounts

Assume also that the design is what it is and I have no power to overhaul the design/structure of the database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与他有关 2025-02-13 08:41:17

听起来您需要这样的东西。

CREATE FUNCTION myfunction (
    @key INT
)
RETURNS TABLE
AS
RETURN
    SELECT 
        id,
fieldName,
tableName
    FROM
        tbl_a
    WHERE
        id = @key;

这将使您通过功能返回您所追求的表格。

It sounds like you need something like this.

CREATE FUNCTION myfunction (
    @key INT
)
RETURNS TABLE
AS
RETURN
    SELECT 
        id,
fieldName,
tableName
    FROM
        tbl_a
    WHERE
        id = @key;

This will give return the table you are after through a function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文