SqlServer清空一个数据库中所有表数据保留表结构
Sqlserver清空数据库所有表数据保留表结构
·
SqlServer将多行查询结果通过逗号拼接成一个字符串
select stuff((select ','+title from tb for xml path('')),1,1,'')
--其中tb是表名,title是要合并的表中的字段名称
select table_name+';' AS table_name FROM information_schema.tables where TABLE_CATALOG='test';--这个是查出数据库所有的表名
select 'TRUNCATE TABLE '+a.table_name from (select table_name+';' AS table_name FROM information_schema.tables where TABLE_CATALOG='test')a --将test(数据库名)换成想要删除的数据库就OK
for xml path('')--这个是将数据记录拼接成字符串
SqlServer清空一个数据库中所有表数据保留表结构
DECLARE @table VARCHAR(Max)
SET @table =STUFF((select 'TRUNCATE TABLE '+a.table_name from (select table_name+';' AS table_name FROM information_schema.tables where TABLE_CATALOG='test')a --将test(数据库名)换成想要删除的数据库就OK
for xml path('')),1,0,'')
EXEC(@table);

更多推荐



所有评论(0)