文章详情

短信预约信息系统项目管理师 报名、考试、查分时间动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

触发器insert

2020-05-08 03:53

关注

USE [stalentzx]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[GZ_HISTORY_INSERT]
ON [dbo].[ySysColumns]
AFTER INSERT--,DELETE,UPDATE
AS
BEGIN
DECLARE @TABLENAME VARCHAR(50)
DECLARE @COLNAME VARCHAR(50)
DECLARE @COLORDER INT
DECLARE @COLTYPE VARCHAR(50)

DECLARE @TABLE_SQL VARCHAR(MAX)
DECLARE @COL_STR VARCHAR(MAX)

SELECT @TABLENAME = TABLENAME , @COLNAME = COLNAME FROM INSERTED
IF @TABLENAME LIKE "GZ_PATSETDATA%" AND
ISNUMERIC(RIGHT(@TABLENAME , 4)) = 1

BEGIN

SELECT * INTO #YSYSCOLUMNS FROM YSYSCOLUMNS WHERE TABLENAME = @TABLENAME

IF EXISTS(SELECT 1 FROM YSYSCOLUMNS
WHERE TABLENAME = "C2" + RIGHT(@TABLENAME , 4)
)

BEGIN

SET @COL_STR = ""
SELECT @COL_STR = @COL_STR + "," + COLNAME + " " +
(CASE WHEN COLTYPE IN ("VARCHAR","CHAR")
THEN COLTYPE + "(" + CONVERT(VARCHAR(4000) , ColWidth) + ")"

WHEN COLTYPE IN ("DECIMAL" , "NUMERIC")
THEN ColType + "(" + CONVERT(VARCHAR(4000) , ColWidth) + "," +
CONVERT(VARCHAR(4000) , ColPrecision) + ")"
ELSE COLTYPE
END) +

(CASE WHEN ISNULL(YSYSCOLUMNS.ColDefault , "") <> ""
THEN (CASE WHEN CHARINDEX("," , ColDefault) > 0
THEN " Default " +
SUBSTRING(ColDefault , 1 , CHARINDEX("," , ColDefault) - 1)
ELSE " Default " + ColDefault
END)
ELSE ""
END)
FROM YSYSCOLUMNS
WHERE TABLENAME="GZ_PATSETDATA" + RIGHT(@TABLENAME , 4) AND
COLNAME = @COLNAME AND
COLNAME NOT IN(SELECT COLNAME FROM YSYSCOLUMNS
WHERE TABLENAME = "C2" + RIGHT(@TABLENAME,4) AND
COLNAME = @COLNAME
)

SELECT @TABLE_SQL = "ALTER TABLE C2" + RIGHT(@TABLENAME,4) + " ADD " +
SUBSTRING(@COL_STR , 2 , 40000) + ";"

EXEC (@TABLE_SQL)

INSERT INTO YSYSCOLUMNS(TableName , ColName , ColOrder , ColType , ColWidth ,
ColPrecision , ColNull , ColDefault , DisplayLabel , DisplayWidth ,
DisplayFormat , EditFormat , ColVarify , VarifyMsg , ColVisible ,
ColProperty , ColGroup , enus , zhtw , OtherLanguage ,
RelationRule , colgroupOther)
SELECT "C2" + RIGHT(@TABLENAME , 4) , ColName , ColOrder , ColType , ColWidth ,
ColPrecision , ColNull , ColDefault , DisplayLabel , DisplayWidth ,
DisplayFormat , EditFormat , ColVarify , VarifyMsg , ColVisible ,
ColProperty , ColGroup , enus , zhtw , OtherLanguage ,
RelationRule , colgroupOther
FROM INSERTED
WHERE COLNAME NOT IN(SELECT COLNAME FROM YSYSCOLUMNS
WHERE TABLENAME = "C2" + RIGHT(@TABLENAME,4) AND
COLNAME = @COLNAME
)

END

ELSE
BEGIN
-----------------------------------创建历史记录物理表---------------------------------
SET @COL_STR = ""
SELECT @COL_STR = @COL_STR + "," + ColName + " " +
(CASE WHEN COLTYPE IN ("VARCHAR","CHAR")
THEN COLTYPE + "(" + CONVERT(VARCHAR(4000) , ColWidth) + ")"

WHEN COLTYPE IN ("DECIMAL" , "NUMERIC")
THEN ColType + "(" + CONVERT(VARCHAR(4000) , ColWidth) + "," +
CONVERT(VARCHAR(4000) , ColPrecision) + ")"
ELSE COLTYPE
END) +

(CASE WHEN ISNULL(YSYSCOLUMNS.ColDefault , "") <> ""
THEN (CASE WHEN CHARINDEX("," , ColDefault) > 0
THEN " Default " +
SUBSTRING(ColDefault , 1 , CHARINDEX("," , ColDefault) - 1)
ELSE " Default " + ColDefault
END)
ELSE ""
END)
FROM YSYSCOLUMNS
WHERE TABLENAME="GZ_PATSETDATA" + RIGHT(@TABLENAME,4)

SELECT @TABLE_SQL = "CREATE TABLE C2" + RIGHT(@TABLENAME,4) +
" (" + SUBSTRING(@COL_STR , 2 , 40000) + ");"
EXEC (@TABLE_SQL)

------由于不存在任何对应的历史表薪资项栏位描述信息,所以需建立对应的历史表描述
SELECT * INTO #YSYSTABLES FROM YSYSTABLES
WHERE TABLENAME = "GZ_PATSETDATA"+ RIGHT(@TABLENAME,4)

INSERT INTO ySysTables(TableName , TableTypeID , TableOrder , TableLabel , ACessable ,
ACessModule , TableVisible ,PRESERE , ISUSERDISPLAY , TEAMVISIBLE , ModuleID)
SELECT "C2"+RIGHT(@TABLENAME,4) , 7 , TableOrder , TableLabel+"_H" , "111" ,
"010000000000000000000000000000" , "1" , 0 , 1 , 1 , "01"
FROM #YSYSTABLES

------建立对应历史表的薪资项栏位信息描述
INSERT INTO YSYSCOLUMNS(TableName , ColName , ColOrder , ColType , ColWidth ,
ColPrecision , ColNull , ColDefault , DisplayLabel , DisplayWidth ,
DisplayFormat , EditFormat , ColVarify , VarifyMsg , ColVisible ,
ColProperty , ColGroup , enus , zhtw , OtherLanguage ,
RelationRule , colgroupOther)
SELECT "C2" + RIGHT(@TABLENAME , 4) , ColName , ColOrder , ColType , ColWidth ,
ColPrecision , ColNull , ColDefault , DisplayLabel , DisplayWidth ,
DisplayFormat , EditFormat , ColVarify , VarifyMsg , ColVisible ,
ColProperty , ColGroup , enus , zhtw , OtherLanguage ,
RelationRule , colgroupOther
FROM #YSYSCOLUMNS
END

END

END

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯