T-SQL将系统表连接到用户表中的值SYS.TABLES,SYS.TYPES,SYS.COLUMNS

时间:2017-06-04 00:02:08

标签: sql-server tsql sql-server-2008-r2

我有这个查询选择像CODE_这样的表。我想将这些结果连接到每个CODE表,如CODE_COUNTRY,CODE_COUNTY,以列出SHORT_DESC的值,它将是以下值:United States,Mexico for Country;布朗,绿色为县;男性,女性为性别*。 SHORT_DESC中的值与转换表PC_Column *中的值匹配。所以我需要加入Transformations表来查找匹配的列。并且左连接,右连接以显示与数据库不匹配的列。如何找到要连接到Transformations表的系统列?

SELECT tb.[schema_id] AS 'Schema'
   ,tb.[OBJECT_ID] AS 'TableObj'
   ,tb.[NAME] AS 'TableName' 
   ,C.NAME as 'Column'
   ,T.name AS 'Type'
   ,C.max_length
   ,C.is_nullable
FROM 
    SYS.COLUMNS C 
INNER JOIN 
    SYS.TABLES tb ON tb.[object_id] = C.[object_id]
INNER JOIN 
    SYS.TYPES T ON C.system_type_id = T.user_type_id
--INNER JOIN 
--    Bridge_test.dbo.Transformations TF ON TF.PC_Column = C.NAME
WHERE
    tb.[is_ms_shipped] = 0 
    AND tb.[NAME] LIKE '%code_%'
    AND C.name = 'SHORT_DESC'
    --C.NAME LIKE '%country%'
ORDER BY 
    tb.[Name]

*注意:这些是要添加到此查询的列

查询结果

Schema  TableObj    TableName   Column  Type    max_length  is_nullable *SHORT_DESC Value (from CODE_ table),  *PC_Column (from Transformations table)
1   1826105546  CODE_COUNTRY    SHORT_DESC  nvarchar    20  0 United States, USA
1   2018106230  CODE_COUNTY SHORT_DESC  nvarchar    20  0 Mexico, Mexico

例如,SHORT_DESC中的值应与PC_Column

匹配
CODE_VALUE_KEY  CODE_VALUE  SHORT_DESC  MEDIUM_DESC LONG_DESC   STATUS
1001    1001    Autauga Autauga Autauga A
1003    1003    Baldwin Baldwin Baldwin A
1005    1005    Barbour Barbour Barbour A
1007    1007    Bibb    Bibb    Bibb    A
1009    1009    Blount  Blount  Blount  A

Transformations Table
    GM_Column   Value   Note1   F4  PC_Table_Column PC_Table    PC_Column   Value1  Note2   F10
    Ugender M   NULL    =   demographics_gender demographics    gender  Male    NULL    NULL
    Ugender F   NULL    =   demographics_gender demographics    gender  Female  NULL    NULL
    Ugender U   NULL    =   demographics_gender demographics    gender  Unknown NULL    NULL
    Umarstat    D   NULL    =   demographics_marital_status demographics    marital_status  Divorced    NULL    NULL
    Umarstat    M   NULL    =   demographics_marital_status demographics    marital_status  Married NULL    NULL
    Umarstat    O   NULL    =   demographics_marital_status demographics    marital_status  Other   NULL    NULL
    Umarstat    S   NULL    =   demographics_marital_status demographics    marital_status  Single  NULL    NULL

系统表 - 查找系统表以查找包含用户数据的公共列,并且看不到要加入用户数据表的公共列

SELECT * FROM sys.tables WHERE name LIKE '%code%'                                                                                                       

/*                                                                                                      
name    object_id   principal_id    schema_id   parent_object_id    type    type_desc   create_date modify_date is_ms_shipped   is_published    is_schema_published lob_data_space_id   filestream_data_space_id    max_column_id_used  lock_on_bulk_load   uses_ansi_nulls is_replicated   has_replication_filter  is_merge_published  is_sync_tran_subscribed has_unchecked_assembly_data text_in_row_limit   large_value_types_out_of_row    is_tracked_by_cdc   lock_escalation lock_escalation_desc
CODE_SOURCETYPE 5195785 NULL    1   0   U   USER_TABLE  45:44.0 16:28.9 0   0   0   0   NULL    18  0   1   0   0   0   0   0   0   0   0   0   TABLE
CODE_CONTROLTYPE    8671795 NULL    1   0   U   USER_TABLE  45:37.8 16:28.9 0   0   0   0   NULL    17  0   1   0   0   0   0   0   0   0   0   0   TABLE
CODE_SPONSORSTATUS  21195842    NULL    1   0   U   USER_TABLE  45:44.1 16:29.0 0   0   0   0   NULL    19  0   1   0   0   0   0   0   0   0   0   0   TABLE
CODE_COUNTRY    24671852    NULL    1   0   U   USER_TABLE  45:37.8 16:29.0 0   0   0   0   NULL    24  0   1   0   0   0   0   0   0   0   0   0   TABLE
CODE_SPONSORTYPE    37195899    NULL    1   0   U   USER_TABLE  45:44.1 16:29.2 0   0   0   0   NULL    17  0   1   0   0   0   0   0   0   0   0   0   TABLE
CODE_COUNTY 40671909    NULL    1   0   U   USER_TABLE  45:37.9 16:29.2 0   0   0   0   NULL    18  0   1   0   0   0   0   0   0   0   0   0   TABLE
CODE_STATE  53195956    NULL    1   0   U   USER_TABLE  45:44.2 16:29.3 0   0   0   0   NULL    19  0   1   0   0   0   0   0   0   0   0   0   TABLE
CODE_CREDCARDTYPE   56671966    NULL    1   0   U   USER_TABLE  45:38.0 16:29.3 0   0   0   0   NULL    17  0   1   0   0   0   0   0   0   0   0   0   TABLE
*/                                                                                                      

SELECT      * FROM sys.objects WHERE type = 'U' --user tables                                                                                                       
/*                                                                                                      
has table name                                                                                                      
name    object_id   principal_id    schema_id   parent_object_id    type    type_desc   create_date modify_date is_ms_shipped   is_published    is_schema_published                                                         
ADDRESSSCHEDULE 7671075 NULL    1   0   U   USER_TABLE  03:10.6 03:14.2 0   0   0                                                           
ADVANCENAME 23671132    NULL    1   0   U   USER_TABLE  03:10.7 03:10.7 0   0   0                                                           
COMBINEMAILING  55671246    NULL    1   0   U   USER_TABLE  03:11.1 03:11.1 0   0   0                                                           
DEMOGRAPHICS    87671360    NULL    1   0   U   USER_TABLE  03:11.4 03:11.4 0   0   0                                                           
*/                                                                                                      
SELECT      * FROM sys.columns                                                                                                      
/*                                                                                                      
has column name                                                                                                     
object_id   name    column_id   system_type_id  user_type_id    max_length  precision   scale   collation_name  is_nullable is_ansi_padded  is_rowguidcol   is_identity is_computed is_filestream   is_replicated   is_non_sql_subscribed   is_merge_published  is_dts_replicated   is_xml_document xml_collection_id   default_object_id   rule_object_id  is_sparse   is_column_set       
119671474   HONORS  10  231 231 12  0   0   SQL_Latin1_General_CP1_CI_AS    1   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0       
119671474   TRANSCRIPT_DATE 11  61  61  8   23  3   NULL    1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       
119671474   CLASS_RANK  12  56  56  4   10  0   NULL    1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       
119671474   CLASS_SIZE  13  56  56  4   10  0   NULL    1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       
*/                                                                                                      
SELECT      * FROM sys.tables                                                                                                       
/*                                                                                                      
has table name                                                                                                      
name    object_id   principal_id    schema_id   parent_object_id    type    type_desc   create_date modify_date is_ms_shipped   is_published    is_schema_published lob_data_space_id   filestream_data_space_id    max_column_id_used  lock_on_bulk_load   uses_ansi_nulls is_replicated   has_replication_filter  is_merge_published  is_sync_tran_subscribed has_unchecked_assembly_data text_in_row_limit   large_value_types_out_of_row    is_tracked_by_cdc   lock_escalation lock_escalation_desc
ADDRESSSCHEDULE 7671075 NULL    1   0   U   USER_TABLE  03:10.6 03:14.2 0   0   0   0   NULL    39  0   1   0   0   0   0   0   0   0   0   0   TABLE
ADVANCENAME 23671132    NULL    1   0   U   USER_TABLE  03:10.7 03:10.7 0   0   0   0   NULL    18  0   1   0   0   0   0   0   0   0   0   0   TABLE
COMBINEMAILING  55671246    NULL    1   0   U   USER_TABLE  03:11.1 03:11.1 0   0   0   0   NULL    16  0   1   0   0   0   0   0   0   0   0   0   TABLE
DEMOGRAPHICS    87671360    NULL    1   0   U   USER_TABLE  03:11.4 03:11.4 0   0   0   0   NULL    31  0   1   0   0   0   0   0   0   0   0   0   TABLE
*/                                                                                                      
SELECT      * FROM sys.tables WHERE name LIKE '%code%'                                                                                                      
SELECT      * FROM systypes                                                                                                     
/*                                                                                                      
has column type                                                                                                     
name    xtype   status  xusertype   length  xprec   xscale  tdefault    domain  uid reserved    collationid usertype    variable    allownulls  type    printfmt    prec    scale   collation                           
text    35  0   35  16  0   0   0   0   4   0   872468488   19  0   1   35  NULL    NULL    NULL    SQL_Latin1_General_CP1_CI_AS                            
uniqueidentifier    36  0   36  16  0   0   0   0   4   0   NULL    0   0   1   37  NULL    16  NULL    NULL                            
date    40  0   40  3   10  0   0   0   4   0   NULL    0   0   1   0   NULL    10  0   NULL                            
time    41  0   41  5   16  7   0   0   4   0   NULL    0   0   1   0   NULL    16  7   NULL                            
datetime2   42  0   42  8   27  7   0   0   4   0   NULL    0   0   1   0   NULL    27  7   NULL                            
datetimeoffset  43  0   43  10  34  7   0   0   4   0   NULL    0   0   1   0   NULL    34  7   NULL                            
...                                                                                                     
34 rows                                                                                                     
*/                                                                                                      

我正在使用SQL Server 2008 R2

我在工作表上发布了格式 https://docs.google.com/spreadsheets/d/1k3rubaSm0M4jXf5VKgk3DuS8QkuPwgzPZYJnsMJdQcI/edit?usp=sharing

0 个答案:

没有答案