TSQL 根据表名生成UPDATE SELECT INSERT

USE [AdventureWorks2012]
go

IF object_id(‘USP_GENERATEDML‘) IS NOT NULL
BEGIN
    PRINT ‘Dropping procedure USP_GENERATEDML‘
    DROP PROCEDURE [USP_GENERATEDML]
    IF @@ERROR = 0 PRINT ‘Procedure USP_GENERATEDML dropped‘
END
go

CREATE PROCEDURE [USP_GENERATEDML]

@TBLNAME NVARCHAR(100)
AS
BEGIN
    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 @result
SELECT 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
UNION
SELECT 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 FLAG
FROM #COLHELP T
UNION
SELECT 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 FLAG
FROM #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 anno
FROM @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 anno
FROM @result AS t
WHERE OBJECT_ID( t.TABLE_SCHEMA + ‘.‘ + t.TABLE_NAME)=OBJECT_ID(@tblname)
 ORDER BY  t.TABLE_SCHEMA, t.TABLE_NAME,t.flag,t.startflag
END
go

IF @@ERROR = 0 PRINT ‘Procedure USP_GENERATEDML created‘
go
EXEC [USP_GENERATEDML] ‘PERSON.PERSON‘

EXEC [USP_GENERATEDML] ‘ALL‘

时间: 2024-10-08 19:51:49

TSQL 根据表名生成UPDATE SELECT INSERT的相关文章

MySQL问题:You can't specify target table '表名' for update in FROM clause

在MySQL中,写SQL语句的时候 ,可能会遇到You can't specify target table '表名' for update in FROM clause这样的错误,它的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中). 1.问题是如何出现的? 数据准备 CREATE TABLE T_Person( pId INT PRIMARY KEY AUTO_INCREMENT, pName VARCHAR(20) ); INSERT INTO T_Pe

根据表名生成表结构 含 主键

------------------------------------------ 根据表名生成表结构 含 主键----------------------------------------declare @TableName varchar(255) set @TableName = 'bao_color' --'company'----------------------------------------declare @str varchar(max)         set @st

MySQL 中 You can't specify target table '表名' for update in FROM clause错误解决办法

背景 在MySQL中,写SQL语句的时候 ,可能会遇到 You can't specify target table '表名' for update in FROM clause 这样的错误 错误含义 它的意思是说,不能先 select 出同一表中的某些值,再 update 这个表(在同一语句中),即不能依据某字段值做判断再来更新某字段的值. 解决问题 将select出的结果再通过中间表select一遍,这样就可以解决错误了 MySQL 中 You can't specify target ta

You can't specify target table '表名' for update in FROM clause”解决方法

You can't specify target table '表名' for update in FROM clause 翻译为:不能先select出同一表中的某些值,再update这个表. 错误语句: update w_workitems ww set ww.endTime = ww.createTime where ww.gid in(select * from a_travel a ,w_workitems b where a.instance_id = b.instanceId and

Sql Server根据表名生成查询的存储过程(查询条件可选)

static void Main(string[] args) { string 表名 = "water_emstime"; string sql = "exec GetTableSelect " + 表名; string ConString = "server=xxx.xxx.xx.xx;database=newFW;uid=sa;pwd=sa"; SqlDataAdapter da = new SqlDataAdapter(sql, ConS

mysql之UPDATE,SELECT,INSERT语法

一 :UPDATE语法   UPDATE 是一个修改表中行的DML语句. #单表语法(常用) UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET assignment_list [WHERE where_condition] [ORDER BY ...] #例如 ORDER BY id DESC; 若ID不重复且自增,故需要先增加的最大的id后增较小id避免因为重复id保存(不常用) [LIMIT row_count] #多表语法 UPDATE

FDQuery多表更新生成sql语句的问题

query.sql='select  a,b,c,d,e from a,b,c where ....'; 来源3个表, 设计时添加字段列表,每个字段有Origin属性 分别是a.a,b.b,c.c格式,表示该字段是那个表. 然后query.edit,query.post方法调用后,生成update或insert语句的时候, UPDATE ttdb.dbo.a SET [a.a] = @P1WHERE [a.a] = @P2 AND [a.b] = @p3 sql server报错,提示 消息 2

SQLSERVER和ORACLE批量处理表名和字段名大写

在sql移植到oracle过程中,都会遇到表名和字段名大写的问题,因为在oracle中默认表名和字段名都是大写的,虽然可以通过使用双引号的方式进行操作,如select * from "testtable"但是很多情况下,这样做的代价很大,因为很多程序的代码是早已经写好的,而要修改oracle系统配置又不安全,一个比较稳妥的办法是批量修改表名和文件名. 先说在SQL中修改表名和字段名的办法:虽然在sql中默认是不区分表名和字段名的大小写的,但是通过sql的DTS进行数据导出导入的时候,如

SQL Server实现跨库查询(跨库select insert)

方法一: select  * from servername.dbo.tablename 方法二: select * from OPENDATASOURCE(         'SQLOLEDB',         'Data Source=远程ip;User ID=sa;Password=密码'         ).库名.dbo.表名where 条件 select top 100 * from OPENDATASOURCE(         'SQLOLEDB',         'Data