SQL server触发器问题

飞不起来诶 2013-05-07 12:19:21
产品编号 varchar(10) not null primary key;
产品名称 varchar(50) not null;
价格 int null;
库数量 int null;

在表中创建触发器,向表中插入新记录时,如不给出产品名称,怎样使产品名称自动显示为产品编号?
...全文
120 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
唐诗三百首 2013-05-07
  • 打赏
  • 举报
回复

-- 建测试表
create table xil
(产品编号 varchar(10) not null primary key,
 产品名称 varchar(20) not null,
 价格 int null,
 库数量 int null)

-- 建触发器
create trigger tr_xil on xil
for insert
as
begin
 update a
  set a.产品名称=a.产品编号
  from xil a
  inner join inserted b on a.产品编号=b.产品编号
  where a.产品名称=''
end
go

-- 插入新记录
insert into xil
 select 'pn001','112233',10,20 union all
 select 'pn002','',11,21 union all  -- 不给出产品名称
 select 'pn003','',12,22            -- 不给出产品名称

-- 结果
select * from xil

/*
产品编号       产品名称             价格          库数量
---------- -------------------- ----------- -----------
pn001      112233               10          20
pn002      pn002                11          21
pn003      pn003                12          22

(3 row(s) affected)
*/
q465897859 2013-05-07
  • 打赏
  • 举报
回复
update 表
set 产品名称=产品编号
where isnull(产品名称,'')=''
yo_yo1120 2013-05-07
  • 打赏
  • 举报
回复
update tbl_name set 产品名称=产品编号 where 产品名称=''

22,299

社区成员

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

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