sql server触发器比较麻烦的问题

gejun2000 2002-12-02 05:58:41
我有两个表一个是 [发送表] 字段:id,日期,记录类型,记录
另外一个表是统计不同记录类型的数量 [日统计表] 字段 :日期,记录类型,记录条数
要求是:触发条件是[发送表]insert,产生判断[日统计表]里面是否有[发送表] 的今天的时间和记录条数,如果有的话更新记录条数,没有的话添加时间和类型
高分求助~
谢谢帮忙~~~~~~~~~~
...全文
169 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gejun2000 2002-12-03
  • 打赏
  • 举报
回复
另外在 insert datebiao (type) values ('1')中我想把写入SendMsg中的type变量如果检测到datebiao中没有该type的话同样以变量的形式也写入到datebiao中,帮忙~~
呵呵~~~~~~
gejun2000 2002-12-03
  • 打赏
  • 举报
回复
能够具体解释一下里面的语法错误吗
我敲了半天
[发送表] 是SendMsg
[日统计表]是datebiao
可惜有错误帮忙找一找
CREATE TRIGGER yes ON [dbo].[269_139_SendMsg]
FOR INSERT
AS begin
if exists(select 1 from [dbo].[269_139_SendMsg] where cast(SendMsg.sendtime as varchar(11))=cast(getdate() as varchar(11)))
update datebiao set type='2' where cast(datebiao.ServerDate as varchar(11))=cast(getdate() as varchar(11))
else
insert datebiao (type) values ('1')
end
l_agang 2002-12-03
  • 打赏
  • 举报
回复
对不起!这句:
insert datebiao (type) values select Type from Inserted
应改为
insert into datebiao (type) select Type from Inserted
l_agang 2002-12-03
  • 打赏
  • 举报
回复
CREATE TRIGGER yes ON [dbo].[269_139_SendMsg]
FOR INSERT
AS begin
if exists(select * from [dbo].[269_139_SendMsg] where cast(SendMsg.sendtime as varchar(11))=cast(getdate() as varchar(11)))
update datebiao set type='2' where cast(datebiao.ServerDate as varchar(11))=cast(getdate() as varchar(11))
else
insert datebiao (type) values select Type from Inserted
end
暂时这样改,如果还有错误请将错误信息贴出来
pengdali 2002-12-02
  • 打赏
  • 举报
回复
create trigger 名 on 发送表
for insert
as
begin
if exists(select 1 from 发送表 where cast(日期 as varchar(11))=cast(getdate() as varchar(11)))
update 日统计表 set 记录条数=记录条数+1 where cast(日期 as varchar(11))=cast(getdate() as varchar(11)))
else
insert 日统计表 select * from inserted
end
XLYT 2002-12-02
  • 打赏
  • 举报
回复
你已经有思路了,用代码写出来就行了。

22,302

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧