我的表格为客户,customerid,companyname,address,phone
现在,我们插入了一个称为备注的新列,该列为空或null
我有文本文件使用以下代码
将视图Indioninsert插入列中bulk insert HRRegion.dbo.Remarksinsert
From 'C:UsersSMSTECHLNG50Documentsremarks..txt'
with
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = 'n'
)
GO
但是它得到错误
msg 515,第16级,状态2,第9行无法将值插入到 列" customerId",table'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)
(好吧,您使用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
;