IF (SELECT Count(*) FROM Table WHERE title='@'and tle='@') >0 应该是这样子的,数据库我比较差
you can test "Having"such as select title,count (title) from Table group by title having ( count(title)>1)
用当前插入的数据的相应字段查询一下,select count(*) from table where title=插入的值 and tel=插入的值,如果返回大于0,说明已经存在重复;如果等于0,说明不存在重复。用ado.net可以,也可以用sql写(这时就是返回大于1说明重复),不过sql的我不会写。
两种思路,不知道能不能帮助你:① 就像 4 楼说的那样没插入一条数据的时候做判断, IF (SELECT Count(1) FROM Table WHERE title='插入的值' and Name='插入的值' ')>0) begin update Table set datetimes="新值" end else begin insert into table values() end 这样就必须做类似的存储过程,用变量接收单条插入的信息,然后循环插入每一条。②既然是批量插入,那把新的数据先插入一个新表(这里可以是临时表)newTable,然后再从newTable中更新原表oldTable的数据就行了 我这里模拟字段 id,Title,ProName,CreateDate---更新相同数据的时间 update oldTable set CreateDate=A.CreateDate from newTable A,oldTable B where B.Title=A.Title and B.ProName=A.ProName ---插入不同数据的记录 insert into oldTable(Title,ProName,CreateDate) select Title,ProName,CreateDate from newTable A where not exists(select id from oldTable where oldTable.Title= A.Title and oldTable.ProName=A.ProName)---这里必须先执行更新 再执行插入
如果是大量数据建议使用临时表 1)先将数据存储到临时表中 2)调用批量更新语句CREATE TABLE Test1(A INT PRIMARY KEY, B NVARCHAR(10)); INSERT Test1 SELECT 1, '11' UNION ALL SELECT 2, '22' UNION ALL SELECT 3, '33'; CREATE TABLE Test2(A INT PRIMARY KEY, B NVARCHAR(10)); INSERT Test2; SELECT 1, '111' UNION ALL SELECT 4, '444' UNION ALL SELECT 5, '555';SELECT * FROM test1; SELECT * FROM test2;--批量更新 UPDATE test1 SET test1.b = test2.b FROM test1 INNER JOIN test2 on test1.a = test2.a; --批量修改 INSERT test1 SELECT test2.* FROM test2 LEFT JOIN test1 ON test2.a = test1.a WHERE test1.a IS null; SELECT * FROM test1; SELECT * FROM test2;drop table test1; drop table test2; 3)……
>0
应该是这样子的,数据库我比较差
select title,count (title)
from Table
group by title
having ( count(title)>1)
IF (SELECT Count(1) FROM Table WHERE title='插入的值' and Name='插入的值' ')>0)
begin
update Table set datetimes="新值"
end
else
begin
insert into table values()
end
这样就必须做类似的存储过程,用变量接收单条插入的信息,然后循环插入每一条。②既然是批量插入,那把新的数据先插入一个新表(这里可以是临时表)newTable,然后再从newTable中更新原表oldTable的数据就行了
我这里模拟字段 id,Title,ProName,CreateDate---更新相同数据的时间
update oldTable set CreateDate=A.CreateDate from newTable A,oldTable B where B.Title=A.Title and B.ProName=A.ProName
---插入不同数据的记录
insert into oldTable(Title,ProName,CreateDate) select Title,ProName,CreateDate from newTable A where not exists(select id from oldTable where oldTable.Title= A.Title and oldTable.ProName=A.ProName)---这里必须先执行更新 再执行插入
1)先将数据存储到临时表中
2)调用批量更新语句CREATE TABLE Test1(A INT PRIMARY KEY, B NVARCHAR(10));
INSERT Test1
SELECT 1, '11' UNION ALL
SELECT 2, '22' UNION ALL
SELECT 3, '33';
CREATE TABLE Test2(A INT PRIMARY KEY, B NVARCHAR(10));
INSERT Test2;
SELECT 1, '111' UNION ALL
SELECT 4, '444' UNION ALL
SELECT 5, '555';SELECT * FROM test1;
SELECT * FROM test2;--批量更新
UPDATE test1
SET test1.b = test2.b
FROM test1 INNER JOIN test2 on test1.a = test2.a;
--批量修改
INSERT test1
SELECT test2.*
FROM test2 LEFT JOIN test1 ON test2.a = test1.a
WHERE test1.a IS null;
SELECT * FROM test1;
SELECT * FROM test2;drop table test1;
drop table test2;
3)……