久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

生成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腳本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 不卡的av电影| 国产免费一区二区三区最新6 | 亚洲精品一区二区三区蜜桃久 | 韩日av在线 | 成人国产精品一级毛片视频毛片 | 天堂精品视频 | 久久久久国产 | 在线观看免费国产 | 中文精品久久 | 国产综合在线视频 | 国产一区二区中文字幕 | 成人三级av | 国产精品美女久久久久久免费 | 另类在线 | 国产精品免费观看 | 色噜噜狠狠色综合中国 | 91操操操| 欧美午夜激情在线 | 亚洲一区精品在线 | 国产欧美精品在线 | 亚洲精品美女在线观看 | 日韩另类 | 国产综合久久久久久鬼色 | av看片| 超级黄色一级片 | 精品九九 | 一区二区三区在线免费观看 | 亚洲一区二区三区免费在线观看 | 日本一区二区三区四区 | 一区二区成人在线 | 午夜爱爱网 | 在线亚洲电影 | 超碰最新在线 | 一区久久 | 日本免费一区二区三区 | 国产不卡一 | 久久av一区二区三区 | 日韩欧美三级 | 日韩一区二区久久 | 99视频久| 欧美一级毛片免费观看 |