sql-server - 如何将ms sql表模式导出到excel

标签 sql-server excel

有没有办法将所有表模式导出到excel?

enter image description here

最佳答案

我从 SP_HELP 的定义中提取了一些代码供引用,这可能对您有所帮助,但您仍然必须手动复制 excel 中的数据:

select 
    'Table_name'            = ao.name,
    'Column_name'           = ac.name,
    'Type'                  = type_name(user_type_id),
    'Computed'              = case when ColumnProperty(ac.object_id, ac.name, 'IsComputed') = 0 then 'no' else 'yes' end,
    'Length'                    = convert(int, max_length),
    -- for prec/scale, only show for those types that have valid precision/scale
    -- Search for type name + ',', because 'datetime' is actually a substring of 'datetime2' and 'datetimeoffset'
    'Prec'                  = case when charindex(type_name(system_type_id) + ',',  N'tinyint,smallint,decimal,int,bigint,real,money,float,numeric,smallmoney,date,time,datetime2,datetimeoffset,') > 0
                                then convert(char(5),ColumnProperty(ac.object_id, ac.name, 'precision'))
                                else '     ' end,
    'Scale'                 = case when charindex(type_name(system_type_id) + ',',  N'tinyint,smallint,decimal,int,bigint,real,money,float,numeric,smallmoney,date,time,datetime2,datetimeoffset,') > 0
                                then convert(char(5),OdbcScale(system_type_id,scale))
                                else '     ' end,
    'Nullable'              = case when is_nullable = 0 then 'no' else 'yes' end,
    'TrimTrailingBlanks'    = case ColumnProperty(ac.object_id, ac.name, 'UsesAnsiTrim')
                                when 1 then 'no'
                                when 0 then 'yes'
                                else '(n/a)' end,
    'FixedLenNullInSource'  = case
                when type_name(system_type_id) not in ('varbinary','varchar','binary','char')
                    then '(n/a)'
                when is_nullable = 0 then 'no' else 'yes' end,
    'Collation'     = collation_name
from sys.all_columns ac
    INNER JOIN sys.all_objects ao
        ON ac.object_id = ao.object_id and ao.type = N'U'
ORDER BY ao.object_id, ac.column_id

关于sql-server - 如何将ms sql表模式导出到excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40043634/

相关文章:

asp.net - 偶发的 ASP.NET 数据错误 : "Cannot find table 0"

SQL 如何在不使用超前和滞后的情况下更新上一行和下一行值?

excel - 如何根据唯一 ID 重建数据集

vba - 如何检查单元格值以更改另一个单元格的值?

c# - OpenXML(SAX 方法)- 将行添加到现有选项卡

SQL UPDATE SELECT 与 WHERE

sql-server - 如何禁用现有列的身份&添加数据后,如何在 SQL Server 2008 中的同一列上再次启用身份?

c# - 登录前握手期间发生错误

python - 组织从 Excel 读取到 Pandas DataFrame 的数据

excel - (excel 2013) 具有 VLOOKUP 功能的 3D 公式