比如
有 
(说明一下列都是 float类型。都是数字的)
表A 的 II字段
表B 的 JJ字段我想让 
表A的II字段 减去 表B的JJ字段 表A II字段得到减去后的结果表B的JJ字段这时清零请问下这SQL句子该怎么写?? 
我想用SQL的作业来实现。。 不知道好不好?

解决方案 »

  1.   

    --这是一个简单的例子,是你想要结果吗?create table t(id int,num int)
    insert into t
    select 1,100
    union all select 2,200
    union all select 3,400create table b(id int ,num2 int)
    insert into b
    select 1,50
    union all select 2,80
    union all select 3,110update t set t.num=t.num-b.num2 from t inner join b on t.id=b.id
    update b set num2=0
    goselect * from t
    select * from bdrop table t,b
      

  2.   


    --这样也可以
    --这是一个简单的例子,是你想要结果吗?create table t(id int,num int)
    insert into t
    select 1,100
    union all select 2,200
    union all select 3,400create table b(id int ,num2 int)
    insert into b
    select 1,50
    union all select 2,80
    union all select 3,110update t set t.num=t.num-b.num2 from b where t.id=b.id
    update b set num2=0
    goselect * from t
    select * from bdrop table t,b
      

  3.   

    一条语句无法实现,因为要更新两个表的数据,
    如果要保证数据的完整性,应该使用事务来实现
     BEGIN   TRANSACTION T1
    update t set t.num=t.num-b.num2 from t inner join b on t.id=b.id
     IF   @@ERROR<>0   THEN   GOTO   problem     
    update b set num2=0
     IF   @@ERROR<>0   THEN   GOTO   problem 
     COMMIT   TRANSACTION T1   
    reutrn problem:   
      ROLLBACK   TRANSACTION T1
      

  4.   

    create table t(id int,num float)
    insert into t
    select 1,100
    union all select 2,200
    union all select 3,400create table b(id int ,num2 float)
    insert into b
    select 1,50
    union all select 2,80
    union all select 3,110update t set t.num=t.num-b.num2 from b where t.id=b.id
    update b set num2=0exec UpdateDataselect * from t
    select * from bdrop table t,b
      

  5.   

    为什么我写了上面的语句 t表 是显示 紫色 但是B表却黑色。 运行后说 t表附近有错误
      

  6.   

    create table T(ID int,num float)
    insert T select 1,5.5
    create table T2(ID int,num float)
    insert T2 select 1,4.5go
    ---先减
    update T
    set num=t.num-t2.num
    from T2
    where T.ID=t2.ID--清0
    update t2
    set num=0
    from t join t2 on t.ID=t2.ID
    -------
    select * from T
    select * from T2
    /*
    ID          num                                                   
    ----------- ----------------------------------------------------- 
    1           1.0(所影响的行数为 1 行)ID          num                                                   
    ----------- ----------------------------------------------------- 
    1           0.0(所影响的行数为 1 行)
    */企业管理器 
    --管理 
    --SQL Server代理 
    --右键作业 
    --新建作业 
    --"常规"项中输入作业名称 
    --"步骤"项 
    --新建 
    --"步骤名"中输入步骤名 
    --"类型"中选择"Transact-SQL 脚本(TSQL)" 
    --"数据库"选择执行命令的数据库 
    --"命令"中输入要执行的语句: 
                          SQL的语句.............................--确定 
    --"调度"项 
    --新建调度 
    --"名称"中输入调度名称 
    --"调度类型"中选择你的作业执行安排 
    --如果选择"反复出现" 
    --点"更改"来设置你的时间安排为一天一次 
    然后将SQL Agent服务启动,并设置为自动启动,否则你的作业不会被执行 设置方法: 
    我的电脑--控制面板--管理工具--服务--右键 SQLSERVERAGENT--属性--启动类型--选择"自动启动"--确定.