Home > OS >  replacing TSQL NOT EXISTS in SQL-92
replacing TSQL NOT EXISTS in SQL-92

Time:10-24

I am have following code which works well in TSQL:

BEGIN 
IF NOT EXISTS (select * from tblDCUSTOM where id = 'All Customers')
    BEGIN
    INSERT INTO tblDCUSTOM
        (ID
    ,Name
    ,English     
   )
   SELECT 'All Customers','All Customers','All Customers'
   END
END

Now, I need to have this functionality in an custom environment, where SQL-92 is used - so no EXISTS (edit: not true, EXISTS works in SQL-92) or BEGIN-END is possible. Any Ideas?

CodePudding user response:

This is the correct answer, the 'EXISTS' statement IS actually supported:

Put the condition in the WHERE: INSERT ... SELECT ... WHERE NOT EXISTS (...). This is arguably better practice even in T-SQL, to make the operation atomic.

CodePudding user response:

As per very first comment;

   INSERT INTO tblDCUSTOM
        (ID
    ,Name
    ,English     
   )
   SELECT 'All Customers','All Customers','All Customers'
   WHERE (SELECT COUNT(*) FROM tblDCUSTOM where id = 'All Customers') >= 1

If TOP is supported this might be better

   INSERT INTO tblDCUSTOM
        (ID
    ,Name
    ,English     
   )
   SELECT 'All Customers','All Customers','All Customers'
   WHERE (SELECT TOP 1 1 as F FROM tblDCUSTOM where id = 'All Customers') IS NOT NULL

I must warn you, many have tried to make a 'database agnostic' system. It's not worth it.

  • Related