如何获得带有括号和所有内容的 SQL Server 列定义?

发布于 2024-07-08 21:13:11 字数 333 浏览 9 评论 0原文

我需要一种智能方法,以可在 CREATE TABLE 语句中使用的方式从 INFORMATION_SCHEMA.COLUMNS 中获取数据类型。 问题是需要理解“额外”字段,例如 NUMERIC_PRECISION 和 NUMERIC_SCALE。

显然,我可以忽略 INTEGER 列(精度为 10,小数位数为 0),但我还会对其他类型感兴趣,例如 NUMERIC。 因此,无需编写大量代码来解析表,关于如何从列定义中获取某种字段简写的任何想法?

我希望能够得到类似的东西: 整数, 约会时间, 钱, 数字**(10,2)**

I need a smart way to get the data types out of INFORMATION_SCHEMA.COLUMNS in a way that could be used in a CREATE TABLE statement. The problem is the 'extra' fields that need to be understood, such as NUMERIC_PRECISION and NUMERIC_SCALE.

Obviously, I can ignore the columns for INTEGER (precision of 10 and scale of 0), but there are other types I would be interested in, such as NUMERIC. So without writing lots of code to parse the table, any ideas on how to get a sort of field shorthand out of the column definition?

I would like to be able to get something like :
int,
datetime,
money,
numeric**(10,2)**

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

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

发布评论

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

评论(4

热鲨 2024-07-15 21:13:11
select column_type = data_type + 
    case
        when data_type like '%text' then ''
        when data_type like '%char' and character_maximum_length = -1 then '(max)'
        when character_maximum_length is not null then '(' + convert(varchar(10), character_maximum_length) + ')'
        when data_type = 'numeric' then '(' + convert(varchar(10), isnull(numeric_precision, 18)) + ', ' + 
            convert(varchar(10), isnull(numeric_scale, 0)) + ')'
        else ''
    end
,*
from information_schema.columns
select column_type = data_type + 
    case
        when data_type like '%text' then ''
        when data_type like '%char' and character_maximum_length = -1 then '(max)'
        when character_maximum_length is not null then '(' + convert(varchar(10), character_maximum_length) + ')'
        when data_type = 'numeric' then '(' + convert(varchar(10), isnull(numeric_precision, 18)) + ', ' + 
            convert(varchar(10), isnull(numeric_scale, 0)) + ')'
        else ''
    end
,*
from information_schema.columns
后知后觉 2024-07-15 21:13:11

这是 GacticCowboy 的答案的更新(盗版!),用于修复一些问题并更新所有(我认为)SQL Server 2008R2数据类型:

select data_type + 
    case
        when data_type like '%text' or data_type in ('image', 'sql_variant' ,'xml')
            then ''
        when data_type in ('float')
            then '(' + cast(coalesce(numeric_precision, 18) as varchar(11)) + ')'
        when data_type in ('datetime2', 'datetimeoffset', 'time')
            then '(' + cast(coalesce(datetime_precision, 7) as varchar(11)) + ')'
        when data_type in ('decimal', 'numeric')
            then '(' + cast(coalesce(numeric_precision, 18) as varchar(11)) + ',' + cast(coalesce(numeric_scale, 0) as varchar(11)) + ')'
        when (data_type like '%binary' or data_type like '%char') and character_maximum_length = -1
            then '(max)'
        when character_maximum_length is not null
            then '(' + cast(character_maximum_length as varchar(11)) + ')'
        else ''
    end as CONDENSED_TYPE
    , *
from information_schema.columns
order by table_schema, table_name, ordinal_position

Here is an update (ripoff!) of GalacticCowboy's answer to fix some issues and update for all (I think) SQL Server 2008R2 datatypes:

select data_type + 
    case
        when data_type like '%text' or data_type in ('image', 'sql_variant' ,'xml')
            then ''
        when data_type in ('float')
            then '(' + cast(coalesce(numeric_precision, 18) as varchar(11)) + ')'
        when data_type in ('datetime2', 'datetimeoffset', 'time')
            then '(' + cast(coalesce(datetime_precision, 7) as varchar(11)) + ')'
        when data_type in ('decimal', 'numeric')
            then '(' + cast(coalesce(numeric_precision, 18) as varchar(11)) + ',' + cast(coalesce(numeric_scale, 0) as varchar(11)) + ')'
        when (data_type like '%binary' or data_type like '%char') and character_maximum_length = -1
            then '(max)'
        when character_maximum_length is not null
            then '(' + cast(character_maximum_length as varchar(11)) + ')'
        else ''
    end as CONDENSED_TYPE
    , *
from information_schema.columns
order by table_schema, table_name, ordinal_position
抹茶夏天i‖ 2024-07-15 21:13:11

SMO 脚本应该处理脚本生成。 我相信这就是 MS 在 SQL Management Studio 中用于生成脚本的方法。

http://msdn.microsoft.com/en-us/library/ms162153。 aspx

@YourComment - 我需要一种智能方法,以可在 CREATE TABLE 语句中使用的方式从 INFORMATION_SCHEMA.COLUMNS 中获取数据类型

这就是您所要求的。 除此之外,您将必须解析信息模式视图结果。

SMO Scripting should take care of the script generations. I believe that this is what MS uses in SQL Management Studio for script generations.

http://msdn.microsoft.com/en-us/library/ms162153.aspx

@YourComment - I need a smart way to get the data types out of INFORMATION_SCHEMA.COLUMNS in a way that could be used in a CREATE TABLE statement

This is what you asked for. Short of that, you will have to parse the info schema view results.

于我来说 2024-07-15 21:13:11

如果您使用 smo,则可以通过访问 列对象

Column.Property["NumericScale"].Value

Column.Property["NumericPrecision"].Value

If you're using smo you can get both precision and scale by accessing the Properties colletion of the Column Object

Column.Property["NumericScale"].Value

Column.Property["NumericPrecision"].Value

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