求一条sql语句,
insert table1 (username,pwd)values ( 'myname ', 'mypwd ')
当table1里存在username=myname,pwd=mypwd记录时就不插入
应该怎么写?
------解决方案--------------------insert table1 (username,pwd)values ( 'myname ', 'mypwd ')
当table1里存在username=myname,pwd=mypwd记录时就不插入
-----------------
if exists (select * from table1 where username=myname and pwd=mypwd)
print '存在 '
else insert table1 (username,pwd)values ( 'myname ', 'mypwd ')
------解决方案--------------------用存储过程吧
CREATE PRO SP_INSERT
@USERNAME VARCHAR(200),
@PWD VARCHAR(200)
AS
IF(@USERNAME <> 'myname ' OR @PWD <> 'mypwd ' )
BEGIN
insert table1 (username,pwd)values (@USERNAME,@PWD)
END
------解决方案--------------------insert table1 (username,pwd)
select top 1 'myname ', 'mypwd '
from table1
where colname <> 'myname ' and colpassword <> 'mypwd '
------解决方案--------------------最简单的是用这两个字段做联合主键
------解决方案--------------------写触发器来约束
------解决方案--------------------当table1里存在username=myname,pwd=mypwd记录时就不插入
----------------------------------------
如果myname、mypwd是变量的话,一条语句肯定不行
if not exists (select * from table1 where username= 'myname ' and pwd= 'mypwd ')
insert into table1 (username,pwd)values ( 'myname ', 'mypwd ')