最近做项目用到一些数据操作,为了防止自己忘记,同时也供大家参考,特地将项目过程中写的sql语句发出来。
1.清空数据库中数据且不破坏表的结构(因为有表约束所以不能用:TRUNCATE TABLE 表名)
版本一:declare cursor_alltablename cursor scroll dynamic for select name from sysobjects where xtype = 'U' and (not name like 'sys%') order by name desc;
declare @tablename varchar(50);
declare @strSql varchar(100);
open cursor_alltablename;
fetch next from cursor_alltablename into @tablename;
while (@@FETCH_STATUS = 0)
begin
set @strSql='alter table ['+@tablename + '] nocheck constraint all'; --这里注意nocheck 和‘]’之间是有空格的,还有最好给@tablename加上'[]'
exec (@strSql)
fetch next from cursor_alltablename into @tablename
end
fetch first from cursor_alltablename into @tablename --移到数据集的首部
while (@@FETCH_STATUS = 0)
begin
set @strSql='delete ['+@tablename+']'
print @strSql
exec (@strSql)
fetch next from cursor_alltablename into @tablename
end
fetch first from cursor_alltablename into @tablename
while (@@FETCH_STATUS = 0)
begin
set @strSql='alter table ['+@tablename +'] check constraint all'
exec (@strSql)
fetch next from cursor_alltablename into @tablename
end
close cursor_alltablename
deallocate cursor_alltablename
改进版:
declare cursor_alltablename cursor scroll dynamic for select name from sysobjects where xtype = 'U' and (not name like 'sys%') order by name desc;
declare @tablename nvarchar(50);
declare @strSql nvarchar(500);
declare @i int=1
open cursor_alltablename;
fetch next from cursor_alltablename into @tablename;
while (@@FETCH_STATUS = 0)
begin
set @strSql=N'alter table ['+@tablename + N'] nocheck constraint all';
exec (@strSql)
set @i=@i+1
fetch next from cursor_alltablename into @tablename
end
fetch last from cursor_alltablename into @tablename
while (@i > 0)
begin
set @strSql=N'delete ['+@tablename+N']'
print @strSql
exec (@strSql)
set @i = @i - 1
fetch prior from cursor_alltablename into @tablename
end
declare @count int;
fetch first from cursor_alltablename into @tablename
while (@@FETCH_STATUS = 0)
begin
set @strSql=N'alter table ['+@tablename +N'] check constraint all'
exec (@strSql)
--set @strSql = N'select @countOut=count(*) from sysobjects where parent_obj= OBJECT_ID(N'''+@tablename+N''') and xtype = N''PK'''; --这个是查询所有的主键列
set @strSql = N'SELECT @countOut=count(*) FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='''+@tablename+ --这个是查询自增列
''' AND (COLUMNPROPERTY(OBJECT_ID('''+@tablename+'''),COLUMN_NAME,''IsIdentity'')=1)'
exec sp_executeSql @strSql,N'@countOut int Output',@countOut = @count Output;
if(@count > 0) --将有主键自增的表的自增列置为0
begin
set @strSql = N'dbcc checkident(N'''+@tablename+N''',reseed,0)'
exec (@strSql)
end
fetch next from cursor_alltablename into @tablename
end
close cursor_alltablename
deallocate cursor_alltablename
2.查询数据库中所有存在自增列的表
方法一:declare @Table_name varchar(60)
set @Table_name = '';
if Exists(Select top 1 1 from sysobjects
Where objectproperty(id, 'TableHasIdentity') = 1
and upper(name) = upper(@Table_name)
)
select 1
else select 0
-- or
if Exists(Select top 1 1 from sysobjects so
Where so.xtype = 'U'
and upper(so.name) = upper(@Table_name)
and Exists(Select Top 1 1 from syscolumns sc
Where sc.id = so.id
and columnproperty(sc.id, sc.name, 'IsIdentity') = 1
)
)
select 1
else select 0
方法二:
SELECT COUNT(*) FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='Orders' AND (COLUMNPROPERTY(OBJECT_ID('Orders'),COLUMN_NAME,'IsIdentity')=1)
3.将数据库中表中自增列重新排列
exec sp_configure 'allow updates',1
reconfigure with override
GO
update syscolumns set colstat = 0 where id = object_id('dbo.Customer') and colstat = 1
GO
update dbo.Customer
set ID=(select count(1) from dbo.Customer where ID<=t.ID)
from dbo.Customer t
GO
ueclare @a int
set @a=(select count(*) from dbo.Customer)
DBCC CHECKIDENT (Customer, RESEED, @a)
GO
update syscolumns set colstat = 1 where id = object_id('dbo.Customer') and name = 'ID'
GO
exec sp_configure 'allow updates',0
reconfigure with override