表A里有字段A1 A2
A1是数据类型的,大小会随时变化
A2根据A1求值
当A1<=0 并且A2要为空(不希望A2变化) 时A2等于此时的时间,以后不再改变除非A1>0
当A1>0时 A2为空
我是这样写的
case when a1<=0 and isdate(a2)=0 then getdate() else '' end
这是错的 因为a2不能包含在自己的公式里
求正确的答案
...全文
5439打赏收藏
sql数据库设计时字段值用公式表示的问题
表A里有字段A1 A2 A1是数据类型的,大小会随时变化 A2根据A1求值 当A10 当A1>0时 A2为空 我是这样写的 case when a1<=0 and isdate(a2)=0 then getdate() else '' end 这是错的 因为a2不能包含在自己的公式里 求正确的答案
CREATE TRIGGER [uptime] ON [产品]
FOR INSERT, UPDATE
AS
DECLARE @PID INT
declare @stock int
declare @stime datetime
IF @@ROWCOUNT = 1
begin
SELECT @stock=[产品数量], @stime=[断货时间], @PID=[产品ID] from INSERTED
if (@stock<=0 and @stime is null)
update 产品 set 断货时间=getdate() WHERE [产品ID] = @PID
else
if (@stock>0 and @stime is not null)
update 产品 set 断货时间=null WHERE [产品ID] = @PID
end
else
begin
declare tb cursor for select [产品id],产品数量,stime from inserted
open tb
fetch next from tb into @PID,@stock,@stime
while @@fetch_status=0
begin
if (@stock<=0 and @stime is null)
update 产品 set 断货时间=getdate() WHERE [产品ID]= @PID
else
if (@stock>0 and @stime is not null)
update 产品 set 断货时间=null WHERE [产品ID] = @PID
fetch next from tb into @PID,@stock,@stime
end
close tb
deallocate tb
end
另一正确答案 不知道哪个好
CREATE TRIGGER [uptime] ON [产品]
FOR INSERT, UPDATE
AS
update 产品 set 断货时间=getdate() where
断货时间 is null and 产品数量 <= 0
update 产品 set 断货时间=null where
断货时间 is not null and 产品数量 > 0
我把你的语句改成这样 然后直接写在表设计状态下的公式里
case when a1<=0 and isdate(select @sql+a2 from TABLEA set @sql=@sql)=0 then getdate() else '' end
是不行的
我想是我改错了吧
因为我只懂得写简单得存储过程 什么触发器 动态SQL都没学过哦
下面是我写的触发器 当然是错的 希望高手帮忙改正
CREATE TRIGGER [uptime] ON [产品]
FOR INSERT, UPDATE
AS
declare @stock int
declare @stime datetime(8)
set @stock=(select 产品数量 from 产品),
set @stime=(select 断货时间 from 产品),
if (@stock<=0 and isdate(@stime)=0)
begin
update 产品 set 断货时间=getdate()
end
if (@stock>0 and isdate(@stime)=1)
begin
update 产品 set 断货时间=null
end