如何检测列是否是计算列

发布于 2024-12-07 10:00:59 字数 1038 浏览 0 评论 0 原文

我正在使用 SQL Server 2000 和 Java。

我正在创建一个应用程序,除其他外,它从表中读取元数据,在另一台服务器上重新创建它们并携带数据。

其中一个表是这样创建的:

CREATE TABLE some_table (
    Date datetime NOT NULL,     
    Code int NOT NULL,
    SameCodeAgainWTF AS Code 
)

如何检测“SameCodeAgainWTF”列是计算列,以便我在数据迁移阶段不会尝试在其中插入值?

我已经从连接中获取了 DatabaseMetaData 对象。但我无法找到为我提供该信息的方法。

...
DatabaseMetaData dbMetaData = connection.getMetaData();
...

提前致谢。

编辑1:

我想知道是否有不运行另一个查询的解决方案。是否有办法从 DatabaseMetaData 或 ResultSetMetaData 获取信息。

我知道我可以查询系统列,但我想避免它。

SELECT 
    sysobjects.name AS TableName, 
    syscolumns.name AS ColumnName
FROM syscolumns
    INNER JOIN sysobjects
    ON syscolumns.id = sysobjects.id
    AND sysobjects.xtype = 'U' --User Tables
WHERE syscolumns.iscomputed = 1

来自:获取数据库表中计算列的列表(SQL服务器)

I'm working with SQL Server 2000 and Java.

I am creating an application that, among other things, reads the metadata from the tables, recreates them at another server and carry the data.

One of the tables was created this way:

CREATE TABLE some_table (
    Date datetime NOT NULL,     
    Code int NOT NULL,
    SameCodeAgainWTF AS Code 
)

How do I detect that the "SameCodeAgainWTF" column is a computed column so I don't try to insert values at it during the data migration phase?

I already got the DatabaseMetaData object from the connection. But I was unable to find a method that gives me that information.

...
DatabaseMetaData dbMetaData = connection.getMetaData();
...

Thanks in advance.

EDIT 1:

I want to know if there is a solution without running another query. If there is a way to get the information from the DatabaseMetaData or the ResultSetMetaData.

I know I can query the syscolumns but I want to avoid it.

SELECT 
    sysobjects.name AS TableName, 
    syscolumns.name AS ColumnName
FROM syscolumns
    INNER JOIN sysobjects
    ON syscolumns.id = sysobjects.id
    AND sysobjects.xtype = 'U' --User Tables
WHERE syscolumns.iscomputed = 1

From: Get List of Computed Columns in Database Table (SQL Server)

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

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

发布评论

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

评论(1

淡忘如思 2024-12-14 10:00:59

查询包含 syscolumns >已计算 列。

SELECT o.name as TableName, c.name as ComputedColumnName
    FROM sysobjects o
        INNER JOIN syscolumns c
            ON o.id = c.id
                AND c.iscomputed = 1
    WHERE o.xtype = 'u'

Query syscolumns which contains an iscomputed column.

SELECT o.name as TableName, c.name as ComputedColumnName
    FROM sysobjects o
        INNER JOIN syscolumns c
            ON o.id = c.id
                AND c.iscomputed = 1
    WHERE o.xtype = 'u'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文