SAP HANA SQLScript in DBeaver - hana

I would like to ask if anyone has any idea/ know how to use SAP HANA SQLScript in DBeaver.
Since SAP HANA supports some very useful features such as table variables etc, I would like to run following code (just a quick example):
do BEGIN
DECLARE lv_id INT;
lv_id = 100;
lt_test = SELECT id
FROM some_table
WHERE ref_id < :lv_id;
SELECT * FROM some_other_table
WHERE from_id IN (SELECT id FROM :lt_test); END;
but unfortunately it will give some error messages
sql syntax error: incorrect syntax near "NULL": line 13 col 35 (at pos 174)
Does anyone know if there is anything that can be done so this code will work in DBeaver without the need of going into SAP Web IDE or HANA Studio all the time?
Maybe someone also knows any other good IDE with features like code completion, etc., for SQL that works well with HANA (other than Web IDE and HANA Studio)...

The "trick" here is to select the correct handling of bind parameters in DBeaver.
When a SQL command with strings that look like bind parameters should be executed, DBeaver presents a dialogue window:
In this window, one can specify how the bind variables should be handled.
As the SQLScript variables are not bind variables (i.e. the client does not bind values to them) the correct selection in this window is to IGNORE the bind variables.

Related

How to use variables in SQL queries on DataGrip (Exasol dialect)?

Database: EXASOL
IDE: IntelliJ DataGrip
I am trying to declare variables in SQL and call them throughout the query.
This is the equivalent in Microsoft SQL Server:
DECLARE #var AS INT = 3
SELECT #var AS var
-- Use case example:
SELECT * FROM table1 WHERE column_value = #var
Is this possible in DataGrip and Exasol? I searched the documentation (General Script Language, Database Interaction) of Exasol where they describe the following:
a = 3
SELECT :a
However, this opens a console where I am asked to type the value of a. I don't want to type the values of variables every time I execute the code. I want to set a variable and use it on different parts of the query, just like any other high-level programming language.
I found two similar questions on JetBrains' forum (1, 2) but they are unanswered. Another one found on StackOverflow (url) just stated that the dialect is not supported on DataGrips.
Does anyone know how to solve this? Is it simply not supported? It would really increase productivity for me and my team.
Thank you in advance!
After spending some time, I found out that this is not possible. Instead Exasol allows LUA scripts that can run such calculations. Below you will find an example:
CREATE LUA SCRIPT "TEST" (p_country) RETURNS TABLE AS
local param_c = p_country
exit(
query(
[[
SELECT * FROM SCHEMA_NAME.TABLE_NAME
WHERE SK_COUNTRY = :local_c;
]]
,{local_c=param_c}
)
);
/
EXECUTE SCRIPT SCHEMA_NAME.TEST('DE');
In this example the keyword RETURNS TABLE outputs the table results from this query. The keyword exit() is similar to a print() method. And lastly, I don't know why but the function parameter needs to be assigned to a local variable, which then needs to be assigned to another variable in the query. This makes no sense to me, but I could not get it to work otherwise.
In my example I have the script parameter p_country which is assigned to the local parameter param_c which is then assigned to the query parameter local_c.
You can find the documentation under:
https://docs.exasol.com/database_concepts/scripting/general_script_language.htm?Highlight=for%20loop
https://docs.exasol.com/database_concepts/scripting/db_interaction.htm

SAS Enterprise Guide: How to get Oracle table indexes

I am using SAS Enterprise Guide (EG) 6.1 and want to know what are the indexes of our Oracle tables. Is there a way to write a program to get this information?
I tried to do:
LIBNAME DW ORACLE USER='username' PASSWORD='password' PATH='path.world' SCHEMA='schema';
DATA _NULL_ ;
dsid = OPEN(DW.some_table) ;
isIndexed = ATTRN(dsid,"ISINDEX") ;
PUT isIndexed = ;
RUN ;
some_table is the name of (my table), but I get an error:
ERROR: DATA STEP Component Object failure. Aborted during the COMPILATION phase.
ERROR 557-185: Variable some_table is not an object.
Reference: https://communities.sas.com/t5/ODS-and-Base-Reporting/check-if-index-exists/td-p/1966
OPEN takes a string or a value that resolves to a string. So you need
dsid= OPEN('dw.some_dataset');
I don't know if you can use that with Oracle or not, and I don't know whether ATTRN will be useful for this particular purpose or not. These all work well with SAS datasets, but it's up to the libname engine (and whatever middleware it uses) to implement the functionality that ATTRN would use.
For example, I don't use Oracle but I do have SQL Server tables with indexes, and I can run the above code on them; the code appears to work (it doesn't show errors) but it shows the tables as being unindexed, when they clearly are.
Your best bet is to connect using pass-through (CONNECT TO ...) instead of libname, and then you can run native Oracle syntax rather than using SAS.

Store Procedures in SQuirrel 3.2.1 when using it with a JDBC driver for a DB2 database

I expend a lot of time trying to retrieve data from a Stored Procedure, here is the code
CREATE PROCEDURE aprocedure(
IN idin CHAR,
OUT returnvalue CHAR)
AS:
SET returnvalue=
(SELECT something
FROM sometable
WHERE id=idin)
I could create it, with no problems, but when I tried to call it like this:
call someprocedure('theid', ?)
Error -313 kept poping out, I did my homework and check the web, IBM forums were no help at all, I couldnt find any documentation, specifications, or anything that make this more clear, also SQL error code -313 means that the number of parameters in the procedure does not match the number of parameters you're using when you call it. So, after too much research, I started thinking that DB2 with JDBC driver and or SQuirreL have trouble when returning OUT values, (I also installed a DB2 CTL client, created a local database, created a table, created the procedure, I called, and everything worked nicely) so I change my code to this (to use a Result Set instead of an OUT):
CREATE PROCEDURE someprocedure(IN idin CHAR (22))
DYNAMIC RESULT SETS 1
P1: BEGIN
DECLARE cursor1 CURSOR WITH RETURN FOR
SELECT something FROM sometable WHERE id=idin;
OPEN cursor1;
END P1
aaaaaaaaaaaand NOTHING, SQuirreL gave me some error codes, when trying to create it, so... I enter that same code in Aqua Data Studio 4.7, and worked like a charm, I call the procedure from Aqua Data like this:
call someprocedure('theid');
and it returned what was supposed to return, I tried that same sentence with SQuirreL...
and it WORKED too !!
Im sure that my sintaxys was correct all along, even with the OUT type of return, so, my question, finally is this.
Does SQuirreL check the input you enter before passing it to the JDBC?
Also
Where do I can find how exactly DB2 is altering SQL code?? because we all know that all DBM change the SQL a bit, but MySQL have great documentation... and i honestly couldnt find any good one on DB2, also im talking about "pure" SQL since in DB2 you can enter stored procedures in C , Java etc...

whats the syntax for params in a DB2 query

in MS-SQL I can do something like this
#myVar AS int;
#myVar = 12;
SELECT * FROM table WHERE field = #myVar;
this totally bombs out in DB2 - and I'm not sure if it's RDBMS specific or if it's because I've FUBAR'd the syntax...
Any help is appreciated
there are a limited number of things you can do dynamically in db2 sql compared to ms-sql. most of the syntax for what you appear to be attempting is reserved for use only in a procedure in db2. see the documentation here http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0004240.html for what you can do dynamically in db2.
if you are ok supplying parameter value on the fly just use
select * from table where field = ?; when you run it, it will ask you to supply a value for the parm.
If you are using ADO.NET Data Provider to connect to the DB2 Express-C, you can prefix bound SQL parameters with #.
I'm not sure about other DB2 environments, but common symbols used in other databases are: : and ?, so it might be worth trying one of those.

Delphi Interbase Sql Conversion to Sql Server and Oracle

i have a delphi application which uses database
interbase / firebird. To consult and write data I use the
InterBase components palette (IBTable, IBQuery, IBDataset). I'm
performing the conversion of my system to sqlserver / Oracle but i have
thousands of queries that are assembled at runtime with SQL Instructions
Specific of database InterBase/Firebird. Anyone know
any component or tool that makes Parse commands Interbase -> SQL Server or Interbase-> Oracle ?
what i need its something like:
Var
Parser: TParser;
OutputSql: String;
Begin
Parser := TParser.Create();
Parser.Text := 'SELECT FIRST 10 CITYNAME FROM TBCITY';
if Firebird then
OutPutSql := Parser.ParseTo('SQLSERVER');
if Oracle then
OutPutSql := Parser.ParseTo('ORACLE');
ComponentAccess.Sql.Text := OutPutSql;
...
The Result Of:
Parser.ParseTo('SQLSERVER');
Will Be
'SELECT TOP 10 CITYNAME FROM TBCITY'
And
Parser.ParseTo('ORACLE');
Will Be
'SELECT CITYNAME FROM TBCITY WHERE ROWNUM <= 10'
1) AFAIK, libraries like AnyDAC, have SQL abstraction syntax. May be you can use this feature in your SQL command text.
2) If you are assembling your SQL at runtime, then why not just code like that:
if Firebird then
SQL.Add(...)
else if Oracle then
SQL.Add(...)
...
We have implemented that in AnyDAC. You may use LIMIT escape function:
ADQuery1.Sql.Text := 'SELECT {LIMIT(10)} CITYNAME FROM TBCITY';
AnyDAC will automatically translate that into target DBMS syntax.
I have used kbmMW from Components4Developers and it has an abstracted set of queries that provide macros and the like to enable /easier/ cross database work but it's mainly for client/server use. Devart also do a good set of cross database components - we use their SQL Server set. However, each project I've done I've ended up writing a specific set of SQL scripts for each database. Obviously there is a fair bit of common ground for simple select stuff but the feature sets of the different databases are often too different to make it easily workable.
I end up with something similar to #oodesigner's response except that we use $ifdef and define my SQL strings in a separate const unit.
{$ifdef USE_MSSQL}
QUERY_ONE = 'select blah blah blah...';
{$else}
QUERY_ONE = 'select nah nah nah...';
{$endif}
Then in the main unit a simple assignment
SQL.Text := QUERY_ONE;
or
SQL.Text := Format(QUERY_TWO, [some_very_carefully_quoted_stuff_or_use_params]);
Don't know of anything that would automate or parse it. And the problem with that is that you still have to go through and check every single query because it's too easy to get things wrong when converting.