How to configure sql query shortcuts in SQL Workbench/J? - sql-workbench-j

I want to get rid of writing a full sql syntax,
like I'm using for years in PL/SQL Developer editor or SQL SERVER Management Studio.
Examples:
count1 = select count(1) from
counthaving = select count(1) from having count(1)>?
s* = select * from
w = where
to_date = to_date('','dd/mm/yyyy')
sc* = select count(*) from
Thanks

This is what macros are intended for.
You can define a macro as "expandable" by selecting the "Expand while typing" check box in the macro definition.
If you type the "name" of such a macro in the editor and use the tab expansion key, the text will be replaced with the text of the macro.
It's recommended to un-check the option "Include in menu" for those macros.

Related

is there are any alpha logic define in informix DB

i am using informix DB , i need to get records that contain alpha [A-Za-z] character on last character
what i try is :
select * from table_name
where (SUBSTR(trim(customer),-1,1)!='0' and SUBSTR(trim(customer),-1,1)!='1' and SUBSTR(trim(customer),-1,1)!='2' and SUBSTR(trim(customer),-1,1)!='3' and SUBSTR(trim(customer),-1,1)!='4' and SUBSTR(trim(customer),-1,1)!='5' and SUBSTR(trim(customer),-1,1)!='6' and SUBSTR(trim(customer),-1,1)!='7' and SUBSTR(trim(customer),-1,1)!='8' and SUBSTR(trim(customer),-1,1)!='9') or (SUBSTR(trim(customer),-1,1)=' ') or (SUBSTR(trim(customer),-1,1)='') or (customer IS NULL)
is there are any way to write where SUBSTR(trim(customer),-1,1)=alpha rather than write
SUBSTR(trim(customer),-1,1)!='0' and SUBSTR(trim(customer),-1,1)!='1' and SUBSTR(trim(customer),-1,1)!='2' and SUBSTR(trim(customer),-1,1)!='3' and SUBSTR(trim(customer),-1,1)!='4' and SUBSTR(trim(customer),-1,1)!='5' and SUBSTR(trim(customer),-1,1)!='6' and SUBSTR(trim(customer),-1,1)!='7' and SUBSTR(trim(customer),-1,1)!='8' and SUBSTR(trim(customer),-1,1)!='9'
If you have a 'recent' version of Informix (anything over 12.10 should do) you can use regex_match():
https://www.ibm.com/support/knowledgecenter/SSGU8G_14.1.0/com.ibm.dbext.doc/ids_dbxt_544.htm
Something like :
> select * from table(set{'test','test1','tesT'})
where regex_match(unnamed_col_1, '[a-zA-Z]$');
unnamed_col_1
test
tesT
2 row(s) retrieved.
>
You can use the Informix MATCHES operator, that is supported in any version.
The query would be like this:
select * from table_name
where customer matches "*[A-Za-z]"

db2 stored procedure with create select and cursor

I'm trying to define a DB2 Stored Proc that would (ideally), CREATE VIEW, then do a SELECT against that VIEW to build another piece of SQL, then execute that SQL using a CURSOR and return a result set.
I've 2 problems:
DB2 doesn't appear to like the mix of CREATE, SELECT and DECLARE CURSOR within a single SP,
and I can't figure out what syntax to use to declare a cursor based on SQL that is stored as a string in the declared VARCHAR that is the output from the SELECT statement.
Has anyone done anything similar and/or able to give me some syntax examples?
Sure you can do that in DB2. In order to execute a 'create view' you need to use a dynamic SQL. The same for the select on the view. Finally, for the cursor, you define a generic cursor, and execute it dynamically.
DECLARE SENTENCE VARCHAR(256);
DECLARE TABNAME VARCHAR(128);
DECLARE STMT STATEMENT;
DECLARE TABLES_CURSOR CURSOR
FOR TABLES_RS;
SET SENTENCE = 'CREATE VIEW TABS SELECT TABNAME FROM SYSCAT.TABLES';
PREPARE STMT FROM SENTENCE;
EXECUTE STMT;
SET SENTENCE = 'SELECT TABNAME FROM TABS';
PREPARE TABLES_RS FROM SENTENCE;
OPEN TABLES_CURSOR;
FETCH TABLES_CURSOR INTO TABNAME;
You can see real SQL PL examples in my project db2unit https://github.com/angoca/db2unit/blob/master/src/main/sql-pl/04-Body.sql

SQL change the "enter value" text to something else?

I am inputting a value into sql plus, and here is how I am getting the input:
And Abc.Abdef = &Abcdef;
When I execute it, it says:
Enter value for Abcdef:
How can I change the text enter value for to something else?
Is it possible?
No, it is not possible. SQL*Plus doesn't let you customise the prompt for substitution variables.
However, you can prompt for an input (with a custom prompt) and assign that to a variable which you then use in your statement:
SQL> accept foo prompt 'FOO: '
FOO: bar
SQL> select * from dual where '&foo'='bar';
old 1: select * from dual where '&foo'='bar'
new 1: select * from dual where 'bar'='bar'
D
-
X

SQL query symbol "*="

What does the symbol "*=" stand for in a SELECT statement? How does this affect the performance? Can we replace it with JOINS?
Thanks
STR
In some database systems,
SELECT ... FROM a, b WHERE a.field1 *= b.field2
is an old syntax for a LEFT OUTER JOIN:
SELECT ... FROM a LEFT JOIN b ON a.field1 = b.field2
Microsoft SQL Server, for example, considers the *= syntax to be deprecated since version 2005.
So yes, you can replace *= with a JOIN. In fact, you should. (I doubt that it affects performance in any relevant way, but your queries might stop working in newer versions of your database engine.)
In some implementations of SQL, a SELECT statement that has a assignment operator(=) can be used to create the relationship between a column heading and the expression that defines the values for the column.
So an example might be:
SELECT name = 'renamed_column_name'
FROM users
More Info:
Unfortunately the = operator can mean both assign and equality.
For assignment:
DECLARE #Counter INT;
SET #Counter = 1;
For equality:
The = is a equality operator states that the left side must equal the right side.
The could mean a value must equal the result returned by a subquery, or a variable must equal a literal, no matter the case... a = b means that a and b have to have the same value.
SELECT * FROM users LEFT JOIN posts ON users.id = posts.user_id

delphi "Invalid use of keyword" in TQuery

I'm trying to populate a TDBGrid with the results of the following TQuery against the file Journal.db:
select * from Journal
where Journal.where = "RainPump"
I've tried both Journal."Where" and Journal.[Where] to no avail.
I've also tried: select Journal.[Where] as "Location" with the same result.
Journal.db is a file created by a third party and I am unable to change the field names.
The problem is that the field I'm interested in is called 'where' and understandably causes the above error. How do I reference this field without causing the BDE (presumably) to explode?
Aah, I'm loving delphi again... I found a workaround. The TQuery component has the Filter property :-)
I omitted the "Where=" where clause from the query whilst still keeping all the other 'and' conditions.
I set the Filter property to "Where = 'RainPump'".
I set the Filtered property to True and life is good again.
I'm still wondering if there's a smarter way to do this using this old technology but if it's stupid and it works, then it's not stupid.
I'm afraid that someone reading this thread will get the impression that the BDE SQL engine cannot handle the query:
select * from Journal where Journal."Where" = "RainPump"
and will waste their time unnecessarily circumlocuting around it.
In fact this construction works fine. The quotes around "Where" keeps the BDE from interpreting it as a keyword, just as you would expect.
I don't know what is wrong in Baldric's particular situation, or what he tried in what order. He describes the problem as querying a *.db table, but his SQL error looks more like something you'd get in passthrough mode. Or, possibly he simplified his code for submission, thus eliminating the true cause of the error.
My tests performed with:
BDE v.5.2 (5.2.0.2)
Paradox for Windows v. 7 (32b)
Delphi 5.0 (5.62)
Various versions of the statement that succeed:
select * from Journal D0 where D0."Where" = "RainPump"
select * from Journal where Journal."Where" = "RainPump"
select * from ":common:Journal" D0 where D0."Where" = "RainPump"
select * from ":common:Journal" where ":common:Journal"."Where" = "RainPump"
select * from :common:Journal where Journal."Where" = "RainPump"
select * from ":common:Journal" D0 where D0."GUMPIK" = 3
select * from ":common:Journal" where ":common:Journal"."GUMPIK" = 3
select * from :common:Journal where Journal."GUMPIK" = 3
Versions of the statement that look correct but fail with "Invalid use of keyword":
select * from ":common:Journal" where :common:Journal."Where" = "RainPump"
select * from :common:Journal where :common:Journal."Where" = "RainPump"
select * from ":common:Journal" where :common:Journal."GUMPIK" = 3
select * from :common:Journal where :common:Journal."GUMPIK" = 3
-Al.
You can insert the resultset into a new table with "values" (specifying no column names) where you have given your own column names in the new table and then do a select from that table, Using a TQuery, something like:
Query1.sql.clear;
query1,sql.add('Insert into newtable values (select * from Journal);');
query1.sql.add('Select * from newtable where newcolumn = "Rainpump";');
query1.open;
Rewrite it like this, should work:
select * from Journal where Journal.[where] = "RainPump"
Me, I'd rename the awkward column.
In MySQL, table/column names can be enclosed in `` (the angled single quotes). I'm not sure what the BDE allows, but you could try replacing [where] with `where`
select * from Journal where Journal."where" = "RainPump"
Ok, so naming columns after keyboards is bad in ANY SQL system. Would you name a column "select" or "count" or "alter" or "table" or perhaps just for the fun of it "truncate" or "drop"? I would hope not.
Even if you build in the work around for this instance you are creating a mine field for whomever comes after you. Do what mj2008 said and rename the bloody column.
Allowing this column name to persist is the worst example of someone who is building a system and would get you on the poop list for any project manager.

Resources