【问题标题】:How to add values to single empty column from local text file如何从本地文本文件向单个空列添加值
【发布时间】:2017-02-28 08:53:11
【问题描述】:

我有一个名为 customers 的表,其中包含 CustomerID、CompanyName、Address、Phone

现在我们插入了一个名为 Remarks 的新列,该列为空或 null

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

bulk insert  HRRegion.dbo.Remarksinsert
 From 'C:\Users\SMSTECHLNG50\Documents\remarks..txt'
 with 
 (
 FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO

但它得到了错误

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

【问题讨论】:

  • 检查'CustomerID'列是声明为主键还是非空列...
  • 似乎您尝试将INSERT 新行添加到表中,而不是UPDATE 已经存在的行。
  • CustomerID 是主键,但我只想在表中添加备注列

标签: sql sql-server null views sql-insert


【解决方案1】:

我认为也在这里,您只能插入一整行或什么都不插入。

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

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)

(好吧,您使用ALTER TABLE 添加remarks 列...),那么,我希望您提到的带有备注的文件看起来有点像这样:

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

因此,您将构建一个remarks_stg 表:

CREATE TABLE remarks_stg (
  custid  SMALLINT    NOT NULL
, remarks VARCHAR(50) NOT NULL
);

然后,您只需使用上述数据文件加载该暂存表 - 并且,至少如果有 SQL Server 2008 及更高版本,则使用MERGE 语句更新customer 表:

MERGE customer t
USING stg_customer s
  ON t.custid = s. custid
WHEN MATCHED THEN UPDATE SET
  remarks = s.remarks
;

【讨论】:

  • 谢谢 .. 我已经从文本文件中导入,现在我需要将主键值复制到备注_stg 中的另一列。 stackoverflow.com/questions/42546635/…
  • 您必须将主键添加到文本文件中。 SQL表不排序,备注不知道去哪里...
  • CustomerID 是客户表中的主键,现在我需要将它们导入到备注_stg
  • 看我回答中的例子。您将看到不可能找出目标表的哪个主键必须转到暂存表中的哪一行。您不能将主键导入临时表期间。如果幸运的话,目标表的主键是有序且连续的,就像我的示例中一样。然后,您可以使用 perl 脚本或 awk 脚本,甚至 excel,将 42 到 52 的序列添加到备注加载文件的每一行的左侧。如果没有,您将不得不手动执行此操作。没有别的办法。
  • 为什么它只为目标中的所有行更新一个 customerID.. 我的意思是我可以从源列复制前 49 行并将其粘贴到目标列
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多