0

Guys i have to do a insert in to a table when there is no record found. is it possible to do it with only sql query. i tried with case and if .

1
  • Yup, I can confirm, it is possible and easy. Show us the source of what you have tried so far? Commented Nov 29, 2010 at 13:12

2 Answers 2

3

Just add the condition of your 'IF' to the WHERE of the INSERT for example

INSERT INTO People
(FirstName,LastName,Email)
SELECT @FirstName,@LastName,@Email WHERE @Email NOT IN (SELECT Email FROM People)

If the email address already exists in the table then the SELECT will return no rows and hence no insert will take place

Sign up to request clarification or add additional context in comments.

Comments

1

How about:

insert into table1 
   (select * from table2 where id not in
      (select id from table1))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.