1

我正在尝试编写一个脚本来从旧的 MS SQL 6.5 中提取数据库,我偶然发现了检索用户类型

我有以下自定义类型:

在此处输入图像描述

我写了一个简单的脚本,其中一个基本名称从同一张表中查找:

select t1.name as name, t2.name as base_name, t1.length as length 
from systypes t1, systypes t2 
where t1.usertype > 100 and t2.usertype <= 100 and t1.type = t2.type

它返回我以下结果:

name                           base_name                      length 
------------------------------ ------------------------------ ------ 
bitZ                           bit                            1      
tinyintZ                       tinyint                        1      
smallintZ                      smallint                       2      
intZ                           int                            4      
smalldatetimeZ                 smalldatetime                  4      
charZ_254                      sysname                        254    
charZ_254                      varchar                        254    
charZ_80                       sysname                        80     
charZ_80                       varchar                        80     
charZ_50                       sysname                        50     
charZ_50                       varchar                        50     
charZ_40                       sysname                        40     
charZ_40                       varchar                        40     
charZ_35                       sysname                        35     
charZ_35                       varchar                        35     
charZ_30                       sysname                        30     
charZ_30                       varchar                        30     
charZ_20                       sysname                        20     
charZ_20                       varchar                        20     
charZ_16                       sysname                        16     
charZ_16                       varchar                        16     
charZ_14                       sysname                        14     
charZ_14                       varchar                        14     
charZ_12                       sysname                        12     
charZ_12                       varchar                        12     
charZ_10                       sysname                        10     
charZ_10                       varchar                        10     
charZ_8                        sysname                        8      
charZ_8                        varchar                        8      
charZ_5                        sysname                        5      
charZ_5                        varchar                        5      
charZ_4                        sysname                        4      
charZ_4                        varchar                        4      
charZ_1                        sysname                        1      
charZ_1                        varchar                        1      
charZ_120                      sysname                        120    
charZ_120                      varchar                        120    
binZ_10                        binary                         10     
binZ_10                        timestamp                      10     

这里有2个问题:

  1. varchar 解析为两种类型,sysname 和 varchar,因为它们具有相同的基本类型。binZ_10 同样的问题
  2. 非 var char 作为非零长度返回

我可以使用哪个脚本来获得正确的 MS 输出?

这是原始表的片段:

select type, name from systypes order by type

type name                           
---- ------------------------------ 
. . .                  
39   charZ_80                       
39   sysname                        
39   varchar                        
45   binary                         
45   binZ_10                        
45   timestamp                      
47   char     
. . .    
4

1 回答 1

0

我使用了跟踪,结果由 sp_MShelptype 返回。

我需要一个字段子集,所以我将结果推送到临时表中:

create table #Result
(
    UserDatatypeName varchar(92), owner varchar(20), basetypename varchar(92), defaultname varchar(61), rulename varchar(61) null, tid int, length int, nullable int, dt_prec int null, dt_scale int null, dt_flags int, allowidentity int
)

insert #Result exec sp_MShelptype null, 'uddt'

select UserDatatypeName name, basetypename base_type, length, defaultname default from #Result

drop table #Result
于 2012-12-08T21:29:07.093 回答