以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- [求助]sql触发器 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=178640) |
-- 作者:2425004926 -- 发布时间:2022/7/13 11:58:00 -- [求助]sql触发器 我想要达到的效果是,sqlserver数据库,增加一行,ComNumber这一列的值就自动生成 比如,主键是1234,那么fx列就是DW00001234, 再比如,主键是12,那么fx列就是DW00000012 也就是DW后面8个数字 ComNumber 这一列数据类型nvarchar(10) 狐表设置固定长度字符串为10 以下是sqlserver触发器语代码 USE [Exhibition] GOSET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER TRIGGER [dbo].[Fill_No]ON [dbo].[UserCom] AFTER INSERT AS BEGIN UP DATE UserCom SET El = \'DW\'+str(_Identify) ‘是ok的,中间有空格,比如变成DW 234 UP DATE UserCom SET ComNumber = \'DW\'+REPLICATE(\'0\',8-LEN(STR(_Identify)))+STR(_Identify) ’这一单元格值还为空 END 最后一行蓝色代码应该怎么改,老师指点一下,谢谢!
|
-- 作者:有点蓝 -- 发布时间:2022/7/13 13:15:00 -- UP DATE UserCom SET ComNumber = \'DW\'+REPLICATE(\'0\',8-LEN(cast([_Identify] as nvarchar(10))))+cast([_Identify] as nvarchar(10)) |
-- 作者:2425004926 -- 发布时间:2022/7/13 15:17:00 -- 谢谢老师!OK
UP DATE UserCom SET ComNumber = \'DW\'+REPLICATE(\'0\',8-LEN(cast([_Identify] as nvarchar(10))))+cast([_Identify] as nvarchar(10))
|
-- 作者:有点蓝 -- 发布时间:2022/7/13 15:36:00 -- https://www.baidu.com/baidu?ie=UTF-8&word=SqlServer%20%E8%A7%A6%E5%8F%91%E5%99%A8%20%E8%8E%B7%E5%8F%96%E6%96%B0%E5%A2%9E%E8%A1%8C UP DATE UserCom SET ComNumber = \'DW\'+REPLICATE(\'0\',8-LEN(cast([_Identify] as nvarchar(10))))+cast([_Identify] as nvarchar(10)) where [_Identify] = Inserted.[_Identify]
|
-- 作者:2425004926 -- 发布时间:2022/7/13 16:25:00 -- 用这个where [_Identify] = Inserted.[_Identify] 显示其它行代码没有通过, 我改了一下,where ComNumber Is Null 因为新增行这里肯定是空,OK UP DATE UserCom SET ComNumber = \'DW\'+REPLICATE(\'0\',8-LEN(cast([_Identify] as nvarchar(10))))+cast([_Identify] as nvarchar(10)) where ComNumber Is Null 感谢老师!感谢!!感谢!!!
|