如何将带有''和''的CSV文件导入SQL Server?

时间:2016-12-08 16:35:29

标签: sql-server sql-insert

将CSV导入SQL Server数据库时出现问题。

我在CSV中有, ,,,个值,所以当我比较SQL中的值时 b.test = a.test(即'' = ' ')是假的。因此插入已完成,但测试具有唯一约束,我在b.test = ''a.test = ' '时获得约束验证。

我该如何解决这个问题?

这里的陈述(b.marke = a.marke是问题):

 INSERT INTO wt_umcodierung_bez(prnummer, marke, spk, bez, quelle) 
 SELECT a.prnummer, a.marke, a.spk, a.bez, a.quelle
 FROM trans_wt_umcodierung_bez a
 WHERE NOT EXISTS (SELECT 1
                  FROM wt_umcodierung_bez b 
                  WHERE b.prnummer = a.prnummer
                    AND b.marke = a.marke
                    AND b.spk = a.spk)
 AND a.spk IN (SELECT spk FROM wt_spk)

1 个答案:

答案 0 :(得分:1)

在这种情况下,您可能希望对字段进行双重修剪,以确保剥离任何前导或尾随空格。如果你遇到NULL值的问题,你可以进一步将a.marke和b.marke包装在ISNULL中,然后LTRIM(RTRIM())

INSERT INTO wt_umcodierung_bez(prnummer, marke, spk, bez, quelle) 
 SELECT a.prnummer, a.marke, a.spk, a.bez, a.quelle
 FROM trans_wt_umcodierung_bez a
 WHERE NOT EXISTS (SELECT 1
                  FROM wt_umcodierung_bez b 
                  WHERE b.prnummer = a.prnummer
                    AND LTRIM(RTRIM(b.marke)) = LTRIM(RTRIM(a.marke))
                    AND b.spk = a.spk)
 AND a.spk IN (SELECT spk FROM wt_spk)