Sql 增加字段(有些只能在ORACLE中运行)
增加字段alter table table_name add column_name varchar(200)删除字段ALTER TABLE table_NAME DROP COLUMN column_NAME修改字段类型ALTER TABLE table_name ALTER COLUMN column_name new_data_type修改表字段的长度(修改表结构) alter table table_name modify column_name varchar(40) ;改名sp_rename更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称。语法sp_rename [ @objname = ] 'object_name' , [ @newname = ] 'new_name' [ , [ @objtype = ] 'object_type' ]--假设要处理的表名为: tb--判断要添加列的表中是否有主键if exists(select 1 from sysobjects where parent_obj=object_id('tb') and xtype='PK')beginprint '表中已经有主键,列只能做为普通列添加'--添加int类型的列,默认值为0alter table tb add 列名 int default 0endelsebeginprint '表中无主键,添加主键列' 查询表中的某些字段重复记录 select count(*),column_name from table_name group by column_name having count(*)>1;查询并修改数据(如将67001改为68701,67101改为68801) select code,('68' || (substr(code, 2, 1) + substr(code,3,1)) || substr(code,4,2)) as code2 from table_name where code like '67___' ; update table_name set code = ('68' || (substr(code, 2, 1) + substr(code,3,1)) || substr(code,4,2)), parentid='68' where code like '67___' ;