生成drop table语句列表的动态sql脚本

Dynamic sql script to generate list of drop table statement(生成drop table语句列表的动态sql脚本)
本文介绍了生成drop table语句列表的动态sql脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我列出了需要从 SQL Server 中删除的表(大约 100++).以下是我将使用的示例代码

I've list of tables (around 100++) that need to be dropped from SQL Server. Below is the sample code that I would use

IF OBJECT_ID('dbo.DS_Area_TBL', 'U') IS NOT NULL
drop table dbo.DS_Area_TBL
Print 'dbo.DS_Area_TBL has been dropped'

我需要用其他表名替换表名 100++ 时间.如何编写一个可以自动生成查询列表的动态sql脚本?

I need to replace table name 100++ time with other table name. How to write a dynamic sql script that can auto generate list of queries?

推荐答案

你可以先生成脚本然后用动态 sql 执行:

You could first generate script then execute with dynamic sql:

CREATE TABLE a(a INT);
CREATE TABLE b(a INT);
CREATE TABLE c(a INT);
CREATE TABLE d(a INT);
CREATE TABLE e(a INT);

CREATE TABLE tab(tab_name SYSNAME);   -- here are table names stored
INSERT INTO tab VALUES ('a'),('b'),('c'),('d'),('e');


-- main part
DECLARE @sql NVARCHAR(MAX);

SELECT @sql = STUFF((SELECT ' ' +  FORMATMESSAGE(
'IF OBJECT_ID(''%s'', ''U'') IS NOT NULL 
BEGIN
   DROP TABLE %s;
   PRINT ''%s has been dropped '';
END
', QUOTENAME(tab_name),QUOTENAME(tab_name),QUOTENAME(tab_name))
                   FROM tab
                   FOR XML PATH('')), 1, 1, '');

PRINT @sql;   -- for debug

EXEC [dbo].[sp_executesql]
    @sql;

如果你使用的SQL Server版本低于2012你需要用字符串连接+来改变FORMATMESSAGE.

If you use version of SQL Server lower than 2012 you need to change FORMATMESSAGE with string concatenation +.

您可以通过修改模板轻松地使用自定义架构等扩展此脚本:

You could easily extend this script with custom schema and so on by modifying template:

'IF OBJECT_ID(''%s'', ''U'') IS NOT NULL 
    BEGIN
       DROP TABLE %s;
       PRINT ''%s has been dropped '';
    END
'

输出:

IF OBJECT_ID('[a]', 'U') IS NOT NULL 
BEGIN
   DROP TABLE [a];
   PRINT '[a] has been dropped ';
END
 IF OBJECT_ID('[b]', 'U') IS NOT NULL 
BEGIN
   DROP TABLE [b];
   PRINT '[b] has been dropped ';
END
 IF OBJECT_ID('[c]', 'U') IS NOT NULL 
BEGIN
   DROP TABLE [c];
   PRINT '[c] has been dropped ';
END
 IF OBJECT_ID('[d]', 'U') IS NOT NULL 
BEGIN
   DROP TABLE [d];
   PRINT '[d] has been dropped ';
END
 IF OBJECT_ID('[e]', 'U') IS NOT NULL 
BEGIN
   DROP TABLE [e];
   PRINT '[e] has been dropped ';
END


工作原理:

  1. XML + STUFFfor string concatenation 是 SQL Server 的常用习惯用法,类似于 MySQL 中的 GROUP_CONCAT.您可以将其视为一种将多个 IF BEGIN END 块组合成一个字符串的方法.
  2. FORMATMESSAGE 将替换 %s 带有实际表名(引用以避免 SQL 注入攻击)
  3. PRINT 用于调试以检查生成的查询,可以注释
  4. sp_executesql 将执行 SQL 字符串
  1. XML + STUFF for string concatenation is common idiom with SQL Server, works like GROUP_CONCAT in MySQL. You can think about it as a way to combine multiple IF BEGIN END chunks into one string.
  2. FORMATMESSAGE will replace %s with actual table names(quoted to avoid SQL Injection attacks)
  3. PRINT is for debug to check generated query, can be commented
  4. sp_executesql will execute SQL string

这篇关于生成drop table语句列表的动态sql脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
SQL query to group by day(按天分组的 SQL 查询)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)