if exists(select name from sysobjects where name='tablename' and type='u')
3 判断指定列在指定表中是否存在
1 2 3
if exists(select * from sys.columns,sys.tables where sys.columns.object_id = sys.tables.object_id and sys.tables.name='tablename' and sys.columns.[name]='columnname')
--取数据库中表的集合 select * from sysobjects where xtype='u' order by name
--取表中字段的一些基本信息 select sys.columns.name, --字段名 sys.types.name as typename, --字段类型 sys.columns.max_length, --字段长度 sys.columns.is_nullable, --是否可空 (select count(*) from sys.identity_columns where sys.identity_columns.object_id = sys.columns.object_id and sys.columns.column_id = sys.identity_columns.column_id ) as is_identity ,--是否自增
(select value from sys.extended_properties where sys.extended_properties.major_id = sys.columns.object_id and sys.extended_properties.minor_id = sys.columns.column_id ) as description --注释 from sys.columns, sys.tables, sys.types where sys.columns.object_id = sys.tables.object_id and sys.columns.system_type_id=sys.types.system_type_id and sys.tables.name='tablename' order by sys.columns.column_id
5 在存储过程中使用事务
1 2 3 4 5 6 7 8 9 10 11 12 13
create procedure procname as begin tran --执行sql语句
if @@ERROR!=0 begin rollback tran --失败 end else begin commit tran --成功 end
6 清除数据库日志
1 2 3 4 5 6
DUMP TRANSACTION DatabseName WITH NO_LOG BACKUP LOG DatabseName WITH NO_LOG DBCC SHRINKFILE(DatabseLogName,1) --DatabseName为数据库名称 --DatabseLogName为日志文件名,可以通过下面语句得到 --select name from sysfiles