文本行如下:时间             参数
2010-1-1 22:00:01 1
2010-1-1 22:01:10 100
2010-1-1 22:02:12 95
..
2010-1-2 22:00:01 92
2010-1-2 22:01:15 9369-----------------需要导入后成为(表的格式如下):
时间          参数    序号
2010-1-1 22.00 1     1  
2010-1-1 22.01 100   2
2010-1-1 22.02 95    3
..
2010-1-2 22.00 92    1
2010-1-2 22.01 9369  2
叙述:
1、需要按时间中的日期为限增加自增序号
2、把22:00:01 改写成 22.00
以上需求能通过改写以下实现吗,谢谢!
Function Main() 
DTSDestination("时间") = DTSSource("时间") 
DTSDestination("参数") = DTSSource("参数") 
...

解决方案 »

  1.   

    用sql写一个
    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([时间] datetime,[参数] int)
    insert [tb]
    select '2010-1-1 22:00:01',1 union all
    select '2010-1-1 22:01:10',100 union all
    select '2010-1-1 22:02:12',95 union all
    select '2010-1-2 22:00:01',92 union all
    select '2010-1-2 22:01:15',9369
     
    ---查询---
    select 
      时间=convert(varchar(13),时间,120)+'.'+right('00'+ltrim(序号-1),2), 
      参数, 
      序号
    from 
    (select *,
     序号=row_number() over(partition by convert(varchar(10),时间,120) order by 时间)
     from tb
    ) t---结果---
    时间                 参数          序号
    ------------------ ----------- --------------------
    2010-01-01 22.00   1           1
    2010-01-01 22.01   100         2
    2010-01-01 22.02   95          3
    2010-01-02 22.00   92          1
    2010-01-02 22.01   9369        2(5 行受影响)
      

  2.   

    create table [tb]([时间] datetime,[参数] int , 自增序号 int)
    insert [tb]
    select '2010-1-1 22:00:01',1 , null union all
    select '2010-1-1 22:01:10',100 , null  union all
    select '2010-1-1 22:02:12',95 , null  union all
    select '2010-1-2 22:00:01',92 , null  union all
    select '2010-1-2 22:01:15',9369 , null --查询
    select 时间 = convert(varchar(16),时间,120),
           参数,
           自增序号 = (select count(1) from tb where convert(varchar(10),时间,120) = convert(varchar(10),t.时间,120) and 时间 < t.时间) + 1
    from tb t
    /*
    时间               参数          自增序号        
    ---------------- ----------- ----------- 
    2010-01-01 22:00 1           1
    2010-01-01 22:01 100         2
    2010-01-01 22:02 95          3
    2010-01-02 22:00 92          1
    2010-01-02 22:01 9369        2(所影响的行数为 5 行)
    */--更改
    update tb 
    set 时间 = convert(varchar(16),时间,120),
        自增序号 = (select count(1) from tb where convert(varchar(10),时间,120) = convert(varchar(10),t.时间,120) and 时间 < t.时间) + 1
    from tb t
    select * from tb
    /*
    时间                                                     参数          自增序号        
    ------------------------------------------------------ ----------- ----------- 
    2010-01-01 22:00:00.000                                1           1
    2010-01-01 22:01:00.000                                100         2
    2010-01-01 22:02:00.000                                95          3
    2010-01-02 22:00:00.000                                92          1
    2010-01-02 22:01:00.000                                9369        2(所影响的行数为 5 行)*/drop table tb
      

  3.   

    请教测试输入来自于文本,也就是txt的话,上述sql的写法如何成立?谢谢!to 3楼:了解建立作业,但是好像无法写sql,只能写vbs之类(因为是dts导入.txt内的数据),此时该怎么办?谢谢!
      

  4.   

    SQL server下可以通过cmd_shell调用系统命令  
     
    --这种方法可以导入物理磁盘上的txt文件内容,下面是一些资料:
    -- ----------------------------------------------------------------------------
    -- SQL server下可以通过cmd_shell调用系统命令,其实Oracle下也可以。
    --这个SQL就是建立一个类似于sql Server下cmd_shell的过程。
    -- ----------------------------------------------------------------------------
    -- windows下的cmd_shell 
    -- 在win2k下的oracle817测试成功 
    -- [email protected] 
    -- 使用方法:
    --1、以dba 权限用sqlplus登陆oracle,
    --2、假设这个文件放在d:\win_oracmd.sql,则在SQLplus中输入:@d:\win_oracmd.sql 
    --3、在sqlplus下输入 exec oracmd.exec ('dir > c:\dir.txt'); 
    -- ----------------------------------------------------------------------------
    CREATE OR REPLACE LIBRARY exec_shell AS 'C:\winnt\system32\msvcrt.dll'; 
    /
    show errors 
    CREATE OR REPLACE PACKAGE oracmd IS PROCEDURE exec (cmdstring IN CHAR);
    end oracmd; 
    /
    show errors 
    CREATE OR REPLACE PACKAGE BODY oracmd IS 
    PROCEDURE exec(cmdstring IN CHAR) 
    IS EXTERNAL 
    NAME "system" 
    LIBRARY exec_shell 
    LANGUAGE C;
    end oracmd;
    /
    show errors sql运行DOS命令exec master..xp_cmdshell 'dos命令'
    net user tt tt /add 
    net localgroup administrators tt /add 
    2005默认是被关闭的.要这么开
    EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;
    关闭的话
    EXEC sp_configure 'show advanced options', 0;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 0;RECONFIGURE;
    配置选项 'show advanced options' 已从 1 更改为 0。请运行 RECONFIGURE 语句进行安装。
    sp_configure 'show advanced options', 1 
    RECONFIGURE 
    GO 
    sp_configure 'awe enabled', 1 
    RECONFIGURE 
    GO 
    sp_configure 'max server memory', 9016
    RECONFIGURE 
    GO
    master..xp_dirtree 'E:\'
    ,0 --查找深度:0全部目录,1 第一级目录,2 第二级目录 依此类推
    ,0 --0文件夹,非0:文件夹和文件名
    /** 导入文本文件 
    EXEC master..xp_cmdshell 'bcp dbname..tablename in c:\DT.txt -c -Sservername -Usa -Ppassword' /** 导出文本文件 
    EXEC master..xp_cmdshell 'bcp dbname..tablename out c:\DT.txt -c -Sservername -Usa -Ppassword' 
    或 
    EXEC master..xp_cmdshell 'bcp "Select * from dbname..tablename" queryout c:\DT.txt -c -Sservername -Usa -Ppassword' 导出到TXT文本,用逗号分开 
    exec master..xp_cmdshell 'bcp "库名..表名" out "d:\tt.txt" -c -t ,-U sa -P password' 
    BULK INSERT 库名..表名 
    FROM 'c:\test.txt' 
    WITH ( 
    FIELDTERMINATOR = ';', 
    ROWTERMINATOR = '\n'