Run PLSQL external script with Liquibase - sql

im using Liquibase to manage my database in SQLdeveloper. Now, we have a PLSQL script that runs fine by itself. It also makes the changes in the database. Now, if we want Liquibase to run this PLSQL script it gets the following error: ORA-00922: missing or invalid option.
Is it even possible for Liquibase to run external PLSQL scripts?

There are two potential issues you are running into:
You are using <sqlFile> but not specifying splitStatements="false". By default, Liquibase will split the SQL on semicolons because that is what the JDBC driver needs normally, but in a PL/SQL script you can have a single CREATE PROCEDURE or something similar which contains semicolons but is still one statement. You can also use <createProcedure> instead of sqlFile which does not split statements.
Your script has sqlplus-specific functionality in it. SQLPlus and SQLDeveloper do not simply pass the SQL strings straight through to the database, they have their own functionality to sometimes modify what is actually executed. JDBC and therefore Liquibase does not have all the same functionality and so if you are using it in your scripts they will not work. If this is the case, the best approach is to use a changeSet with <executeCommand> to make a call to sqldeveloper or sqlplus.

Related

How to log values of variables during a sql script execution

I have a complex sql script which I am running using db2 +v -txf sqls/connection.sql This script is part of a unix service which is running lot of other scripts as well. The script is querying a temporary session table so I cannot run the script manually (since the table is gone by that time). I want to be able to run the script as part of the service but would like to log the values of variables being calculated in the sql file. For eg: The sql script as the following line timestampdiff(1, char(max(END_TS) - min(START_TS))) as ELAPSED_TIME, I would like to know the values of END_TS and START_TS.
What I have tried:
I tried adding -v to the the db2 command and it printed the entire sql being executed but not the exact values.
If the Db2-server runs on Linux/unix/windows, you can use set serveroutput on; along with call dbms_output.PUT_LINE('......'); which lets you see what you logged as output when the script ends.
dbms_output.put_line docs.
The dbms_output module contains other useful services, and people familiar with Oracle will recognize it.
If the Db2-server runs on a Z/OS or i-series you should tag your question as db2-zos or db2-400 , because the answer can depend on the platform.

Using an Oracle SQL*Plus file with an Oracle Database [Intellij]

I am trying to run many SQL scripts that are contained within a few directories for an Oracle database. My approach was to use an Oracle SQL*Plus file that could simply run each file (or directory if possible) at once. However, when I run the SQLPlus file using the default intellij run configuration, I notice that it doesn't properly call any of my scripts and then, proceeds to change the SQL Dialect of the file to just Oracle.
Is this approach even possible? I have tried changing the dialects of all the scripts to be PLUS but, with no success. I've used all the methods I have seen: #, ##, SQL > and :r to call my scripts but, everything seems to fail and then, change the dialect of the SQLPlus file. However, running the sql statements themselves inside the SQLPlus file works fine, and does not change the dialect.

Jetbrains Pycharm SQL vs Oracle SQL Developer

I'm running a complicated SQL script in Oracle SQL Developer. The query starts with
DEFINE custom_date = "'22-JUL-2016'"
While this works fine in Oracle SQL Developer I get an error in jetbrains:
<statement> expected got DEFINE
Also when I run the query it says:
ORA-00919: invalid function
even though it all works fine in Orace SQl Developer.
Is there anything specific I need to configure in Jetbrains Pycharm to be able to execute Oracle SQL queries correctly?
DEFINE isn't a core feature of the database, instead it's a command in SQL*Plus.
SQL Developer has as script engine which supports all of the SQL*Plus commands, including DEFINE, which is why it works when you run it there.
DEFINE just creates a variable and assigns a text value to it. You'll need to re-write your code to declare the variable and assign values to it instead.
Docs for DEFINE

Can liquibase syntactically validate the SQL generated using updateSql command?

I found 2 issues per se when I ran the updateSQL commandline in Liquibase
The last statement in Liquibase updateSQL output viz Insert into DBCHANGELOG table does not commit automatically when the sql is run via sqlplus commandline
As a result of this, though the changeset gets executed, the DBCHANGELOG table does not have the insert statement to record it. So when I run the updateSQL once again, the last changeset is once again created in the SQL output which is incorrect.
Liquibase does not validate / check syntax errors in SQL.
As a result of this, even if the changeset SQL fails, the insert to DBChangeLog table for the changeset succeeds which is incorrect. Is there a way that the insert statement following the changeset be stopped / failed if the changeset SQL actually failed ?
Any help is greatly appreciated... we are this close to getting Liquibase implemented... !!
To answer the question in your subject line, no, Liquibase cannot validate the SQL. Liquibase supports many different databases, and each has different SQL syntax.
If you can, stop using the SQL generated by updateSQL to actually do the updates, and use Liquibase itself to do the updates. That way Liquibase can detect errors and behave more properly. I recommend that if DBAs are scared of Liquibase touching the database that teams use the generateSQL as a pre-check to see what Liquibase will do, but let Liquibase do its job.
I also find best practice of Liquibase is not to use a SQL script but to manually write the Liquibase XML file for the change.
I've tried using the ExecuteCommand tag to lunch sqlplus or sqlcmd (as I know my target database) and it has a bug which as for now it is closed?! (but this is open source, so I can't complain :) )
Having said that, I found that working on XML to specify the changes causes many other challenges, for example:
1. Making sure that every change was included in the changelog xml file. I've heard many organizations who forget to add the file to the changelog.
2. Making sure the file for the specific change is always in sync with the file-based version control. Imagine what will happen if it doesn't - which happens to many of my customers...
3. Wasted time spent on merging changelogs between different environments (branches, UAT - critical fixes, sandboxes, etc...)

is it possible to omit GO in Sybase Sql scripts?

I have started doing Sybase programming since some time.the scripts I have encountered are all having GO statements as a batch separator. my question here is is it possible to omit GO in a script and get the script execued?
if yes, will it affect anything at all ?
go is required. It's no different than Oracle and MySQL using ; If you omit the go, the script will not execute.