SQLTeam.com | Weblogs | Forums

Insert unique record into the table

DML
declare @temp table
(
rowid int,
name nvarchar(100),
code int
)

insert into @temp values(1,'sony',100)
insert into @temp values(2,'LGI',101)

if try to insert or update the below scenario it should not insert or update
name = 'sony' and code = 101
name = 'LGI' and code = 100

can any one please try to help me.

this is similar to your other post. What are you trying to do? What is the primary key for the table?

create a unique constraint on Name Column

@mike01 rowid is primary key

@harishgg1 am expecting query

what if we wanted to add this row

name = 'LGI' and code = 666

Hi

What do you mean by query

Query to create constraint

It should not allow since LGI is already in inserted

SQL Script

Ok sure
Please give me some time

Sample script .. modify

ALTER TABLE
Customer
ADD CONSTRAINT
U_Name UNIQUE(Name,Age)

maybe this might help?

;with src
as
(
 
select rowid = 3, name = 'sony', code = 101 union
select rowid = 4, name = 'LGI', code = 100 union
select rowid = 5, name = 'LGI' , code = 666
)
insert into @temp
select * 
  from src
  where not exists(select 1 from @temp tgt where tgt.name = src.name)
    and not exists(select 1 from @temp tgt where tgt.code = src.code)

select * from @temp