日期:2014-05-18  浏览次数:20557 次

SQL高手请帮我,100分送
我想实现如下的功能:
每天只能插一条日期记录,但是总是控制不了,总是想增多少就增多少,我都哭了
(不要有异常返回哦)

望高手指点错误,代码如下,并有100分送:
INSERT   AAA(A)  
SELECT   CONVERT(VARCHAR(10),GETDATE(),120)
WHERE   NOT   EXISTS  
(SELECT   0  
FROM   AAA
WHERE   DATEDIFF(DD,A,GETDATE()) <> 0)



------解决方案--------------------
用事務才能控制
------解决方案--------------------
INSERT AAA(A)
SELECT CONVERT(VARCHAR(10),GETDATE(),120)
WHERE NOT EXISTS
(SELECT 1
FROM AAA
WHERE DATEDIFF(day,0,A)=datediff(day,0,getdate()))
------解决方案--------------------
DATEDIFF(DD,A,GETDATE()) <> 0
的大把,昨天的,前天的,都不等于0。

DATEDIFF(day,0,A)=datediff(day,0,getdate())
这个就没有了。
------解决方案--------------------
begin tran

INSERT AAA(A)
SELECT CONVERT(VARCHAR(10),GETDATE(),120)

if exists(select count(A) from AAA where A=convert(varchar(10),getdate(),120)
group by A
having count(*)> 1 )

ROLLBACK TRAN

------解决方案--------------------
--测试
create table TEST(A datetime)
go

insert TEST select getdate() where not exists (select 1 from TEST where datediff(day,0,A)=datediff(day,0,getdate()))
/*

(所影响的行数为 1 行)

*/

insert TEST select getdate() where not exists (select 1 from TEST where datediff(day,0,A)=datediff(day,0,getdate()))
/*

(所影响的行数为 0 行)

*/

--删除测试
drop table TEST