Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

How to use a single query for sql update or insert?

Asked by Dadhija Raj Sep 29, 2022 898 views 1 answer
Share

About this question

 I have a table test having columns id which primary key and auto incremented and name. I want to insert a new record if and only if there are no records. For example

input is id=30122 and name =john

if there are records with id 30122 then I have updated the name column to john,if there are no records then I have inserted a new record.

I can do using 2 queries like

select * from test where id=30122

if it has some records then I can use update test set name='john' where id=3012

or if it does not have records then I can use

insert into test(name) values('john')

But I wanted to use a single query?

Can somebody tell if it's possible?

Your answer

1 Answer

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.