操作列 删列: alter table tablename drop column columnname; 加列: alter table Medical_institution add Hospital_Level int not null default(1) 改列长度/类型 alter table tableName alter column columnName varchar(4000) --检查项目简要意义300 alter table CheckItem alter column Jyyy varchar(300) 改列名 EXEC sp_rename 'tableName.column1' , 'column2' (把表名为tableName的column1列名修改为column2) Eg: [Date_jz]改为 Date_yy。 EXEC sp_rename 'Two_way_referral.[Date_jz]' , 'Date_yy' , 'COLUMN' 改表名: exec sp_rename 'old','new' 加default约束 alter table customer add constraint customer_CODE_DEFAULT default( dbo.c_NextBH()) for Cus_code SQL语句删除和添加外键、主键的方法 --删除外键语法:alter table 表名drop constraint 外键约束名如: alter table Stu_PkFk_Sc drop constraint FK_s alter table Stu_PkFk_SC drop constraint FK_c --添加外键语法:alter table 表名add constraint 外键约束名foreign key(列名) references 引用外键表(列名) 如: alter table Stu_PkFk_Sc add constraint Fk_s foreign key (sno) references Stu_PkFk_S(sno) go --删除主键语法:alter table 表名drop constraint 主键约束名如: alter table Stu_PkFk_S drop constraint PK_S go --增加主键语法:alter 表名add constraint 主键约束名primary key(列名) alter table Stu_PkFk_S add constraint PK_S primary key (sno) Go
使用事物 (示例:删除表,修改表名) begin try begin transaction drop table chsil_jktj exec sp_rename 'chsil_jktj_test','chsil_jktj' commit transaction end try begin catch select ERROR_NUMBER() as errornumber , '操作失误,事物回滚' rollback transaction end catch create proc pro_getPuerperantM_Visit_Record_04--19+2 @grdabh varchar(20),--个人档案编号 @yunzhou varchar(20),--孕周 as begin declare @count int select @count=COUNT(*) from chsycf_2csf where sickcode=@grdabh and sfrq=@sfrq if @count=0 begin select '2'code,'查无数据'message return end begin transaction update chsycf_2csf set yunzhou=case @yunzhou when null then '' else @yunzhou end where sickcode=@grdabh if @@ERROR<>0 goto final commit transaction select '1' code,'保存成功' message return final: select '0' code,'保存失败,请重试' message rollback transaction end