当我使用过程 GETDATE 时,SQL Server 2008 触发器抛出错误

发布于 2024-11-28 23:22:21 字数 995 浏览 2 评论 0原文

我正在尝试在 SQL Server 2008 中创建一个触发器,该触发器在第一个数据库中更新后将一行插入到第二个数据库中。

但是我不断收到错误..

(过程behaviour_alert,第11行')'附近的语法不正确

这是因为在触发器中使用了DATETIME。

这可以作为普通查询使用,我不明白为什么它不能作为触发器使用。 只能调整查询选择当前日期时间吗?

下面的查询

create trigger behaviour_alert
on [database1].[dbo].[studconduct]
for update
as
begin

declare @conductdatetime as datetime
set @conductdatetime = GETDATE()

insert into database2.dbo.behaviouralert
  select *
  from studconduct
  where curr_ind='Y'
  and cond_pts >= '5'
  and conduct_date >= @conductdatetime

我在这里缺少什么,斗鸡眼地看着这个。也许我喝太多咖啡了。

编辑:这就是我最终得到的结果并且它有效。我错过了触发器末尾的 END

create trigger behaviour_alert
    on [database1].[dbo].[studconduct]
    for update
    as
    begin
    insert into database2.dbo.behaviouralert
      select *
      from studconduct
      where curr_ind='Y'
      and cond_pts >= '5'
      and conduct_date >= datetime
    end

I am trying to create a trigger in SQL Server 2008 which inserts a row into a 2nd database after an update in the 1st database.

However I keep getting an error..

(Procedure behaviour_alert, Line 11 Incorrect syntax near ')'

Is this because of using DATETIME in the trigger.

This works as a normal query, I can't see why it won't work as a trigger.
Can only adjust the query to choose the current datetime?

Query below

create trigger behaviour_alert
on [database1].[dbo].[studconduct]
for update
as
begin

declare @conductdatetime as datetime
set @conductdatetime = GETDATE()

insert into database2.dbo.behaviouralert
  select *
  from studconduct
  where curr_ind='Y'
  and cond_pts >= '5'
  and conduct_date >= @conductdatetime

What am I missing here, going crossed eyed looking at this. Maybe I have had too much coffee.

Edit: this is what I ended up with and it worked. I missed the END at the end of the trigger

create trigger behaviour_alert
    on [database1].[dbo].[studconduct]
    for update
    as
    begin
    insert into database2.dbo.behaviouralert
      select *
      from studconduct
      where curr_ind='Y'
      and cond_pts >= '5'
      and conduct_date >= datetime
    end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

成熟稳重的好男人 2024-12-05 23:22:21

我认为问题不在 GETDATE() 中。
也许您只是忘记了触发器末尾的 END ?

I think the problems is not in GETDATE().
maybe you just forget about END in the end of trigger?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文