Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 将多行记录上载到SQL Server_Sql Server 2008_Ms Access_Import_Multiline_Bcp - Fatal编程技术网

Sql server 2008 将多行记录上载到SQL Server

Sql server 2008 将多行记录上载到SQL Server,sql-server-2008,ms-access,import,multiline,bcp,Sql Server 2008,Ms Access,Import,Multiline,Bcp,我们从客户机接收固定长度的数据集,这些数据集如下所示: 1 SOMEFILE 20110922 2 20110101ABC999 3 JOHN SMITH 19800201 4 5000000 1000 2 20060101DEF999 3 JANE KOTZE 19811001 4 200000 800 5 5200000 1800 20110101ABC999JOHN SMIT

我们从客户机接收固定长度的数据集,这些数据集如下所示:

1 SOMEFILE   20110922
2 20110101ABC999  
3 JOHN         SMITH     19800201
4 5000000       1000
2 20060101DEF999  
3 JANE         KOTZE     19811001
4 200000        800
5 5200000       1800
20110101ABC999JOHN         SMITH     198002015000000       1000
20060101DEF999JANE         KOTZE     19811001200000        800
其中,每行第一个位置的数字表示该行中的信息类型。这些类型包括:

1  Header record (only appears once, in the first line)  
2  Contract record  
3  Person record  
4  Amounts record  
5  Trailer record (only appears once, in the last line)
2、3和4中的信息实际上都与一条记录有关,我需要在上传阶段找到一种方法将它们合并为一条记录。没有明确指定2、3和4的哪些组合属于彼此的标识符,但在所有情况下,它们在原始数据中的顺序都是显示在彼此的正下方

我需要的是一个预处理步骤,它将获取原始数据,然后将正确的2、3和4行合并到一个记录中(然后再次输出为txt文件),如下所示:

1 SOMEFILE   20110922
2 20110101ABC999  
3 JOHN         SMITH     19800201
4 5000000       1000
2 20060101DEF999  
3 JANE         KOTZE     19811001
4 200000        800
5 5200000       1800
20110101ABC999JOHN         SMITH     198002015000000       1000
20060101DEF999JANE         KOTZE     19811001200000        800
我曾想过将bcp'ing转换成SQL(甚至只是使用Access)并将一个自动递增的整数指定为PK。i、 e:

  PK Type  Record 
  1  1     SOMEFILE   20110922
  2  2     20110101ABC999  
  3  3     JOHN         SMITH     19800201
  4  4     5000000       1000
  5  2     20060101DEF999  
  6  3     JANE         KOTZE     19811001
  7  4     200000        800
  8  5     5200000       1800
然后做一些类似的事情:

select 
type2.[record]+type3.[record]+type4.[record]
from

(select [record] from uploaded where [type]=2) as type2

join
(select [record] from uploaded where [type]=3) as type3
on type2.PK + 1 = type3.PK

join
(select [record] from uploaded where [type]=4) as type4
on type2.PK + 2 = type4.PK
但我担心的是,这完全取决于SQL Server按照数据在输入文件中出现的顺序分配PKs;我不确定情况是否必然如此

有人知道吗?或者知道更好的方法吗

谢谢

卡尔

编辑:添加了第二种解决方案

解决方案1:

$rows = GET-CONTENT -PATH D:\BD\Samples\MyData.txt

$rows[0]="00000 "+$row
$rows[$rows.length-1]="99999 "+$row

$groupid=0

for($i=1; $i -lt $rows.length-1; $i=$i+3)
{
    $groupid++

    $row = $rows[$i]
    $temp=("00000"+[string]$groupid)
    $rows[$i]=$temp.substring($temp.length-5)+" "+$row

    $row = $rows[$i+1]
    $temp=("00000"+[string]$groupid)
    $rows[$i+1]=$temp.substring($temp.length-5)+" "+$row

    $row = $rows[$i+2]
    $temp=("00000"+[string]$groupid)
    $rows[$i+2]=$temp.substring($temp.length-5)+" "+$row        

}

SET-CONTENT -PATH D:\BD\Samples\MyDataResults2.txt $rows
您无法确定SQL Server插入顺序。 在SQL Server中导入数据之前,必须进行一些文本文件处理。例如,您可以使用
PowerShell
PK
添加到文件中,从而:

$rows = GET-CONTENT -PATH D:\BD\Samples\MyData.txt

for($i=0; $i -lt $rows.length; $i++)
{
    $row = $rows[$i]
    $temp=("00000"+[string]($i+1))
    $rows[$i]=$temp.substring($temp.length-5)+" "+$row

}

SET-CONTENT -PATH D:\BD\Samples\MyDataResults.txt $rows
之前(
MyData.txt
content):

PowerShell
处理(
MyDataResults.txt
content)之后:

在这两个PS脚本中,我假设您最多可以插入99999行

解决方案2:

$rows = GET-CONTENT -PATH D:\BD\Samples\MyData.txt

$rows[0]="00000 "+$row
$rows[$rows.length-1]="99999 "+$row

$groupid=0

for($i=1; $i -lt $rows.length-1; $i=$i+3)
{
    $groupid++

    $row = $rows[$i]
    $temp=("00000"+[string]$groupid)
    $rows[$i]=$temp.substring($temp.length-5)+" "+$row

    $row = $rows[$i+1]
    $temp=("00000"+[string]$groupid)
    $rows[$i+1]=$temp.substring($temp.length-5)+" "+$row

    $row = $rows[$i+2]
    $temp=("00000"+[string]$groupid)
    $rows[$i+2]=$temp.substring($temp.length-5)+" "+$row        

}

SET-CONTENT -PATH D:\BD\Samples\MyDataResults2.txt $rows
结果:

00000 4 200000        800
00001 2 20110101ABC999
00001 3 JOHN         SMITH     19800201
00001 4 5000000       1000
00002 2 20060101DEF999
00002 3 JANE         KOTZE     19811001
00002 4 200000        800
99999 4 200000        800

编辑:添加了第二个解决方案

解决方案1:

$rows = GET-CONTENT -PATH D:\BD\Samples\MyData.txt

$rows[0]="00000 "+$row
$rows[$rows.length-1]="99999 "+$row

$groupid=0

for($i=1; $i -lt $rows.length-1; $i=$i+3)
{
    $groupid++

    $row = $rows[$i]
    $temp=("00000"+[string]$groupid)
    $rows[$i]=$temp.substring($temp.length-5)+" "+$row

    $row = $rows[$i+1]
    $temp=("00000"+[string]$groupid)
    $rows[$i+1]=$temp.substring($temp.length-5)+" "+$row

    $row = $rows[$i+2]
    $temp=("00000"+[string]$groupid)
    $rows[$i+2]=$temp.substring($temp.length-5)+" "+$row        

}

SET-CONTENT -PATH D:\BD\Samples\MyDataResults2.txt $rows
您无法确定SQL Server插入顺序。 在SQL Server中导入数据之前,必须进行一些文本文件处理。例如,您可以使用
PowerShell
PK
添加到文件中,从而:

$rows = GET-CONTENT -PATH D:\BD\Samples\MyData.txt

for($i=0; $i -lt $rows.length; $i++)
{
    $row = $rows[$i]
    $temp=("00000"+[string]($i+1))
    $rows[$i]=$temp.substring($temp.length-5)+" "+$row

}

SET-CONTENT -PATH D:\BD\Samples\MyDataResults.txt $rows
之前(
MyData.txt
content):

PowerShell
处理(
MyDataResults.txt
content)之后:

在这两个PS脚本中,我假设您最多可以插入99999行

解决方案2:

$rows = GET-CONTENT -PATH D:\BD\Samples\MyData.txt

$rows[0]="00000 "+$row
$rows[$rows.length-1]="99999 "+$row

$groupid=0

for($i=1; $i -lt $rows.length-1; $i=$i+3)
{
    $groupid++

    $row = $rows[$i]
    $temp=("00000"+[string]$groupid)
    $rows[$i]=$temp.substring($temp.length-5)+" "+$row

    $row = $rows[$i+1]
    $temp=("00000"+[string]$groupid)
    $rows[$i+1]=$temp.substring($temp.length-5)+" "+$row

    $row = $rows[$i+2]
    $temp=("00000"+[string]$groupid)
    $rows[$i+2]=$temp.substring($temp.length-5)+" "+$row        

}

SET-CONTENT -PATH D:\BD\Samples\MyDataResults2.txt $rows
结果:

00000 4 200000        800
00001 2 20110101ABC999
00001 3 JOHN         SMITH     19800201
00001 4 5000000       1000
00002 2 20060101DEF999
00002 3 JANE         KOTZE     19811001
00002 4 200000        800
99999 4 200000        800

我是从接触的角度讲的。数据已经在Access中还是在文本文件中?如果它在Access中没有主键,那么您已经遇到了重大问题。如果它是某种描述的文本文件,您可以使用导入向导将其导入Access,允许其分配ID。这将保留顺序。然后我倾向于运行一点VBA来整理。数据以文本文件的形式提交给我们。我是一个访问人,因此,对我来说,导入Access最简单,允许它添加一个自动编号ID以保留行顺序,然后在整个记录集中运行一些VBA。我倾向于在导入之前处理文件,并在开头插入行号。这确保了在导入以某种方式丢失源订单时,可以恢复源订单。数据已经在Access中还是在文本文件中?如果它在Access中没有主键,那么您已经遇到了重大问题。如果它是某种描述的文本文件,您可以使用导入向导将其导入Access,允许其分配ID。这将保留顺序。然后我倾向于运行一点VBA来整理。数据以文本文件的形式提交给我们。我是一个访问人,因此,对我来说,导入Access最简单,允许它添加一个自动编号ID以保留行顺序,然后在整个记录集中运行一些VBA。我倾向于在导入之前处理文件,并在开头插入行号。这可以确保在导入过程中丢失源订单时,可以恢复源订单。因此,当您导入此文件的已处理版本时,记录出现故障?我不是说。我是说,不采用某种插入顺序是安全的。使用Access,可以安全地假定记录以正确的顺序导入,并且分配的ID反映了这一点。您是否有一些引用(超链接或其他内容)?因此,当您导入此文件的已处理版本时,记录坏了?我不是这么说的。我是说,不采用某种插入顺序是安全的。使用Access,可以安全地假定记录以正确的顺序导入,并且分配的ID反映了这一点。您是否有一些引用(超链接或其他内容)?