博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TSQL 根据表名生成UPDATE SELECT INSERT
阅读量:6722 次
发布时间:2019-06-25

本文共 3174 字,大约阅读时间需要 10 分钟。

USE [AdventureWorks2012]goIF object_id('USP_GENERATEDML') IS NOT NULLBEGIN     PRINT 'Dropping procedure USP_GENERATEDML'    DROP PROCEDURE [USP_GENERATEDML]      IF @@ERROR = 0 PRINT 'Procedure USP_GENERATEDML dropped'ENDgoCREATE PROCEDURE [USP_GENERATEDML] @TBLNAME NVARCHAR(100)ASBEGIN    SET NOCOUNT ON ;DECLARE @result as TABLE ([PREFIX] [varchar](1500)  ,[NAME] [nvarchar](2630)  ,[ENDFIX] [nvarchar](2800)  ,[STARTFLAG] [bigint]  ,[TABLE_SCHEMA]  [nvarchar](2800)  ,[TABLE_NAME]  [nvarchar](2800)  ,[FLAG] [varchar](600)  )  ; WITH cte AS (        SELECT ROW_NUMBER() OVER(                PARTITION BY t.TABLE_SCHEMA,                t.TABLE_NAME ORDER BY c.name ASC            )  AS  startflag, ROW_NUMBER() OVER(                PARTITION BY t.TABLE_SCHEMA,                t.TABLE_NAME ORDER BY c.name DESC            )  AS endflag, QUOTENAME(t.TABLE_SCHEMA) AS TABLE_SCHEMA,QUOTENAME(t.TABLE_NAME) AS TABLE_NAME, QUOTENAME(c.name) AS name        FROM   INFORMATION_SCHEMA.TABLES AS t            INNER JOIN syscolumns c                    ON  id = OBJECT_ID( t.TABLE_SCHEMA + '.' + t.TABLE_NAME)        WHERE  t.TABLE_TYPE = 'BASE TABLE'    )SELECT * INTO #COLHELP FROM   cte  t  INSERT INTO @resultSELECT CASE T.STARTFLAG WHEN 1 THEN 'SELECT ' ELSE '' END AS PREFIX,T.NAME,CASE T.ENDFLAG WHEN 1 THEN ' FROM '+t.TABLE_SCHEMA+'.'+t.TABLE_NAME ELSE ',' END AS ENDFIX,T.STARTFLAG,t.TABLE_SCHEMA, t.TABLE_NAME,'SELECT' AS FLAG  FROM #COLHELP T  UNIONSELECT CASE T.STARTFLAG WHEN 1 THEN 'INSERT INTO '+t.TABLE_SCHEMA+'.'+t.TABLE_NAME+'( ' ELSE '' END AS PREFIX,T.NAME,CASE T.ENDFLAG WHEN 1 THEN ' ) '  ELSE ',' END AS ENDFIX,T.STARTFLAG,t.TABLE_SCHEMA, t.TABLE_NAME,'INSERT' AS FLAGFROM #COLHELP T  UNIONSELECT CASE T.STARTFLAG WHEN 1 THEN 'VALUES ( ' ELSE '' END AS PREFIX,'@'+SUBSTRING(T.NAME,2,LEN(T.NAME)-2) AS NAME ,CASE T.ENDFLAG WHEN 1 THEN ' ) '  ELSE ',' END AS ENDFIX,T.STARTFLAG+5000 AS STARTFLAG,t.TABLE_SCHEMA, t.TABLE_NAME,'INSERT' AS FLAGFROM #COLHELP T   UNION SELECT CASE T.STARTFLAG WHEN 1 THEN 'UPDATE T1 SET  ' ELSE '' END AS PREFIX,'T1.'+T.NAME+'=T2.'+T.NAME AS NAME ,CASE T.ENDFLAG WHEN 1 THEN    ' FROM '+t.TABLE_SCHEMA+'.'+t.TABLE_NAME+' T1 INNER JOIN T2' ELSE ',' END AS ENDFIX,T.STARTFLAG,t.TABLE_SCHEMA, t.TABLE_NAME,'UPDATE' AS FLAG  FROM #COLHELP T  if exists(select * from tempdb..sysobjects where id=object_id('tempdb..#COLHELP'))DROP TABLE #COLHELP --SELECT * FROM #RESULT ORDER BY t.TABLE_SCHEMA, t.TABLE_NAME,T.FLAG,t.startflag IF @tblname='ALL'SELECT t.PREFIX, t.NAME, t.ENDFIX,CASE  t.STARTFLAG when 1 then  '/*'+t.flag+' '+ t.TABLE_SCHEMA+'.'+ t.TABLE_NAME+'*/'  ELSE '' END AS annoFROM @result AS t ORDER BY  t.TABLE_SCHEMA, t.TABLE_NAME,t.flag,t.startflag  ELSE    SELECT t.PREFIX, t.NAME, t.ENDFIX,CASE  t.STARTFLAG when 1 then  '/*'+t.flag+' '+ t.TABLE_SCHEMA+'.'+ t.TABLE_NAME+'*/'  ELSE '' END AS annoFROM @result AS tWHERE OBJECT_ID( t.TABLE_SCHEMA + '.' + t.TABLE_NAME)=OBJECT_ID(@tblname) ORDER BY  t.TABLE_SCHEMA, t.TABLE_NAME,t.flag,t.startflag ENDgoIF @@ERROR = 0 PRINT 'Procedure USP_GENERATEDML created'goEXEC [USP_GENERATEDML] 'PERSON.PERSON'EXEC [USP_GENERATEDML] 'ALL'

 

转载于:https://www.cnblogs.com/fuckcn/p/3944556.html

你可能感兴趣的文章
atof函数扩充
查看>>
Python-functools模块
查看>>
xmake高级特性之选项绑定
查看>>
路由原理——多宿主网络案例分析(二)
查看>>
Iredmail 测试
查看>>
我的友情链接
查看>>
ubuntu下简易postfix+dovcote配置
查看>>
计算机类产品评价技术指标体系研究之中央处理器篇
查看>>
GIT导出差量更新包
查看>>
谁说要从hello world开始的
查看>>
.xyz域名总量TOP12:西部数码域名激增 强势问鼎
查看>>
11月第2周全球域名商新增注册量十五强:爱名网季军
查看>>
jstat的用法
查看>>
我的友情链接
查看>>
【微信小程序】从入门到放弃
查看>>
项目规划管理 - 3
查看>>
Linux学习笔记(一)——Linux系统概要及基本操作
查看>>
键盘上的快捷键以及{Win+R}命令,简单分享。
查看>>
Linux系统管理——触发挂载篇
查看>>
php下载时修改文件名
查看>>