表中有FileName, UserName , FileId 三项
往表中插入FileName, UserName
一个FileName对应多个Usrname,想实现FileId的自增长,如下:
FileName UserName FileId
a 1 1
a 2 1
a 3 1
b 2 2
b 3 2
c 2 3我写了下面的触发器,显示语法错误,能告诉我下错在哪或有什么更好的解决办法么?CREATE TRIGGER `AddFileId` AFTER INSERT ON `userinformation`
FOR EACH ROW
as
begin
declare @NewId int,@i int,@NewFileName char(255)
select @NewFileName=Inserted.FlieName from Inserted
select @NewId=Inserted.Id from Inserted
if exists(select * from UserInformation where FileName=@NewFileName)
begin
select @i=select FileId form UserInformation where FileName=@NewFileName;
update UserInformation
set UserInformation.FileId=@i where UserInformation.Id=NewId;
end
else
begin
select @i=MAX(FileId) FROM UserInformation;
set @i=@i+i
update UserInformation
set UserInformation.FileId=@i where UserInformation.Id=NewId
end
end
第一次写触发器,实在没经验,谢谢帮忙了
往表中插入FileName, UserName
一个FileName对应多个Usrname,想实现FileId的自增长,如下:
FileName UserName FileId
a 1 1
a 2 1
a 3 1
b 2 2
b 3 2
c 2 3我写了下面的触发器,显示语法错误,能告诉我下错在哪或有什么更好的解决办法么?CREATE TRIGGER `AddFileId` AFTER INSERT ON `userinformation`
FOR EACH ROW
as
begin
declare @NewId int,@i int,@NewFileName char(255)
select @NewFileName=Inserted.FlieName from Inserted
select @NewId=Inserted.Id from Inserted
if exists(select * from UserInformation where FileName=@NewFileName)
begin
select @i=select FileId form UserInformation where FileName=@NewFileName;
update UserInformation
set UserInformation.FileId=@i where UserInformation.Id=NewId;
end
else
begin
select @i=MAX(FileId) FROM UserInformation;
set @i=@i+i
update UserInformation
set UserInformation.FileId=@i where UserInformation.Id=NewId
end
end
第一次写触发器,实在没经验,谢谢帮忙了
解决方案 »
- 求助一个用存储过程实现的mysql数据库查找的问题,求大侠解决啊!
- 求一存储过程或sql语句,批量动态更新,mysql,数据量大
- 天啊unknown variables master-host
- 将sql2005存储过程转为mysql存储过程
- MySQL不能连接……
- 求教:MySQL中如何计算两个时间之间相差的秒数?
- 朋友的一个问题,我没有解决掉,汗,请高手相助。
- 还是两表查询问题 union all 为什么只能取到其中的前面的而后没有,求大侠指点。
- 使用mysqldump的--all-databases参数完整备份数据库时如何排除掉某些不需要备份的库?
- 关于MySQL Update 别名的问题
- 使用gunzip恢复备份文件报错
- 插入mysql数据库报Incorrect string value: '\xF0\x9F\x90\x82'错误
一张是fieldname, fieldid (fieldid设为自增长即可)
另一张是fieldid, username
select @NewFileName=Inserted.FlieName from Inserted MYSQL没有Inserted表
select @NewId=Inserted.Id from Inserted if exists(select * from UserInformation where FileName=@NewFileName) then ...
SET @b='';
SELECT *,@a:=IF(@b=FileName ,@a,@a+1),@b:=FileName FROM tt
SET @a=0;
SET @b='';
SELECT *,@a:=IF(@b=FileName ,@a,@a+1),@b:=FileName FROM tt
放到触发器了么?
没作用呀