Syntactically Better SQL CREATE Statements
Join the DZone community and get the full member experience.Join For Free
debugging etl sql scripts can be frustrating because of the syntax used to create database objects. in this post i share some tips to write syntactically and functionally better sql scripts that are particularly useful when using sql for etl. all syntax written for sybase iq 15.4.
historically to avoid object already exists type errors i used to put a statement such as the one below around most sql code blocks:
ifnotexists(select*from sys.sysobject where objname ="my_object")then do something endif;
as the sql script gets longer it becomes harder to separate actual code implementing the business logic from code used to make sure that your script does not fail. i found that there are smarter ways to write safe code.
the typical code used for creating variables is as follows:
create variable @start_date date;set@start_date = getdate();
the problem with creating variables in this way is that the variable persists for the duration of your session. if you try rerun the sql statement you will get an error because the variable already exists. you need to drop the variable before you can rerun the part of sql code. it is good practice to drop your variables at the end of your script but the code you are testing might be nowhere near the end of your full script.
to test your code your either have to drop your connection to the database and reconnect or run the drop variable statement before running the create variable statement. the following syntax solves all these problems:
createor replace variable @start_date date;
you can run this statement over and over without worrying if a variable already exists and without the need to drop your database session to flush out the variables.
views and stored procedures
the first time you create a view you use the create view statement then changes to the view are done using the alter view statement. to use one statement for creating or altering a view use the following statement:
createor replace view myview as...
a single coded statement can be used to create and update stored procedures.
createor replace procedure my_procedure()beginselect*from mytable end
you only ever need to create a table once so when debugging a script you have to keep dropping the table to test your script. you have to be careful to remove the drop table statement in the script promoted to production. sometimes you can forget to remove the drop statement. instead of using
ifexists(select*from sys.sysobject where objname ="my_object")thendroptable my_table;endif;ifnotexists(select*from sys.sysobject where objname ="my_object")thencreatetable my_table (col1 int, col2);endif;
you only need use one statement.
createtableifnotexists my_table (col1 int, col2);
keeping the syntax simple helps keep the scripts simple which in turn allows you to focus on coding the business logic and not focusing on the syntax. please share your tips for other database servers.
Published at DZone with permission of Mpumelelo Msimanga, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.