Sql 如何从本地文本文件向单个空列添加值

Sql 如何从本地文本文件向单个空列添加值,sql,sql-server,null,views,sql-insert,Sql,Sql Server,Null,Views,Sql Insert,我用客户ID、公司名称、地址、电话给客户打过电话 现在,我们插入了一个名为“备注”的新列,该列为空或null 我有一个文本文件要使用带有以下代码的视图remarkiinsert批量插入到列中 bulk insert HRRegion.dbo.Remarksinsert From 'C:\Users\SMSTECHLNG50\Documents\remarks..txt' with ( FIELDTERMINATOR = ',', ROWTERMINATOR = '\n' ) GO

我用客户ID、公司名称、地址、电话给客户打过电话

现在,我们插入了一个名为“备注”的新列,该列为空或null

我有一个文本文件要使用带有以下代码的视图remarkiinsert批量插入到列中

bulk insert  HRRegion.dbo.Remarksinsert
 From 'C:\Users\SMSTECHLNG50\Documents\remarks..txt'
 with 
 (
 FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
但这是一个错误

Msg 515,16级,状态2,第9行不能将值NULL插入 列'CustomerID',表'HRRegion.dbo.Customers';列不存在 允许空值。插入失败。声明已终止


我认为在这里,你只能插入一整行或者什么都不插入

如果您的客户表如下所示:

customer
custid|co_name        |addr                     |phone            |remarks
    42|Laverda        |Breganze, Italy          |+39 6 233 84 81  |(NULL)
    43|Benelli        |Pesaro, Italy            |+39 8 284 55 32  |(NULL)
    44|Ural           |Irbit, Russia            |+7 14 526 22 2342|(NULL)
    45|Dnepr          |Kiew, Ukraine            |+380 526 22 2342 |(NULL)
    46|Harley Davidson|Milwaukee, US            |+1 802 223 4444  |(NULL)
    47|Honda          |Tokyo, Japan             |+81 82 555 4123  |(NULL)
    48|Moto Guzzi     |Mandello del Lario, Italy|+39 6 423 04 72  |(NULL)
    49|Ducati         |Bologna, Italy           |+39 7 722 04 43  |(NULL)
    50|Norton         |Birmingham, UK           |+44 7234 723 4423|(NULL)
    51|Matchless      |Plumstead, London, UK    |+44 8021 612 0843|(NULL)
    52|Brough         |Nottingham, UK           |+44 5812 512 4883|(NULL)
remarks
custid|remarks
    42|built also tractors, closed now
    43|first series 6-cylinder motorbike
    44|old style sidecar rigs with modern engine
    45|old style sidecar rigs, permanent two-wheel drive
    46|the american classic
    47|builders of the CB 750 four and the gold wing
    48|famous for horizontal singles and 90° V twins
    49|90° V twin bikes with lateral crankshaft
    50|english classic, still alive
    51|english classic, closed now
    52|probably the finest motorcycles ever built
(好吧,您使用
ALTER TABLE
…)添加
备注
列),然后,我希望您提到的带有备注的文件看起来像这样:

customer
custid|co_name        |addr                     |phone            |remarks
    42|Laverda        |Breganze, Italy          |+39 6 233 84 81  |(NULL)
    43|Benelli        |Pesaro, Italy            |+39 8 284 55 32  |(NULL)
    44|Ural           |Irbit, Russia            |+7 14 526 22 2342|(NULL)
    45|Dnepr          |Kiew, Ukraine            |+380 526 22 2342 |(NULL)
    46|Harley Davidson|Milwaukee, US            |+1 802 223 4444  |(NULL)
    47|Honda          |Tokyo, Japan             |+81 82 555 4123  |(NULL)
    48|Moto Guzzi     |Mandello del Lario, Italy|+39 6 423 04 72  |(NULL)
    49|Ducati         |Bologna, Italy           |+39 7 722 04 43  |(NULL)
    50|Norton         |Birmingham, UK           |+44 7234 723 4423|(NULL)
    51|Matchless      |Plumstead, London, UK    |+44 8021 612 0843|(NULL)
    52|Brough         |Nottingham, UK           |+44 5812 512 4883|(NULL)
remarks
custid|remarks
    42|built also tractors, closed now
    43|first series 6-cylinder motorbike
    44|old style sidecar rigs with modern engine
    45|old style sidecar rigs, permanent two-wheel drive
    46|the american classic
    47|builders of the CB 750 four and the gold wing
    48|famous for horizontal singles and 90° V twins
    49|90° V twin bikes with lateral crankshaft
    50|english classic, still alive
    51|english classic, closed now
    52|probably the finest motorcycles ever built
因此,您将构建一个
备注\u stg
表:

CREATE TABLE remarks_stg (
  custid  SMALLINT    NOT NULL
, remarks VARCHAR(50) NOT NULL
);
MERGE customer t
USING stg_customer s
  ON t.custid = s. custid
WHEN MATCHED THEN UPDATE SET
  remarks = s.remarks
;
然后,如上所述,只加载带有数据文件的暂存表,并且,至少如果有SQL Server 2008及更高版本,则使用
MERGE
语句更新
customer
表:

CREATE TABLE remarks_stg (
  custid  SMALLINT    NOT NULL
, remarks VARCHAR(50) NOT NULL
);
MERGE customer t
USING stg_customer s
  ON t.custid = s. custid
WHEN MATCHED THEN UPDATE SET
  remarks = s.remarks
;

检查列“CustomerID”是否声明为主键或非空列…似乎您试图在表中插入新行,而不是更新现有行。CustomerID是主键,但我只想在表中添加备注列谢谢。。我已从文本文件导入,现在需要将主键值复制到备注中的另一列。您必须将主键添加到文本文件中。SQL表没有排序,备注也不知道去哪里…CustomerID是customers表中的prmary键,现在我需要将它们导入到我答案中示例中的备注中。您将看到,不可能找出目标表的哪个主键必须转到暂存表中的哪一行。无法将主键导入临时表,句点。如果幸运的话,目标表的主键是排序的和连续的,如我的示例所示。然后,您可以使用perl脚本或awk脚本,甚至excel,在加载文件的每行左侧添加42到52的序列。如果没有,您将不得不手动执行此操作。没有其他方法。为什么它只为目标中的所有行更新一个customerID。。我的意思是,我可以从源列复制前49行并将其粘贴到目标列吗