SQL Query- Check if exist then insert

I am using SQL Server and I have a table names Campaign that contains (AccountID, campaign, date, cost, impressions, clicks) and I would like to write a query that check through the table records if they found date=@Date then update the record if didn't find the date then insert as new record.

any help
Thank you
Rawan JambiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vikas GargBusiness Intelligence DeveloperCommented:
HI,

You can try this

Update Campaign
set column = value
where date = @date

INSERT INTO COMPAIGN

SELECT * FROM TABLE WHERE DATE NOT IN (SELECT DATE FROM CAMPAIGN)
AND DATE = @DATE
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Vikas gave a good solution but he's assuming that the data are coming from a table. Is that correct?
If so and if the table have plenty of records, than you may want to execute the update and insert in a transaction so it's be protected against any data change during the process.

Cheers
0
Vikas GargBusiness Intelligence DeveloperCommented:
@Vitor,

It is not clear from the post that from where the data will come and the above was a method by using which the Asker can achieve his goal
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.