如何选择不可选的列

时间:2017-04-06 17:52:01

标签: sql-server tsql sql-server-2012

我在sql server中有一个我需要选择的列,我无法选择但似乎存在。它是只读列还是其他一些问题?希望我在这里添加了足够的信息,并将列和表的名称更改为通用名称。

SELECT t.name as TName, c.name AS ColName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE upper(c.name) like '%MY_COLUMN_NAME%'

TName      ColName    
------     -----------
My_Table   My_Column   

但是当我尝试选择它时,它说它不存在。有解决方法吗?

select My_Column from My_Table

Msg 207, Level 16, State 1, Line 9
Invalid column name 'My_Column'.

当我尝试所有信息时,我得到了这个:

SELECT t.name as TName, c.name AS ColName, t.*, c.*
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE upper(c.name) like '%My_Column%'

TName                                                                                                                            ColName                                                                                                                          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                                         is_filetable 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
-------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ----------- ------------ ----------- ---------------- ---- ------------------------------------------------------------ ----------------------- ----------------------- ------------- ------------ ------------------- ----------------- ------------------------ ------------------ ----------------- --------------- ------------- ---------------------- ------------------ ----------------------- --------------------------- ----------------- ---------------------------- ----------------- --------------- ------------------------------------------------------------ ------------ ----------- -------------------------------------------------------------------------------------------------------------------------------- ----------- -------------- ------------ ---------- --------- ----- -------------------------------------------------------------------------------------------------------------------------------- ----------- -------------- ------------- ----------- ----------- ------------- ------------- --------------------- ------------------ ----------------- --------------- ----------------- ----------------- -------------- --------- -------------
My_Table                                                                                                                         My_Column                                                                                                              My_Table                                                                                                                         1223935682  NULL         20          0                U    USER_TABLE                                                   2014-02-19 10:35:44.537 2017-02-11 12:48:13.037 0             0            0                   1                 NULL                     39                 0                 1               0             0                      0                  0                       0                           0                 0                            0                 0               TABLE                                                        0            1223935682  My_Column                                                                                                              7           104            376          1          1         0     NULL                                                                                                                             1           0              0             0           0           0             0             0                     0                  0                 0               0                 0                 0              0         0

(1行受影响)

2 个答案:

答案 0 :(得分:2)

您是否在具有不同列的不同架构中具有相同名称的表?

您的结果看起来像是bit列,而schema_id20,所以我怀疑它是默认架构(dbo)。但我可能正在读错,因为值和标题看起来像是失去了对齐。

select 
    TName = quotename(schema_name(t.schema_id))+'.'+quotename(t.name)
  , ColName = c.name
from sys.columns c
  inner join sys.tables t 
    on c.object_id = t.object_id
where c.name like '%My_Column%'

答案 1 :(得分:1)

我可能会采用不同的架构 尝试使用以下查询,因为这将为您提供模式名称。

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MY_COLUMN';

这个假设是由于您上次查询中的schema_id为0而实现的。