sql server if not exist

Solutions on MaxInterview for sql server if not exist by the best coders in the world

showing results for - "sql server if not exist"
Norah
31 Sep 2019
1At first glance your original attempt seems pretty close. I'm assuming that clockDate is a DateTime fields so try this:
2
3IF (NOT EXISTS(SELECT * FROM Clock WHERE cast(clockDate as date) = '08/10/2012') 
4    AND userName = 'test') 
5BEGIN 
6    INSERT INTO Clock(clockDate, userName, breakOut) 
7    VALUES(GetDate(), 'test', GetDate()) 
8END 
9ELSE 
10BEGIN 
11    UPDATE Clock 
12    SET breakOut = GetDate()
13    WHERE Cast(clockDate AS Date) = '08/10/2012' AND userName = 'test'
14END 
15Note that getdate gives you the current date. If you are trying to compare to a date (without the time) you need to cast or the time element will cause the compare to fail.
16
17If clockDate is NOT datetime field (just date), then the SQL engine will do it for you - no need to cast on a set/insert statement.
18
19IF (NOT EXISTS(SELECT * FROM Clock WHERE clockDate = '08/10/2012') 
20    AND userName = 'test') 
21BEGIN 
22    INSERT INTO Clock(clockDate, userName, breakOut) 
23    VALUES(GetDate(), 'test', GetDate()) 
24END 
25ELSE 
26BEGIN 
27    UPDATE Clock 
28    SET breakOut = GetDate()
29    WHERE clockDate = '08/10/2012' AND userName = 'test'
30END 
31As others have pointed out, the merge statement is another way to tackle this same logic. However, in some cases, especially with large data sets, the merge statement can be prohibitively slow, causing a lot of tran log activity. So knowing how to logic it out as shown above is still a valid technique.
similar questions
if exists sql server
queries leading to this page
if not exist sqlsql select if not existssql if exists not workingif not exists mssqladd if not exists sqlsql not exists queryif not exists in sql alterchecking not exists condition in sqlsql if not exists selectsql where if not exists sql not exists vs not insql if not exists exceptionor where not exists sqlselect where not exists sqlsql check not exists tableif not exists sql serverms sql table exists or notcan we use where with not exists in sqlhow to use not exists in sqltable if not exists in sql serverdoes not exist in sqlsql if exists vs if not existsnot exist in sqlwhere not exists syntax sqlsql server not in or not existsnot exists in sqlsql and not exists exampleif not exist sql serversql server if not exists sql serverinserrt when doesnot exist sqluse not exists in sqlif not exists sql statementwhere does not exist sqlt sql if not existssql creat if not existscan we use where and not exists in sqlmssql where not existssql difference between not exists and not insql check if not existssql where not exists or exceptnot exists sql errorsql if not existssql if not existsql 22not exists 22sql server if not exists not workingnot exists operator in sqlexist and not exists in sqlnot exists sql serversql check if value does not exist in tablereturn if not exists sqlsql if not exists tableif entry does not exist sqlnot in and not exists sqlif table not exists in sql serversql query not existsif not exists select sql serversql if not exists valuenot exists in sql serverwhat does if not exists in sqlsql select where not existsexist and not exists in sql serverwhen not exists sqlif not exists sql databasehow to use where not exists in sqlsql if not exists not workingnot exists sqlsql not in vs not existsif not exists sql server examplesql add if not existif not exists table sqlnot exists en sqlsql and not existssql select not existsis not exist sqlsql not exists notdoes not exist sqlwhere not exists sql serversql values not existif not existsuse not exits sqlusing where not exists in sqlusing not exists operator in sqldoes not exist or existssql check if value not exists in tablesql server not exists in selectsql query if not existsand not exists sqlif does not exist sqlsql server if not existsnot exists in sql server in where not exist sqlexists not exists in sqlnot exist sql servernot exists vs not in sqlnot existe sqlsql not in and not existssql not existssql if not exists 0example not exists in sql serverif not exists in sqlhow to use sql not existssql if id not existssql existss vs not existssql update if not existssql query select if not existswhere not exists sqlsql if value does not existsql not exist vs not insql not exitsif not exists sql 27not exists 27 sqlnot exist in sql querysql where not exists 5dsql not exists selectif not exist in sqlusing not exists in sqlsql where not presentexists and not exists in sqlif not exist table in sqlif table does not exists sqlif table not exists sqlwhere not exists orsql exists not existsnot in sql not existsnot exists in mssqlcreate if not exist in sqlif exists syntax in sql errornot exists in where in sqlsql server and not existssql not exists examplecheck if value exists or not sqlsql not existsql if table not existsnot exist statemn in sqlwhere not exists in sqldoes not exist in sqlsql not exists conditionselect not exists sqlif data does not exists in sql then do a thingsql is not existsql where not exist in tablesql does not existif exists sql notnot in sql serverif not exists in sql 2b using coundms sql if not existscheck if not exists in sqlselect where not exists in sqlsql server not existsif not exist else sqlget if not exists sqlmssql if not existssql not existsexist not exist in sqlexist or not exists in sqlif not exists table in sql serversql server if not existsql server if not exists selectsql if not exist tablenot exists sql selectif not exists in sql exampleor not exists sqlif doesn 27t exist sqlmicrosoft sql if not existswhere not exist sqlif not exists in sql serversql where exists not workingnot exists 2c sqlsql server if does not existsadd if not exist sqlhow to use if not exists in sql serverexists not exists sqlsql where not existssql server where not existsif id not exists sqlms sql server if not existssql server if not exist