ToB企服应用市场:ToB评测及商务社交产业平台

标题: 具体分析SQL Server触发器的根本知识 [打印本页]

作者: 尚未崩坏    时间: 2024-8-15 12:28
标题: 具体分析SQL Server触发器的根本知识
前言

原先写过一篇类似的,不过是基于Mysql:添加链接描述
对应Sql Server的增补知识点:具体设置SQL Server的链接服务器(图文操纵Mysql数据库)
1. 根本知识

根本知识着实差不多,先从CRUD开始
触发器的范例:

触发器的作用

2. Demo

创建触发器:
  1. CREATE TRIGGER trigger_name
  2. ON table_name
  3. [AFTER | INSTEAD OF] {INSERT | UPDATE | DELETE}
  4. AS
  5. BEGIN
  6.     -- 触发器逻辑
  7. END;
复制代码
增加触发器:
  1. CREATE TRIGGER trg_AfterInsert
  2. ON Employees
  3. AFTER INSERT
  4. AS
  5. BEGIN
  6.     INSERT INTO AuditLog (Action, LogDate, EmployeeID)
  7.     SELECT 'INSERT', GETDATE(), EmployeeID
  8.     FROM inserted;
  9. END;
复制代码
更新触发器:
  1. CREATE TRIGGER trg_InsteadOfUpdate
  2. ON Employees
  3. INSTEAD OF UPDATE
  4. AS
  5. BEGIN
  6.     -- 执行自定义的更新逻辑
  7.     UPDATE Employees
  8.     SET Name = inserted.Name
  9.     FROM inserted
  10.     WHERE Employees.EmployeeID = inserted.EmployeeID;
  11. END;
复制代码
检察触发器:
  1. SELECT *
  2. FROM sys.triggers
  3. WHERE parent_id = OBJECT_ID('table_name');
复制代码
删除触发器:
  1. DROP TRIGGER trigger_name;
复制代码
3. 查找特定表的存储过程


  1. SELECT name
  2. FROM sys.procedures;
复制代码


使用sys.sql_modules视图和OBJECT_DEFINITION函数
  1. SELECT OBJECT_NAME(object_id) AS ProcedureName
  2. FROM sys.sql_modules
  3. WHERE OBJECT_DEFINITION(object_id) LIKE '%YourTableName%';
复制代码

多加一个字段属性,可以给出具体的存储过程的界说
  1. SELECT
  2.     OBJECT_NAME(m.object_id) AS ProcedureName,
  3.     m.definition AS ProcedureDefinition
  4. FROM
  5.     sys.sql_modules m
  6. WHERE
  7.     OBJECT_DEFINITION(m.object_id) LIKE '%ags.GateReservationDetail%';
复制代码


  1. SELECT
  2.     OBJECT_NAME(m.object_id) AS ProcedureName,
  3.     m.definition AS ProcedureDefinition
  4. FROM
  5.     sys.sql_modules m
  6. WHERE
  7.     OBJECT_NAME(m.object_id) IN ('View_JobOwner');
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4