C#实现 将Excel数据表导入SQL数据库中 然后如果某字段内容重复则不插入那一行 应该如何编写代码
1个回答
展开全部
你可以先导入,并在数据库中创建一个行号列,记录每一行的行号。导入后,再对数据进行处理,设要判断重新的字段为Col1,设行号列为ID1, 设表名为TBL,那么代码如下:
DECLARE @T TABLE( Col1 VARCHAR(1000 ) )
DECLARE @C1 VARCHAR(1000)
INSERT @T( Col1 )
SELECT Col1 FROM TBL GROUP BY Col1 HAVING COUNT(*) > 1
WHILE ((SELECT COUNT(*)FROM @T)> 0 )
BEGIN
SELECT TOP 1 @C1 = Col1 FROM @T1
DELETE @T WHERE Col1 = @C1
DELETE TBL WHERE Col1 = @C1 AND ID1 < ( SELECT MAX( ID1 ) FROM WHERE Col1 = @C1 )
END
DECLARE @T TABLE( Col1 VARCHAR(1000 ) )
DECLARE @C1 VARCHAR(1000)
INSERT @T( Col1 )
SELECT Col1 FROM TBL GROUP BY Col1 HAVING COUNT(*) > 1
WHILE ((SELECT COUNT(*)FROM @T)> 0 )
BEGIN
SELECT TOP 1 @C1 = Col1 FROM @T1
DELETE @T WHERE Col1 = @C1
DELETE TBL WHERE Col1 = @C1 AND ID1 < ( SELECT MAX( ID1 ) FROM WHERE Col1 = @C1 )
END
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询