AndroidAL
AndroidAL

Reputation: 1119

Schedule sql query on create of new record

Is it possible to a schedule an sql query when a new record is added. At the moment I have a query that is scheduled to execute once a day at a certain time.

Thanks,

EDIT 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.

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.

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

In my case @InputString was the fieldname that was being updated.

Upvotes: 0

Views: 85

Answers (2)

Simone
Simone

Reputation: 1924

You should use trigger for this:

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

Upvotes: 2

Beau6601
Beau6601

Reputation: 71

You could look in to using a Trigger, you can do this on Inserts so every time a new row is inserted it will fire and run your query.

Upvotes: 0

Related Questions