简体   繁体   English

在创建新记录时安排SQL查询

[英]Schedule sql query on create of new record

Is it possible to a schedule an sql query when a new record is added. 添加新记录时,是否可以计划SQL查询。 At the moment I have a query that is scheduled to execute once a day at a certain time. 目前,我有一个查询,该查询计划在某个时间每天执行一次。

Thanks, 谢谢,

EDIT 1; 编辑1; Thank you everyone for your comments. 谢谢大家的评论。 To test this here is my trigger below, is my syntax correct? 为了测试这一点,下面是我的触发器,我的语法正确吗? What im trying to do, is when a data is entered convert it to TitleCase. 我想做的是,当输入数据时,将其转换为TitleCase。

CREATE TRIGGER [dbo].[TableInsert]
ON [Table] 
AFTER INSERT
AS
BEGIN
DECLARE @InputString varchar(4000) 
DECLARE @Index          INT
DECLARE @Char           CHAR(1)
DECLARE @PrevChar       CHAR(1)
DECLARE @OutputString   VARCHAR(255)

SET @OutputString = LOWER(@InputString)
SET @Index = 1

WHILE @Index <= LEN(@InputString)
BEGIN
SET @Char     = SUBSTRING(@InputString, @Index, 1)
SET @PrevChar = CASE WHEN @Index = 1 THEN ' '
                     ELSE SUBSTRING(@InputString, @Index - 1, 1)
                END

IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')
BEGIN
    IF @PrevChar != '''' OR UPPER(@Char) != 'S'
        SET @OutputString = STUFF(@OutputString, @Index, 1, UPPER(@Char))
END

SET @Index = @Index + 1
END

RETURN @OutputString

END

EDIT 2: @InputString was used when calling the function. 编辑2:调用函数时使用了@InputString

update dbo.mytable
set fieldName=[dbo].[InitCap](@InputString);

In my case @InputString was the fieldname that was being updated. 在我的情况下, @InputString是要更新的字段名。

You should use trigger for this: 您应该为此使用触发器:

CREATE TRIGGER [dbo].[TableInsert]
ON [Table] 
AFTER INSERT
AS
--Do your magic

您可以使用触发器,可以在插入项上执行此操作,因此,每次插入新行时,都会触发并运行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM