Search
 
SCRIPT & CODE EXAMPLE
 

SQL

SQL NOT EXISTS

SELECT customer_id, first_name
FROM Customers
WHERE NOT EXISTS (
  SELECT order_id
  FROM Orders
  WHERE Orders.customer_id = Customers.customer_id
);
Comment

if not exists in sql

At first glance your original attempt seems pretty close. I'm assuming that clockDate is a DateTime fields so try this:

IF (NOT EXISTS(SELECT * FROM Clock WHERE cast(clockDate as date) = '08/10/2012') 
    AND userName = 'test') 
BEGIN 
    INSERT INTO Clock(clockDate, userName, breakOut) 
    VALUES(GetDate(), 'test', GetDate()) 
END 
ELSE 
BEGIN 
    UPDATE Clock 
    SET breakOut = GetDate()
    WHERE Cast(clockDate AS Date) = '08/10/2012' AND userName = 'test'
END 
Note 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.

If 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.

IF (NOT EXISTS(SELECT * FROM Clock WHERE clockDate = '08/10/2012') 
    AND userName = 'test') 
BEGIN 
    INSERT INTO Clock(clockDate, userName, breakOut) 
    VALUES(GetDate(), 'test', GetDate()) 
END 
ELSE 
BEGIN 
    UPDATE Clock 
    SET breakOut = GetDate()
    WHERE clockDate = '08/10/2012' AND userName = 'test'
END 
As 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.
Comment

sql select if not exists

BEGIN
   IF NOT EXISTS (SELECT * FROM EmailsRecebidos 
                   WHERE De = @_DE
                   AND Assunto = @_ASSUNTO
                   AND Data = @_DATA)
   BEGIN
       INSERT INTO EmailsRecebidos (De, Assunto, Data)
       VALUES (@_DE, @_ASSUNTO, @_DATA)
   END
END
Comment

sql not exists

-- The SQL NOT EXISTS Operator will act quite opposite to EXISTS Operator. 
-- It is used to restrict the number of rows returned by the SELECT Statement.

SELECT column_name
FROM table_name
WHERE NOT EXISTS (Write Subquery to Check);
Comment

PREVIOUS NEXT
Code Example
Sql :: mysql disable logging 
Sql :: what is having clause in sql 
Sql :: ms sql mac 
Sql :: multiple replace value mssql 
Sql :: print hello world in sql 
Sql :: exec procedure pl sql 
Sql :: postgresql conectar 
Sql :: add foreign key to existing table postgres 
Sql :: add column table pl sql 
Sql :: not regexp_like in oracle 
Sql :: mac django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient? 
Sql :: sum mysql 
Sql :: table structure in sql 
Sql :: select into 
Sql :: osm2pgsql mac 
Sql :: pgadmin see indexes 
Sql :: oracle chain rules 
Sql :: get all employee of salary if more than in sql 
Sql :: sql restore database from backup 
Sql :: sql duplicate a table with data 
Sql :: set mysql password 
Sql :: sql view index 
Sql :: sql pivot rows to columns 
Sql :: finish transaction sql 
Sql :: change schema of all tables postgres 
Sql :: set column width in sqlplus 
Sql :: java sql insert return id 
Sql :: sql unique 
Sql :: creating sql table 
Sql :: get size of mysql database 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =