如何防止重复添加数据
1个回答
展开全部
可用索引选项(IGNORE_DUP_KEY)过滤而不抛出错误
GO
CREATE TABLE #Test (C1 INT);
GO
CREATE UNIQUE INDEX AK_Index ON #Test (C1)
WITH (IGNORE_DUP_KEY = ON);
GO
INSERT INTO #Test VALUES (1);
INSERT INTO #Test VALUES (1); --已忽略重复的键。
GO
SELECT COUNT(*)AS [Number of rows] FROM #Test;
GO
DROP TABLE #Test;
GO
/*
1
*/
GO
CREATE TABLE #Test (C1 INT);
GO
CREATE UNIQUE INDEX AK_Index ON #Test (C1)
WITH (IGNORE_DUP_KEY = ON);
GO
INSERT INTO #Test VALUES (1);
INSERT INTO #Test VALUES (1); --已忽略重复的键。
GO
SELECT COUNT(*)AS [Number of rows] FROM #Test;
GO
DROP TABLE #Test;
GO
/*
1
*/
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询