sql数据库设计时字段值用公式表示的问题

zilu2008 2006-08-28 02:16:00
表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不能包含在自己的公式里
求正确的答案
...全文
543 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zilu2008 2006-08-31
  • 打赏
  • 举报
回复
刚刚测试通过的标准正确答案 谁来评说下啊 哪个答案好


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
zilu2008 2006-08-30
  • 打赏
  • 举报
回复
我是需要写在数据库里的
因为我不想去改程序

我把你的语句改成这样 然后直接写在表设计状态下的公式里
case when a1<=0 and isdate(select @sql+a2 from TABLEA set @sql=@sql)=0 then getdate() else '' end
是不行的
我想是我改错了吧
因为我只懂得写简单得存储过程 什么触发器 动态SQL都没学过哦
leagenary 2006-08-30
  • 打赏
  • 举报
回复
构造动态SQL可以实现
set @sql='case when a1<=0 and isdate('
select @sql+a2 from tableA
set @sql=@sql+')=0 then getdate() else '''' end'
exec(@sql)

大概这样的写法,没有调试!
zilu2008 2006-08-30
  • 打赏
  • 举报
回复
换个问题 就问触发器
表 [产品] 里有 [产品ID] [产品数量] [断货时间] 等字段

断货时间 与 产品数量 的关系是
当 产品数量<=0 时 断货时间=getdate()
当 产品数量>0 时 断货时间=null

下面是我写的触发器 当然是错的 希望高手帮忙改正
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
zilu2008 2006-08-29
  • 打赏
  • 举报
回复
触发器??
汗 不懂哦

怎么建啊
fxf66 2006-08-29
  • 打赏
  • 举报
回复
实在不行就建个触发器
zilu2008 2006-08-28
  • 打赏
  • 举报
回复
a2的值改变是有条件的 当a1<=0时它记录这一时刻的时间
然后直到a1的值大于0 a1又重新为空
zilu2008 2006-08-28
  • 打赏
  • 举报
回复
非常感谢
不过还没解决我的问题
我想让a2的值由数据库来自动改变
所以公式是写在数据库里的
并且a2记录的时间反映的是a1值变为0时那个时刻的时间
这个时间知道a1的值变的大于0时才改变
再次感谢dutguoyi(新鲜鱼排)
这是我第一次在这里提问 终于有人回答了:))
新鲜鱼排 2006-08-28
  • 打赏
  • 举报
回复
--这样的意思么??
create table t(A1 int, A2 datetime)
insert t select -1,null
union all select 1,null

select * from t
update t
set A2=case when A1<=0 and A2 is null then getdate() else null end
select * from t

drop table t

/*
A1 A2
----------- ------------------------------------------------------
-1 NULL
1 NULL

(2 row(s) affected)


(2 row(s) affected)

A1 A2
----------- ------------------------------------------------------
-1 2006-08-28 16:52:41.000
1 NULL

(2 row(s) affected)
*/

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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