I want to ask this question.

I have a query which I have written to insert if the row doesn't exist in sql server.

insert into Attributes (Id, Disabled, AttributeValue) 
values (@id, @disabled, @attr_value)
if not exists
(
select * from Attributes
where 
Id = @id
)
  1. Not sure, if this is a valid query.
  2. I have seen people use where not exists. What is the difference and how to use where not exists? When I put "where not exists", it is saying "Incorrect syntax near where."
share|improve this question
1  
For one thing, you shouldn't be formatting a list as a code block. –  BoltClock's a Unicorn Mar 28 '12 at 15:58
    
Either stackoverflow or Database Administrators probably but it's a duplicate: Only inserting a row if it's not already there –  Martin Smith Mar 28 '12 at 15:59
add comment

1 Answer

up vote 0 down vote accepted

We have http://dba.stackexchange.com for Database Administrators. It's FAQ states that;

If you have a question about...

  1. Database Administration including configuration and backup/restore
  2. Advanced Querying including window-functions dynamic-sql and query-performance
  3. Data Modelling and database-design including referential-integrity
  4. Advanced Programming in built-in server-side languages including stored-procedures and triggers.
  5. Data Warehousing and Business Intelligence including etl reporting and olap

... then you're in the right place to ask your question!

but, dba.se is not the right place to ask questions about...

  1. Client-side programming - ask on Stack Overflow
  2. Basic SQL - ask on Stack Overflow
  3. Career advice, including salary or résumé - try Patrick McKenzie's Don't call yourself a programmer

So ultimately, your question should be posted on StackOverflow.

share|improve this answer
    
This could be on topic for Database Administrators if the OP wanted an answer addressing concurrency issues but seems that probably they are just asking about syntax... –  Martin Smith Mar 28 '12 at 16:02
    
Not relevant to question but, Career advice, including salary or résumé - try Patrick McKenzie's Don't call yourself a programmer - means it should be also posted on Stackoverflow right? –  user179895 Mar 28 '12 at 16:03
    
@tech_learner: No, see kalzumeus.com/2011/10/28/dont-call-yourself-a-programmer –  Matt Mar 28 '12 at 16:04
    
@MartinSmith: Indeed, but given the snippet of the question posted in the OP, I'd classify it as a "basic SQL" question. –  Matt Mar 28 '12 at 16:05
1  
@tech: No, career advice is off-topic for Stack Overflow. –  Cody Gray Mar 28 '12 at 16:06
add comment

You must log in to answer this question.