在sql server 2005中截断多个表 [英] Truncate multiple table in sql server 2005

查看:88
本文介绍了在sql server 2005中截断多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sql server 2005,在我的数据库中我有超过100个表。现在我想使用单个sql查询截断一些选定的表,如50表。我知道这是可能的,我不知道如何编写查询。请任何人都可以帮助我..?

i am using sql server 2005 and in my database i have more than 100 tables. now i want to truncate some selected table like 50 table using single sql query. i know it is possible and i do not know how to write query. please any one can help me..?

推荐答案

你可以通过多种方式实现这一目标

1.
You can do it in many ways
1.
EXEC sp_MSForEachTable 'TRUNCATE TABLE ?'



但是你不能截断具有外键的表,所以这只有在表之间没有外键约束时才有效。无论如何你都可以禁用约束。



2.如果你知道要截断的表的特定前缀,请使用动态查询,例如emp_


but you cannot truncate tables which have foreign keys, so this will only work if there are no foreign key constraints between tables. You can disable constraint anyways.

2. use dynamic query if you know specific prefix of tables to truncate e.g emp_

SELECT 'TRUNCATE TABLE ' + TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'emp%'



此查询将返回表的Truncate语句,然后您可以立即运行这些查询。 (如果有外键,它也不会被截断)

3.您可以使用带有动态查询的光标来选择和截断具有上述动态查询的表格。



谢谢


this query will return Truncate statements of tables and then you can run these query at once. (again it will not truncate if having foreign key)
3. You can use cursor with dynamic query to select and truncate tables with above dynamic query.

Thanks


这篇关于在sql server 2005中截断多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆