数据导入的问题,需要改变txt的格式

black5 2017-12-02 10:53:59
在txt里数据是这样的: txt文件名是年月日

[12:17:46]设备1:
[12:17:46]参数1:100
[12:17:46]参数2:89
[12:17:46]参数3:99
[12:17:46]参数4:80
[12:17:46]参数5: 100
[12:17:48]
[12:17:53]
[12:17:54]
[12:17:57]设备2:
[12:17:57]参数1:99
[12:17:57]参数2:87
[12:17:57]参数3:99
[12:17:57]参数4:80
[12:17:57]参数5: 100
[12:18:00]
[12:18:01]
[12:18:03]
[12:18:05]设备3:
[12:18:05]参数1:99
[12:18:05]参数2:87
[12:18:05]参数3:99
[12:18:05]参数4:80
[12:18:05]参数5: 100
[12:18:07]



想要输入数据库,变成这样几列:
列1时间 列2设备号 列3参数1 列4参数2 列5参数3 列6参数4 列7参数5



列1中要有年月日时分秒 求助如何实现
...全文
284 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
black5 2017-12-04
  • 打赏
  • 举报
回复
引用 9 楼 cloverlay 的回复:
没事。看来你接触这个才两三天吧。考虑到这个,把完整版给你一个,加个条件判断,确保重复运行不报错:
再次感谢!!
cloverlay 2017-12-03
  • 打赏
  • 举报
回复
没事。看来你接触这个才两三天吧。考虑到这个,把完整版给你一个,加个条件判断,确保重复运行不报错: if OBJECT_ID('原始数据','U')is not null drop table 原始数据 create table 原始数据( 原始数据 nvarchar(510) )on [PRIMARY] bulk insert 原始数据 from '你机器上的txt文件的路径及文件名' with(firstrow=1,rowterminator='0x0d0a',tablock) if OBJECT_ID('目标数据库','U')is not null drop table 目标数据库 select convert(varchar(100),getdate(),23)+' '+substring(a.时间,2,8) 时间, a.设备,b.参数1, b.参数2, b.参数3, b.参数4, b.参数5 into 目标数据库 from ( select SUBSTRING(原始数据,1,10) 时间 ,SUBSTRING(原始数据,11,3 ) 设备 from 原始数据 where 原始数据 like '%设备%' group by SUBSTRING(原始数据,1,10) ,SUBSTRING(原始数据,11,3 ) ) a , ( SELECT 时间 , sum (case when 参数 = '参数1' then 参数值 else 0 end) 参数1 , sum (case when 参数 = '参数2' then 参数值 else 0 end) 参数2 , sum (case when 参数 = '参数3' then 参数值 else 0 end) 参数3 , sum (case when 参数 = '参数4' then 参数值 else 0 end) 参数4 , sum (case when 参数 = '参数5' then 参数值 else 0 end) 参数5 FROM (select SUBSTRING(原始数据,1,10) 时间, SUBSTRING(原始数据,11,3 ) 参数, convert(int,SUBSTRING(原始数据,15,10)) 参数值 from 原始数据 where 原始数据 like '%参数%' ) S group by 时间 ) b where a.时间=b.时间 select * from 目标数据库
black5 2017-12-03
  • 打赏
  • 举报
回复
引用 7 楼 cloverlay 的回复:
看来你刚入门啊。 把以下这一段替换一下: create table 原始数据( 原始数据 nvarchar(510) )on [PRIMARY] insert into 原始数据 select '[12:17:46]设备1:' union all select '[12:17:46]参数1:100' union all select '[12:17:46]参数2:89' union all select '[12:17:46]参数3:99' union all select '[12:17:46]参数4:80' union all select '[12:17:46]参数5: 100' union all select '[12:17:48]' union all select '[12:17:53]' union all select '[12:17:54]' union all select '[12:17:57]设备2:' union all select '[12:17:57]参数1:99' union all select '[12:17:57]参数2:87' union all select '[12:17:57]参数3:99' union all select '[12:17:57]参数4:80' union all select '[12:17:57]参数5: 100' union all select '[12:18:00]' union all select '[12:18:01]' union all select '[12:18:03]' union all select '[12:18:05]设备3:' union all select '[12:18:05]参数1:99' union all select '[12:18:05]参数2:87' union all select '[12:18:05]参数3:99' union all select '[12:18:05]参数4:80' union all select '[12:18:05]参数5: 100' 替换成 : create table 原始数据( 原始数据 nvarchar(510) )on [PRIMARY] bulk insert 原始数据 from '你机器上的txt文件的路径及文件名' with(firstrow=2,rowterminator='0x0d0a',tablock) 就行了。
谢谢!我刚学还不会。。。
cloverlay 2017-12-03
  • 打赏
  • 举报
回复
看来你刚入门啊。 把以下这一段替换一下: create table 原始数据( 原始数据 nvarchar(510) )on [PRIMARY] insert into 原始数据 select '[12:17:46]设备1:' union all select '[12:17:46]参数1:100' union all select '[12:17:46]参数2:89' union all select '[12:17:46]参数3:99' union all select '[12:17:46]参数4:80' union all select '[12:17:46]参数5: 100' union all select '[12:17:48]' union all select '[12:17:53]' union all select '[12:17:54]' union all select '[12:17:57]设备2:' union all select '[12:17:57]参数1:99' union all select '[12:17:57]参数2:87' union all select '[12:17:57]参数3:99' union all select '[12:17:57]参数4:80' union all select '[12:17:57]参数5: 100' union all select '[12:18:00]' union all select '[12:18:01]' union all select '[12:18:03]' union all select '[12:18:05]设备3:' union all select '[12:18:05]参数1:99' union all select '[12:18:05]参数2:87' union all select '[12:18:05]参数3:99' union all select '[12:18:05]参数4:80' union all select '[12:18:05]参数5: 100' 替换成 : create table 原始数据( 原始数据 nvarchar(510) )on [PRIMARY] bulk insert 原始数据 from '你机器上的txt文件的路径及文件名' with(firstrow=2,rowterminator='0x0d0a',tablock) 就行了。
black5 2017-12-03
  • 打赏
  • 举报
回复
引用 4 楼 z10843087 的回复:
[quote=引用 楼主 black5 的回复:] 在txt里数据是这样的: txt文件名是年月日 [12:17:46]设备1: [12:17:46]参数1:100 [12:17:46]参数2:89 [12:17:46]参数3:99 [12:17:46]参数4:80 [12:17:46]参数5: 100 [12:17:48] [12:17:53] [12:17:54] [12:17:57]设备2: [12:17:57]参数1:99 [12:17:57]参数2:87 [12:17:57]参数3:99 [12:17:57]参数4:80 [12:17:57]参数5: 100 [12:18:00] [12:18:01] [12:18:03] [12:18:05]设备3: [12:18:05]参数1:99 [12:18:05]参数2:87 [12:18:05]参数3:99 [12:18:05]参数4:80 [12:18:05]参数5: 100 [12:18:07] 想要输入数据库,变成这样几列: 列1时间 列2设备号 列3参数1 列4参数2 列5参数3 列6参数4 列7参数5 列1中要有年月日时分秒 求助如何实现
先想办法吧txt的数据用bulk insert 的方式导入到数据库的表中,然后再用查询转换为你想要的格式。[/quote] 具体怎么做呢? bulk insert?
black5 2017-12-03
  • 打赏
  • 举报
回复
引用 3 楼 cloverlay 的回复:
create table 原始数据( 原始数据 nvarchar(510) )on [PRIMARY] insert into 原始数据 select '[12:17:46]设备1:' union all select '[12:17:46]参数1:100' union all select '[12:17:46]参数2:89' union all select '[12:17:46]参数3:99' union all select '[12:17:46]参数4:80' union all select '[12:17:46]参数5: 100' union all select '[12:17:48]' union all select '[12:17:53]' union all select '[12:17:54]' union all select '[12:17:57]设备2:' union all select '[12:17:57]参数1:99' union all select '[12:17:57]参数2:87' union all select '[12:17:57]参数3:99' union all select '[12:17:57]参数4:80' union all select '[12:17:57]参数5: 100' union all select '[12:18:00]' union all select '[12:18:01]' union all select '[12:18:03]' union all select '[12:18:05]设备3:' union all select '[12:18:05]参数1:99' union all select '[12:18:05]参数2:87' union all select '[12:18:05]参数3:99' union all select '[12:18:05]参数4:80' union all select '[12:18:05]参数5: 100' select convert(varchar(100),getdate(),23)+' '+substring(a.时间,2,8) 时间, a.设备,b.参数1, b.参数2, b.参数3, b.参数4, b.参数5 into 目标数据库 from ( select SUBSTRING(原始数据,1,10) 时间 ,SUBSTRING(原始数据,11,3 ) 设备 from 原始数据 where 原始数据 like '%设备%' group by SUBSTRING(原始数据,1,10) ,SUBSTRING(原始数据,11,3 ) ) a , ( SELECT 时间 , sum (case when 参数 = '参数1' then 参数值 else 0 end) 参数1 , sum (case when 参数 = '参数2' then 参数值 else 0 end) 参数2 , sum (case when 参数 = '参数3' then 参数值 else 0 end) 参数3 , sum (case when 参数 = '参数4' then 参数值 else 0 end) 参数4 , sum (case when 参数 = '参数5' then 参数值 else 0 end) 参数5 FROM (select SUBSTRING(原始数据,1,10) 时间, SUBSTRING(原始数据,11,3 ) 参数, convert(int,SUBSTRING(原始数据,15,10)) 参数值 from 原始数据 where 原始数据 like '%参数%' ) S group by 时间 ) b where a.时间=b.时间 select * from 目标数据库
谢谢,不过好几万行的,都这么写,受不了啊。。。。
OwenZeng_DBA 2017-12-03
  • 打赏
  • 举报
回复
引用 楼主 black5 的回复:
在txt里数据是这样的: txt文件名是年月日 [12:17:46]设备1: [12:17:46]参数1:100 [12:17:46]参数2:89 [12:17:46]参数3:99 [12:17:46]参数4:80 [12:17:46]参数5: 100 [12:17:48] [12:17:53] [12:17:54] [12:17:57]设备2: [12:17:57]参数1:99 [12:17:57]参数2:87 [12:17:57]参数3:99 [12:17:57]参数4:80 [12:17:57]参数5: 100 [12:18:00] [12:18:01] [12:18:03] [12:18:05]设备3: [12:18:05]参数1:99 [12:18:05]参数2:87 [12:18:05]参数3:99 [12:18:05]参数4:80 [12:18:05]参数5: 100 [12:18:07] 想要输入数据库,变成这样几列: 列1时间 列2设备号 列3参数1 列4参数2 列5参数3 列6参数4 列7参数5 列1中要有年月日时分秒 求助如何实现
先想办法吧txt的数据用bulk insert 的方式导入到数据库的表中,然后再用查询转换为你想要的格式。
cloverlay 2017-12-03
  • 打赏
  • 举报
回复
create table 原始数据( 原始数据 nvarchar(510) )on [PRIMARY] insert into 原始数据 select '[12:17:46]设备1:' union all select '[12:17:46]参数1:100' union all select '[12:17:46]参数2:89' union all select '[12:17:46]参数3:99' union all select '[12:17:46]参数4:80' union all select '[12:17:46]参数5: 100' union all select '[12:17:48]' union all select '[12:17:53]' union all select '[12:17:54]' union all select '[12:17:57]设备2:' union all select '[12:17:57]参数1:99' union all select '[12:17:57]参数2:87' union all select '[12:17:57]参数3:99' union all select '[12:17:57]参数4:80' union all select '[12:17:57]参数5: 100' union all select '[12:18:00]' union all select '[12:18:01]' union all select '[12:18:03]' union all select '[12:18:05]设备3:' union all select '[12:18:05]参数1:99' union all select '[12:18:05]参数2:87' union all select '[12:18:05]参数3:99' union all select '[12:18:05]参数4:80' union all select '[12:18:05]参数5: 100' select convert(varchar(100),getdate(),23)+' '+substring(a.时间,2,8) 时间, a.设备,b.参数1, b.参数2, b.参数3, b.参数4, b.参数5 into 目标数据库 from ( select SUBSTRING(原始数据,1,10) 时间 ,SUBSTRING(原始数据,11,3 ) 设备 from 原始数据 where 原始数据 like '%设备%' group by SUBSTRING(原始数据,1,10) ,SUBSTRING(原始数据,11,3 ) ) a , ( SELECT 时间 , sum (case when 参数 = '参数1' then 参数值 else 0 end) 参数1 , sum (case when 参数 = '参数2' then 参数值 else 0 end) 参数2 , sum (case when 参数 = '参数3' then 参数值 else 0 end) 参数3 , sum (case when 参数 = '参数4' then 参数值 else 0 end) 参数4 , sum (case when 参数 = '参数5' then 参数值 else 0 end) 参数5 FROM (select SUBSTRING(原始数据,1,10) 时间, SUBSTRING(原始数据,11,3 ) 参数, convert(int,SUBSTRING(原始数据,15,10)) 参数值 from 原始数据 where 原始数据 like '%参数%' ) S group by 时间 ) b where a.时间=b.时间 select * from 目标数据库
black5 2017-12-03
  • 打赏
  • 举报
回复
引用 1 楼 shoppo0505 的回复:
看不懂看不懂
啊。。。。就是txt是我写的格式,想变成表
shoppo0505 2017-12-03
  • 打赏
  • 举报
回复
看不懂看不懂

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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