if not exists in sql

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

showing results for - "if not exists in sql"
Santino
13 Feb 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
sql not exist vs not innot exist statemn in sqlexists not exists sqldoes not exist in sqlsql check not exists tableif not exists sql serverexists and not exists in sqlms sql if not existssql not exists conditionnot exists in sql server in where not in and not exists sqlsql where not presentcan we use where with not exists in sqlsql if not existif not exists table in sql serversql query select if not existssql and not existssql server if not existnot exists in where in sqlms sql server if not existswhere not exist sqlhow to use not exists in sqlif not exists mssqlsql where not exist in table 27not exists 27 sqlselect where not exists sqlnot in sql serversql not exists queryusing not exists operator in sqlmssql if not existswhere not exists sql serverexist not exist in sqlmssql where not existsif not exists table sqlif not exists in sql examplenot exist sqlnot exists sql errorsql if not exist tablenot exist sql serversql difference between not exists and not insql and not exists examplecreate if not exist in sqlchecking not exists condition in sqlsql not existsql check if not existsusing where not exists in sqlif not exists sqlnot exists 2c sqlnot exists vs not in sqladd if not exist sqlif not exists in sqlwhere not exists sqldoes not exist in sqlsql where not exists or exceptif id not exists sqlsql exists not existswhere does not exist sqlsql does not existsql if not exists not workingexist and not exists in sql serversql if not exists valuesql select not existsif not exist in sqlsql check if value does not exist in tableexist or not exists in sqlsql server if not exists not workingtable if not exists in sql serverif not exists in sql 2b using coundif not exists in sql serverif not exists sql databasereturn if not exists sqlif not exist sql serversql not existssql if not exists exceptionsql server not existsnot exist in sql querysql creat if not existsnot exist in sqlselect where not exists in sqlsql if value does not existsql check if value not exists in tablenot existe sqldoes not exist or existssql not exitssql is not existuse not exits sqlinserrt when doesnot exist sqlcheck if not exists in sqlsql select where not existssql if exists not workingsql not in vs not existsnot exists en sqlsql not existssql 22not exists 22exists not exists in sqlif entry does not exist sqlnot exists sql serverwhere not exists in sqlsql if table not existssql not exists exampleif exists syntax in sql errorcheck if value exists or not sqlnot exists in sqlhow to use where not exists in sqlif not exist sqlhow to use if not exists in sql servernot exists sql selectsql server where not existsget if not exists sqlcan we use where and not exists in sqlnot exists in sql serverif does not exist sqlif not exists sql server examplenot exists sqlif not exist else sqlsql server if not existssql where not existssql if not exists tableif not exists sql statementsql not exists notselect not exists sqlsql if exists vs if not existshow to use sql not existsusing not exists in sqlsql if id not existsif table does not exists sqlsql server if not exists selectsql not exists selectif data does not exists in sql then do a thingsql select if not existssql not in and not existssql server not in or not existssql query if not existsms sql table exists or notsql server if does not existsis not exist sqlsql where if not exists sql add if not existif not exist table in sqldoes not exist sqlwhen not exists sqlwhat does if not exists in sqland not exists sqlsql where not exists 5dsql existss vs not existsif table not exists sqlsql not exists vs not insql values not existsql if not exists selectexample not exists in sql serveruse not exists in sqlsql server not exists in selectadd if not exists sqlif doesn 27t exist sqlt sql if not existssql where exists not workingif not exists select sql serverif exists sql notnot exists in mssqlif not exists in sql altersql if not existsnot in sql not existsexist and not exists in sqlnot exists operator in sqlsql query not existsor not exists sqlif not exists in sql