Cari Blog Ini

28 Juni 2012

How To Validate Availability of Table in Database using EXISTS

1.
----------------------------------------------------------------------------------
-----code:start
----------------------------------------------------------------------------------
EXEC
(
    'if EXISTS (
            select * from INFORMATION_SCHEMA.tables
            WHERE table_name = ''Cabang_1''
            )
    SELECT ''-----Table:Cabang_1 is available  '' '
)
----------------------------------------------------------------------------------
-----code:end
----------------------------------------------------------------------------------

Result Messages:
----------------------------------
-----Table:Cabang_1 is available 

(1 row(s) affected)


2.
----------------------------------------------------------------------------------
-----code:start
----------------------------------------------------------------------------------
if EXISTS
    (
        SELECT * FROM INFORMATION_SCHEMA.tables
        WHERE table_name ='Cabang_1'
    )
    SELECT '-----Table:Cabang_1 is available-----'
----------------------------------------------------------------------------------
-----code:end
----------------------------------------------------------------------------------

Result Messages:
-------------------------------------
-----Table:Cabang_1 is available-----

(1 row(s) affected)