Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Exists problem

Author  Topic 

shaik.zakeer
Posting Yak Master

117 Posts

Posted - 2008-08-30 : 06:21:32
Hello

I have a table called vehicle maker.

create table vehicle_maker
(
id int identity(1,1),
maker_name varchar(20)
)

data avaliable

id maker_name
1 Hyundai
2 TATA


if i insert again the existing maker_name it should allow to insert.
If its not avaliable in the table it should commit the transaction.

When ever someone is inserting values in this table how can i do this validation.

How can i check whether the record is avaliable or not.

thanks in advance

Thanks

Zakeer Sk

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-30 : 09:57:17
Use a trigger or set a unique constraint on maker_name. the trigger will be a INSTEAD OF INSERT trigger like
CREATE TRIGGER CheckValue ON [vehicle maker]
INSTEAD OF INSERT
AS
IF NOT EXISTS(SELECT 1 FROM INSERTED i
INNER JOIN [vehicle maker] v
ON v.maker_name=i.maker_name)
BEGIN
INSERT INTO vehicle_maker(id,maker_name)
SELECT id,maker_name
FROM INSERTED
END
Go to Top of Page

shaik.zakeer
Posting Yak Master

117 Posts

Posted - 2008-09-01 : 08:13:36
Thanks

Thanks

Zakeer Sk

Go to Top of Page
   

- Advertisement -