Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am a not much aware of sql so please help me ,I need to avoid duplicate records while inserting a table in sql



Here is my code below



ALTER procedure [dbo].[sp_InsertBankName]
(
@bankname as varchar(50)
,@banknamea as nvarchar(50)
)
as
begin
declare @bankid as varchar(10)
select @bankid=isnull(max(convert(int,bankid)),0)+1 from bankname
insert into bankname
(bankid,bankname,banknamea)
values (@bankid,@bankname,@banknamea)
end
Posted

In Table bankname ,
set BankId as PrimaryKey And AutoIncreament

then write query...
SQL
ALTER procedure [dbo].[sp_InsertBankName]
 (
 @bankname as varchar(50)
 ,@banknamea as nvarchar(50)
 )
 as
 begin
     insert into bankname (bankname,banknamea)
                    values(@bankname,@banknamea)
 end

Happy Coding!
:)
 
Share this answer
 
SQL
ALTER procedure [dbo].[sp_InsertBankName]
(
@bankname as varchar(50)
,@banknamea as nvarchar(50)
)
as
begin
IF EXISTS(SELECT bankid FROM bankname WHERE BankName=bankname )
AS BEGIN
<return any="" message="">
END
ELSE
BEGIN
declare @bankid as varchar(10)
select @bankid=isnull(max(convert(int,bankid)),0)+1 from bankname
insert into bankname
(bankid,bankname,banknamea)
values (@bankid,@bankname,@banknamea)
END
end</return>
 
Share this answer
 
Comments
ManojMurali 2-May-13 6:03am    
While i tried the above code its getting error

Msg 195, Level 15, State 10, Procedure sp_InsertBankName, Line 14
'message' is not a recognized built-in function name.
u can try something similar to this

SQL
DECLARE @Count int

	SELECT @Count = Count(AutoID) FROM UserTable WHERE UserName = @userName

	IF @Count = 0

		BEGIN

			INSERT INTO UserTable (UserName) VALUES (@userName)

		END
 
Share this answer
 
Comments
ManojMurali 2-May-13 6:20am    
Its working ok but how to show message that it already exists
[no name] 2-May-13 6:23am    
That u have to do through your c#/vb.net coding..and if my solutions worked please accept that for future reference..
Thanks
ManojMurali 2-May-13 6:31am    
Thanks Dude @
[no name] 2-May-13 6:37am    
u r welcome..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900