Quantcast
Channel: SCN: Message List - SAP Adaptive Server Enterprise (SAP ASE) for Custom Applications
Viewing all articles
Browse latest Browse all 3587

Re: if exists not evaluated correctly

$
0
0

The entire batch of SQL is parsed/compiled before it is actually executed.  Part of the compilation process includes validating that the various commands can complete successfully if they are eventually performed during the execution phase.

 

The first time you run the script the 'create table' is parsed/compiled and confirmed as a valid statement ... because the table does not exist, ie, if the 'create table' is executed then it should succeed.

 

The second time you run the script the 'create table' is parsed/compiled and found to be an invalid statement ... because the table already exists, ie, if the 'create table' is executed then it will fail.

 

The easiest way to get this type of coding to  parse/compile/execute successfully each time it's run is to wrap the 'create table' command in an execute immediate construct, eg:

 

if exists (select name from tempdb..sysobjects where name = 'SSt_stats_purge')

     print 'table exists'

else

     exec("create table tempdb..SSt_stats_purge(scan_date datetime, value int)")

 

---------------

 

The contents of the exec() call are parsed/compiled/validated if/when the exec() is actually executed. Soooo ...

 

- the first time your SQL is run the exec() will be executed, at which point the 'create table' will be parsed/compiled/validated/executed

 

- follow-on runs of the SQL will see the exec() ignored, so the 'create table' will never be parsed/compiled/validated


Viewing all articles
Browse latest Browse all 3587

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>