查询字段信息:
-- 查询字段信息:
SELECT * FROM all_tab_columns WHERE OWNER = 'OWNER_NAME' AND TABLE_NAME='TABLE_NAME'
-- 或者
SELECT * FROM user_tab_columns WHERE TABLE_NAME='TABLE_NAME'
-- 查询字段注释:
SELECT * FROM all_col_comments WHERE TABLE_NAME='TABLE_NAME'
-- 或者
SELECT * FROM user_col_comments WHERE TABLE_NAME='TABLE_NAME'
-- 查询表字段信息及注释
SELECT
a.OWNER 模式,
a.TABLE_NAME 表名,
a.COLUMN_NAME 列名,
a.DATA_TYPE 数据类型,
a.DATA_LENGTH 长度,
a.NULLABLE 非空,
--(CASE WHEN a.NULLABLE = 'N' THEN 'Y' ELSE 'N' END) 非空,
b.COMMENTS 注释
FROM all_tab_columns a
LEFT JOIN all_col_comments b ON a.OWNER = b.OWNER AND a.TABLE_NAME = b.TABLE_NAME AND a.COLUMN_NAME = b.COLUMN_NAME
WHERE a.OWNER = 'OWNER_NAME' AND a.TABLE_NAME = 'TABLE_NAME '
ORDER BY a.TABLE_NAME, a.COLUMN_ID
-- 或者
SELECT
a.TABLE_NAME 表名,
a.COLUMN_NAME 列名,
a.DATA_TYPE 数据类型,
a.DATA_LENGTH 长度,
a.NULLABLE 非空,
--(CASE WHEN a.NULLABLE = 'N' THEN 'Y' ELSE 'N' END) 非空,
b.COMMENTS 注释
FROM user_tab_columns a
LEFT JOIN user_col_comments b ON a.TABLE_NAME = b.TABLE_NAME AND a.COLUMN_NAME = b.COLUMN_NAME
WHERE a.TABLE_NAME = 'TABLE_NAME '
ORDER BY a.COLUMN_ID
-- 1.查询数据库中所有表名称:
select table_name from information_schema.tables where TABLE_CATALOG=‘数据库名称’;(包含视图)
select table_name from information_schema.tables where TABLE_CATALOG=‘数据库名称’ and table_type = ‘BASE TABLE’
-- 2.查询每张表中所有字段名:
select COLUMN_NAME from INFORMATION_SCHEMA.Columns where table_name=‘表名称’ and TABLE_CATALOG=‘数据库名称’;
-- information_schema:系统视图
-- sys.extended_properties:系统视图
-- 表或表字段等的注释,是数据库对象的扩展属性。在MSSQL中,支持把一些注释性的内容放到数据库或数据
-- 库对象中,增强可读性,有助于日后的管理和维护工作。扩展属性的内容可以通过SSMS添加、修改或删--
-- 除,也可以通过系统视图查询,通过执行相关的存储过程来维护。
--查询一个表的所有列名
select t.column_name from information_schema.columns t where t.table_name='sys_menu';
-- 查询一个表的所有列名,数据类型
select column_name,data_type from information_schema.columns
where table_name = ''
-- 查询一个表的所有字段的注释
select value from sys.extended_properties where major_id = object_id ('');
-- 查询一个表的所有信息
select * FROM information_schema.columns
where table_name = ''
-- 查询一个表的所有列名,字段的注释
SELECT
A.name AS table_name,
B.name AS column_name,
C.value AS column_description
FROM sys.tables A
INNER JOIN sys.columns B ON B.object_id = A.object_id
LEFT JOIN sys.extended_properties C ON C.major_id = B.object_id AND C.minor_id = B.column_id
WHERE A.name = ''
-- 查询某个表的列名称、说明、备注、类型等
SELECT
表名 = case when a.colorder=1 then d.name else '' end,
表说明 = case when a.colorder=1 then isnull(f.value,'') else '' end,
字段序号 = a.colorder,
字段名 = a.name,
标识 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主键 = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end,
类型 = b.name,
占用字节数 = a.length,
长度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空 = case when a.isnullable=1 then '√'else '' end,
默认值 = isnull(e.text,''),
字段说明 = isnull(g.[value],'')
FROM
syscolumns a
left join
systypes b
on
a.xusertype=b.xusertype
inner join
sysobjects d
on
a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join
1.将数据库里面的字段格式改为nvarchar,可以立即解决问题,但是可能会导致跑在上面的其他系统出问题,所以还有下面第二种方法
2.在编写sql语句的时候,直接重新编码,用CONVERT(nvarchar(100),invdetail.transname) transname,例如:
select city_name from city_table,修改为下面的代码
select CONVERT(nvarchar(100), city_name) city_name from city_table
此方法直接修改字段的格式为nvarchar,可以直接解决问题
3.在Python程序里面将读出来的每个字段重新编码
city = city.encode('latin-1').decode('gbk')