CREATE PROCEDURE sp_DeleteAllData AS
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'ALTER TABLE ? DISABLE TRIGGER ALL'
EXEC sp_MSForEachTable 'DELETE FROM ?'
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'ALTER TABLE ? DISABLE TRIGGER ALL'
EXEC sp_MSForEachTable 'DELETE FROM ?'
EXEC sp_MSForEachTable 'TRUNCATE TABLE ?'
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'ALTER TABLE ? ENABLE TRIGGER ALL'
GO
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'ALTER TABLE ? ENABLE TRIGGER ALL'
GO
--先禁用所有外键约束
exec sp_msforeachtable "alter table ? nocheck CONSTRAINT all "
--禁止所有触发器
exec sp_msforeachtable "alter table ? disable trigger all "
--然后删除数据
exec sp_msforeachtable @command1= 'truncate table ? '
exec sp_msforeachtable "alter table ? nocheck CONSTRAINT all "
--禁止所有触发器
exec sp_msforeachtable "alter table ? disable trigger all "
--然后删除数据
exec sp_msforeachtable @command1= 'truncate table ? '
,@whereand= ' and objectproperty(o.id, ' 'TableHasForeignRef ' ')=0 '
--此处o.id是什么意思?
exec sp_msforeachtable @command1= 'delete from ? '
,@whereand= ' and objectproperty(o.id, ' 'tablehasforeignref ' ')=1 '
--再启用所有外键约束
exec sp_msforeachtable "alter table ? check constraint all "
--启用所有触发器
exec sp_msforeachtable "alter table ? enable trigger all "
另外,我只想删除自己的表的数据,那些sys开头的数据不想清空,上面这个存储过程应该不行吧?
--此处o.id是什么意思?
exec sp_msforeachtable @command1= 'delete from ? '
,@whereand= ' and objectproperty(o.id, ' 'tablehasforeignref ' ')=1 '
--再启用所有外键约束
exec sp_msforeachtable "alter table ? check constraint all "
--启用所有触发器
exec sp_msforeachtable "alter table ? enable trigger all "
另外,我只想删除自己的表的数据,那些sys开头的数据不想清空,上面这个存储过程应该不行吧?
--然后删除数据
exec sp_msforeachtable @command1= 'truncate table ? '
,@whereand= ' and objectproperty(o.id, ' 'TableHasForeignRef
exec sp_msforeachtable @command1= 'truncate table ? '
,@whereand= ' and objectproperty(o.id, ' 'TableHasForeignRef
' ')=0 '
--此处o.id是什么意思?
exec sp_msforeachtable @command1= 'delete from ? '
,@whereand= ' and objectproperty(o.id, ' 'tablehasforeignref ' ')=1 '
能不能解释一下这里是什么意思?
--此处o.id是什么意思?
exec sp_msforeachtable @command1= 'delete from ? '
,@whereand= ' and objectproperty(o.id, ' 'tablehasforeignref ' ')=1 '
能不能解释一下这里是什么意思?
create proc usp_delete as
delete a
delete b
go
delete a
delete b
go
exec usp_delete
如何清空SQL数据库里的数据 |
1、打开“服务管理器”,选中你要处理的数据库,然后打开“工具”里的“SQL查分析器”,运行下面语句: --先禁用所有外键约束 exec sp_msforeachtable "alter table ? nocheck CONSTRAINT all" --然后删除数据 exec sp_msforEachTable "TRUNCATE TABLE?" --再启用所有外键约束 exec sp_msforeachtable "alter table ? check constraint all" 2、成功。然后导入你的数据或重新填写数据。 使用存储过程来删除:
执行上面的存储过程的话,你的数据库将空空如也~~~~慎重使用啊! 也可以指定要删除清空哪个表的数据.....EXEC sp_MSForEachTable 'DELETE FROM ?' 里面的问号换成表名就好了! | ||
public ResultSet conten(int id)throws SQLException
{
DbConn conn = new DbConn();
String sql="truncate table temporary";
conn.save(sql);
System.out.print("清空temporary表中的记录"+sql);
String sql1="select distinct content from temporary where id='"+id+"'";
rs=conn.query(sql1);
System.out.println("向temporary表中添加新数据成功!");
return rs;
}
你的sql1语句是查询的语句,如果执行了sql的截断语句清空了表,则查询的结果为空了,也就没有返回值。
插入数据应该用:
insert into temporary (字段1,字段2) value (值1,值2);
SQL语句清空数据库所有表的数据
2009年12月07日 星期一 09:32 A.M.
利用SQL语句一次清空所有数据.到了三种方法进行清空.使用的数据库为MS SQL SERVER.
1.搜索出所有表名,构造为一条SQL语句
1.搜索出所有表名,构造为一条SQL语句
declare @trun_name varchar(8000)
set @trun_name=''
select @trun_name=@trun_name + 'truncate table ' + [name] + ' ' from sysobjects where xtype='U' and status > 0
exec (@trun_name)
set @trun_name=''
select @trun_name=@trun_name + 'truncate table ' + [name] + ' ' from sysobjects where xtype='U' and status > 0
exec (@trun_name)
该方法适合表不是非常多的情况,否则表数量过多,超过字符串的长度,不能进行完全清理.
2.利用游标清理所有表
declare @trun_name varchar(50)
declare name_cursor cursor for
select 'truncate table ' + name from sysobjects where xtype='U' and status > 0
open name_cursor
fetch next from name_cursor into @trun_name
while @@FETCH_STATUS = 0
begin
exec (@trun_name)
print 'truncated table ' + @trun_name
fetch next from name_cursor into @trun_name
end
close name_cursor
deallocate name_cursor
declare name_cursor cursor for
select 'truncate table ' + name from sysobjects where xtype='U' and status > 0
open name_cursor
fetch next from name_cursor into @trun_name
while @@FETCH_STATUS = 0
begin
exec (@trun_name)
print 'truncated table ' + @trun_name
fetch next from name_cursor into @trun_name
end
close name_cursor
deallocate name_cursor
这是我自己构造的,可以做为存储过程调用, 能够一次清空所有表的数据,并且还可以进行有选
择的清空表.
3.利用微软未公开的存储过程
exec sp_msforeachtable "truncate table ?"
该方法可以一次清空所有表,但不能加过滤条件.
CREATE OR REPLACE PROCEDURE XXX
(
iID IN MY_TABLE.ID%TYPE
)
IS
BEGIN
END XXX;
说明:
iID IN MY_TABLE.ID%TYPEtruncate的数据如何恢复
iID参数的数据类型跟MY_TABLE表的ID字段类型一样,这样,你修改这个表的ID字段类型,就不用改存储过程了。
declare crsr cursor
for SELECT [name] FROM DBO.SYSOBJECTS
WHERE OBJECTPROPERTY(ID,N'IsTable')=1 and type = 'U' and [name] <> 'dtproperties' --
for SELECT [name] FROM DBO.SYSOBJECTS
WHERE OBJECTPROPERTY(ID,N'IsTable')=1 and type = 'U' and [name] <> 'dtproperties' --
open crsr
declare @tblName sysname
fetch crsr into @tblName
EXEC('truncate table '+@tblName)
declare @tblName sysname
fetch crsr into @tblName
EXEC('truncate table '+@tblName)
while @@fetch_status=0
fetch next from crsr into @tblName
EXEC('truncate table '+@tblName)
close crsr
deallocate crsr --删除以释放游标
可以为crdate字段指定表的创建日期
EXEC('truncate table '+@tblName)
close crsr
deallocate crsr --删除以释放游标
可以为crdate字段指定表的创建日期
declare @trun_name varchar(8000)
set @trun_name=''
select @trun_name=@trun_name + 'truncate table ' + [name] + ' ' from sysobjects where xtype='U' and status > 0
exec (@trun_name)
set @trun_name=''
select @trun_name=@trun_name + 'truncate table ' + [name] + ' ' from sysobjects where xtype='U' and status > 0
exec (@trun_name)
T-SQL语句实现清空数据库数据的代码,保留原有表形式和各种约束
Posted on 2009-11-23 16:39 许清明 阅读(534) 评论(0) 编辑 收藏
/**********************************************************************/
----------------------清空数据库数据的SQL代码--------------
/**********************************************************************/
------------------------------------------------------
use TSZongHe_new --选择你要清空的数据库
-------------------------------------------------------
-------------禁用本库中所有表的外键约束-----
-------------------------------------------------------
DECLARE Employee_Cursor CURSOR FOR
select name from sysobjects where xtype='u' and (not name LIKE 'dtproperties') order by [name] asc;
declare @tablename varchar(300);
declare @str varchar(1000);
declare @rst int;
-
----------------------清空数据库数据的SQL代码--------------
/**********************************************************************/
------------------------------------------------------
use TSZongHe_new --选择你要清空的数据库
-------------------------------------------------------
-------------禁用本库中所有表的外键约束-----
-------------------------------------------------------
DECLARE Employee_Cursor CURSOR FOR
select name from sysobjects where xtype='u' and (not name LIKE 'dtproperties') order by [name] asc;
declare @tablename varchar(300);
declare @str varchar(1000);
declare @rst int;
-
-SET @MyVariable = MyCursor
OPEN Employee_Cursor;
FETCH NEXT FROM Employee_Cursor into @tablename;
WHILE @@FETCH_STATUS = 0
BEGIN
select @str = 'ALTER TABLE '+ @tablename + ' NOCHECK CONSTRAINT ALL';
EXECUTE(@str);
FETCH NEXT FROM Employee_Cursor into @tablename;
END
CLOSE Employee_Cursor;
DEALLOCATE Employee_Cursor;
GO
----------------------------------------------------
--------------删除本库中所有表数据----------
-
OPEN Employee_Cursor;
FETCH NEXT FROM Employee_Cursor into @tablename;
WHILE @@FETCH_STATUS = 0
BEGIN
select @str = 'ALTER TABLE '+ @tablename + ' NOCHECK CONSTRAINT ALL';
EXECUTE(@str);
FETCH NEXT FROM Employee_Cursor into @tablename;
END
CLOSE Employee_Cursor;
DEALLOCATE Employee_Cursor;
GO
----------------------------------------------------
--------------删除本库中所有表数据----------
-
---------------------------------------------------
DECLARE Employee_Cursor CURSOR FOR
select name from sysobjects where xtype='u' and (not name LIKE 'dtproperties')
-------------------------------------------------------------------------------------------
--如果你想清空库中所有表的话,就不需要下段这部分代码
-------------------------------------------------------------------------------------------
and (name not in ('PageRights','ModuleInfo','RoleModuleRight','RoleInfo','UserInfo',
'PersonnelInfo','SystemDirectory','MarriageState','TechnicalPostInfo','EducationInfo',
'PlaceInfo ','DepartmentInfo ','FoodStandard','FieldsValue','PublicResShare','StyleInfo',
'DocumentTypeInfo','AddressKindInfo','PayTypeInfo','FoodStandard','CityInfo','HotelType',
'FootQuomodoInfo','HotelGradeInfo','PlaceArea','TravelAgencySortInfo','BusTypeInfo'))
--------------------------------------------------------------------------------------------
--如果你想保留某些表数据的话,可以加上下面这段代码,当然表名根据自己的情况写
--------------------------------------------------------------------------------------------
order by [name] asc;
DECLARE Employee_Cursor CURSOR FOR
select name from sysobjects where xtype='u' and (not name LIKE 'dtproperties')
-------------------------------------------------------------------------------------------
--如果你想清空库中所有表的话,就不需要下段这部分代码
-------------------------------------------------------------------------------------------
and (name not in ('PageRights','ModuleInfo','RoleModuleRight','RoleInfo','UserInfo',
'PersonnelInfo','SystemDirectory','MarriageState','TechnicalPostInfo','EducationInfo',
'PlaceInfo ','DepartmentInfo ','FoodStandard','FieldsValue','PublicResShare','StyleInfo',
'DocumentTypeInfo','AddressKindInfo','PayTypeInfo','FoodStandard','CityInfo','HotelType',
'FootQuomodoInfo','HotelGradeInfo','PlaceArea','TravelAgencySortInfo','BusTypeInfo'))
--------------------------------------------------------------------------------------------
--如果你想保留某些表数据的话,可以加上下面这段代码,当然表名根据自己的情况写
--------------------------------------------------------------------------------------------
order by [name] asc;
declare @tablename varchar(300);
declare @str varchar(1000);
declare @rst int;
--SET @MyVariable = MyCursor
OPEN Employee_Cursor;
FETCH NEXT FROM Employee_Cursor into @tablename;
WHILE @@FETCH_STATUS = 0
BEGIN
select @str = 'Delete '+ @tablename ;
EXECUTE(@str);
FETCH NEXT FROM Employee_Cursor into @tablename;
END
CLOSE Employee_Cursor;
DEALLOCATE Employee_Cursor;
GO
declare @str varchar(1000);
declare @rst int;
--SET @MyVariable = MyCursor
OPEN Employee_Cursor;
FETCH NEXT FROM Employee_Cursor into @tablename;
WHILE @@FETCH_STATUS = 0
BEGIN
select @str = 'Delete '+ @tablename ;
EXECUTE(@str);
FETCH NEXT FROM Employee_Cursor into @tablename;
END
CLOSE Employee_Cursor;
DEALLOCATE Employee_Cursor;
GO
----------------------------------------------------
-------------恢复本库中所有表的外键约束--
----------------------------------------------------
DECLARE Employee_Cursor CURSOR FOR
select name from sysobjects where xtype='u' and (not name LIKE 'dtproperties') order by [name] asc;
declare @tablename varchar(300);
declare @str varchar(1000);
declare @rst int;
--SET @MyVariable = MyCursor
OPEN Employee_Cursor;
FETCH NEXT FROM Employee_Cursor into @tablename;
WHILE @@FETCH_STATUS = 0
BEGIN
select @str = 'ALTER TABLE '+ @tablename + ' CHECK CONSTRAINT ALL';
EXECUTE(@str);
FETCH NEXT FROM Employee_Cursor into @tablename;
END
CLOSE Employee_Cursor;
DEALLOCATE Employee_Cursor;
GO
/****************************************************/
EXECUTE(@str);
FETCH NEXT FROM Employee_Cursor into @tablename;
END
CLOSE Employee_Cursor;
DEALLOCATE Employee_Cursor;
GO
/****************************************************/
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论