企业管理器
--管理
--SQL Server代理
--右键作业
--新建作业
--"常规"项中输入作业名称
--"步骤"项
--新建
--"步骤名"中输入步骤名
--"类型"中选择"Transact-SQL 脚本(TSQL)"
--"数据库"选择执行命令的数据库
--"命令"中输入要执行的语句:insert into B(ID,Time1,Time2,Value)
select 1001,Time1,Time2,Value from A where ID=101
union all
select 1002,Time1,Time2,Value from A where ID=101
union all
select 1003,Time1,Time2,Value from A where ID=102
union all
select 1004,Time1,Time2,Value from A where ID=102
union all
select 1005,A.Time1,A.Time2,A.Value*0.6+B.Value*0.4 
from A,B where A.ID=101 and B.ID=102 and A.Time1=B.Time1 and A.Time2=B.Time2
--确定
--"调度"项
--新建调度
--"名称"中输入调度名称
--"调度类型"中选择你的作业执行安排
--如果选择"反复出现"
--点"更改"来设置你的时间安排
然后将SQL Agent服务启动,并设置为自动启动,否则你的作业不会被执行设置方法:
我的电脑--控制面板--管理工具--服务--右键 SQLSERVERAGENT--属性--启动类型--选择"自动启动"--确定.

解决方案 »

  1.   

    在A表上面加个触发器吧,如果每天的数据量不多的话。数据量较大的话建议用SSIS来做。
    本人对触发器的语法不熟,以下是大体的思路:
    1.如果A表新增记录的ID=101:
        a) 往B插入ID分别为1001和1002的两条记录
        b) 检查B是否存在有ID=1003且Time1+Time2都匹配的记录,无则忽略,
           如果存在,则继续检查B是否存在有ID=1005且Time1+Time2都匹配的记录,有则忽略
           无则插入一条ID=1005的记录,该记录的value按照相应规则计算
    2.如果新增记录的ID=102,同理~~
      

  2.   


    你这个会不会发生主键冲突,能保证从A表select的都是新接收的记录吗 
      

  3.   


    生成JOB的脚本也不难,先在你的机器上手工建立一个作业,然后
    如果需要生成脚本的话,企业管理器—>数据库服务器—>管理目录—>SQL SERVER代理—>作业—>右键你刚完成的作业—>所有任务—>生成SQL脚本,即可生成你需要的脚本。把里面的内容修改一下,就可以拿到客户那去用。
      

  4.   

    --如果你担心主键重复的话,那就判断一下。
    insert into B(ID,Time1,Time2,Value)
    select  * from 
    (select 1001,Time1,Time2,Value from A where ID=101
    union all
    select 1002,Time1,Time2,Value from A where ID=101
    union all
    select 1003,Time1,Time2,Value from A where ID=102
    union all
    select 1004,Time1,Time2,Value from A where ID=102
    union all
    select 1005,A.Time1,A.Time2,A.Value*0.6+B.Value*0.4 
    from A,B where A.ID=101 and B.ID=102 and A.Time1=B.Time1 and A.Time2=B.Time2) c 
    where not exists(select 1 from b where b.ID=c.id and b.Time1=c.Time1 and b.Time2=c.Time2)
      

  5.   

    其实主键冲突是由于你语句的筛选问题。你可以在A表中接个字段:ID ,Time1,Time2 ,Value,IsNew(bit,默认是1,同步后将其更新为0)insert into B(ID,Time1,Time2,Value)
    select 1001,Time1,Time2,Value from A where ID=101 and IsNew=1 union all
    select 1002,Time1,Time2,Value from A where ID=101 and IsNew=1 union all
    select 1003,Time1,Time2,Value from A where ID=102 and IsNew=1 union all
    select 1004,Time1,Time2,Value from A where ID=102 and IsNew=1 union all
    select 1005,A.Time1,A.Time2,A.Value*0.6+B.Value*0.4 
    from A,B 
    where A.ID=101 and A.IsNew=1 and B.ID=102 and B.IsNew=1
     and A.Time1=B.Time1 and A.Time2=B.Time2
      

  6.   

    前面举例时,由于A表和B表字段名一样,各位给的代码调试半天都通不过
    实际上两边表中字段没有一个相同的(类型是对应的),还请各位不厌其烦,指点一下代码
    A表(主键:AID+ATime1+ATime2)
    AID    ATime1        ATime2        AValueB表(主键:BID+BTime1+BTime2)
    BID    BTime1        BTime2        BValue
      

  7.   


    --TRY:
    insert into B(BID,BTime1,BTime2,BValue)
    select  * from 
    (select 1001 AS BID,ATime1,ATime2,AValue from A where AID=101
    union all
    select 1002,ATime1,ATime2,AValue from A where AID=101
    union all
    select 1003,ATime1,ATime2,AValue from A where AID=102
    union all
    select 1004,ATime1,ATime2,AValue from A where AID=102
    union all
    select 1005,A.ATime1,A.ATime2,A.AValue*0.6+B.BValue*0.4 
    from A,B where A.AID=101 and B.BID=102 and A.ATime1=B.BTime1 and A.ATime2=B.BTime2) c 
    where not exists(select 1 from b where b.BID=c.Bid and b.BTime1=c.ATime1 and b.BTime2=c.ATime2)
      

  8.   

    回复15楼,先谢谢了,有个疑问
    select 1005,A.ATime1,A.ATime2,A.AValue*0.6+B.BValue*0.4 
    from A,B where A.AID=101 and B.BID=102 and A.ATime1=B.BTime1 and A.ATime2=B.BTime2
    这几句中,应当是B.BID=1002吧,还有就是这几句是在insert之前执行,此时,B表中还没有记录,我试了几遍,前面的能插入,最终B表中不会插入编号为1005的记录