If Then Block using SnowSQL in snowflake - teradata

Is there a way to achieve If then computational logic using snowsql commands like we
have in Teradata Bteq Commands as below:
.if 1 >= 0 then select department_id from department;
.if ACTIVITYCOUNT > 0 then select employee_id from employee

Related

INSERT OR IGNORE conversion sqlite to mariadb

I'm trying to convert my SQlite code to Mariadb but i'm stuck with a query for a crossed table between two another tables
The purpose of the table is to add ID from the other table but only if they don't exists
I have two tables
Table COMPUTERS
ID(primary) / NETBIOS(unique) / IP / SOFTWARE_STAT / COPY_STAT / AV_STAT
ex : 1 PC1 192.168.1.1 KO KO 0
Table SOFTWARES
ID(primary) / NAME(unique)
ex : 1 ADOBE
And the cross table
Table INSTALL
ID (primary)/ COMPUTER_ID / SOFTWARE_ID / FAIL
1 1 1 0
My SQLITE code below is working
INSERT OR IGNORE INTO INSTALL (COMPUTER_ID,SOFTWARE_ID)
SELECT
(SELECT ID FROM COMPUTERS WHERE IP = '192.168.1.1'),
(SELECT ID FROM SOFTWARES WHERE NOM = 'ADOBE')
WHERE NOT EXISTS
(SELECT
COMPUTER_ID,SOFTWARE_ID FROM INSTALL
WHERE
COMPUTER_ID = (SELECT ID FROM COMPUTERS WHERE IP = '192.168.1.1')
AND
SOFTWARE_ID = (SELECT ID FROM SOFTWARES WHERE NOM = 'ADOBE')
)
I've tried that
INSERT INTO INSTALL (COMPUTER_ID,SOFTWARE_ID)
SELECT
(SELECT RowID FROM COMPUTERS WHERE IP='192.168.1.1'),
(SELECT RowID FROM SOFTWARES WHERE NOM='ADOBE')
WHERE NOT EXISTS
(SELECT COMPUTER_ID,SOFTWARE_ID FROM INSTALL
WHERE
COMPUTER_ID = (SELECT RowID FROM COMPUTERS WHERE IP='192.168.1.1')
AND
SOFTWARE_ID = (SELECT RowID FROM SOFTWARES WHERE NOM='ADOBE'));
Without success
Someone have an idea ? Thanks in advance.
INSERT OR IGNORE --> INSERT IGNORE
However, you have a redundancy: (1) IGNORE, and (2) NOT EXISTS. That is, you both verify that it is not a dup and you ignore errors if it is.
Otherwise, the Sqlite code should work as is in MariaDB. What went wrong? Did you get an error?
MariaDB has no RowID.

Ignore case in Oracle 11g

Is there any way to set table or fields to be ignore case? And I don't want to use lower or upper function. I'm shocked from oracle if they not support Ignore case, I still searching but I didn't find any solution
As you said, you can't explicitly make a column "case insensitive".
You can, however, work around it by using virtual columns, triggers, check constraints or just querying using case insensitive predicates and upper/lower functions.
Some examples:
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.4.0
Connected as fsitja#sitja
SQL>
SQL> create table job (JOB_TITLE varchar2(40));
Table created
SQL> alter table job add constraint ck_job_upper check (job_title = upper(job_title));
Table altered
SQL> insert into job values ('clerk');
insert into job values ('clerk')
ORA-02290: restrição de verificação (FSITJA.CK_JOB_UPPER) violada
SQL> insert into job values ('CLERK');
1 row inserted
SQL> commit;
Commit complete
SQL> create or replace trigger tr_br_upper_job_title before insert or update of job_title on job
2 for each row
3 begin
4 :new.job_title := upper(:old.job_title);
5 end;
6 /
Trigger created
SQL> insert into job values ('clerk');
1 row inserted
SQL> select * from job;
JOB_TITLE
----------------------------------------
CLERK
SQL> commit;
Commit complete
SQL>
SQL> drop table job;
Table dropped
SQL> create table job (JOB_TITLE varchar2(40), upper_job_t as (upper(job_title)));
Table created
SQL> insert into job (job_title) values ('CLERK');
1 row inserted
SQL> insert into job (job_title) values ('clerk');
1 row inserted
SQL> commit;
Commit complete
SQL> select * from job where regexp_like(job_title, 'CLERK', 'i');
JOB_TITLE UPPER_JOB_T
---------------------------------------- ----------------------------------------
CLERK CLERK
clerk CLERK
SQL>

Oracle Trigger Issue (Toad)

Oracle 11g/XE
Toad 12.7.1.11
I have a newly-installed db. Connected as SYS, created a user called 'jasons' with password and granted DBA. Disconnected and reconnected as 'jasons'. Then ran the following script:
CREATE SEQUENCE people_seq
MINVALUE 0
MAXVALUE 10000
START WITH 0
INCREMENT BY 1
CACHE 20;
commit;
create table people (PID number(10), FirstName varchar(20), LastName varchar(20));
commit;
alter table people add (constraint people_pk primary key (PID));
commit;
CREATE OR REPLACE TRIGGER people_trig
BEFORE INSERT ON people
FOR EACH ROW
BEGIN
SELECT people_seq.NEXTVAL
INTO :new.pid
FROM dual;
END;
commit;
When I run the script, everything goes well until the CREATE TRIGGER section. It throws:
ORA-04089: cannot create triggers on objects owned by SYS
I just created that very table as jasons! Can somebody please tell me what's going on?
Duh...
When making a new connection in Toad there's a nice little dropdown box in the middle of the right-hand side that says "Connect As" with your choice of Normal, SYSDBA, and SYSOPER.
It defaults to SYSDBA -- but when you select 'Normal'... Well... The above script works just fine ;-)

sqlite count rows of tables identified by subquery

I want to get the count of rows in every table in a Sqlite3 database. I want to avoid writing out a longhand query. I can get the list of tables like this:
SELECT name FROM sqlite_master WHERE type='table'
and I would like to use it in a subquery like this:
select count (*) from (SELECT name FROM sqlite_master WHERE type='table');
but would just return the total rows in the subquery, which isn't what I want.
How can I write a query that will list each table along with their counts?
I have seen dynamic SQL for this kind of thing but I don't think SQLite has that.
I have written a bash loop to do this but I would prefer to do it as a single query
for t in $(sqlite3 data.db "SELECT name FROM sqlite_master WHERE type='table'"); do
echo -n "$t = "; sqlite3 data.db "SELECT COUNT(*) FROM $t;"
done
Ideas appreciated
I used the following shell syntax to blindly get counts from tables in a database I was debugging.
db="database.db"
for i in `sqlite3 "$db" "SELECT name FROM sqlite_master WHERE
type='table';"`; do echo -n $i\ \=\ ; sqlite3 "$db" "SELECT
COUNT(*) FROM $i;"; done
cols = 0
sqlite_sequence = 0
datacols = 17
hits = 0
host = 0
document = 0
admin = 2
comments = 0
If you want to know these values for debugging purposes, look at the output of the sqlite3_analyzer tool.
If you want to use these values in your program, you have to generate the queries dynamically in your program.

sqlite: insert or update a row, performance issue

sqlite table:
CREATE TABLE IF NOT EXISTS INFO
(
uri TEXT PRIMARY KEY,
cap INTEGER,
/* some other columns */
uid TEXT
);
INFO table has 5K+ rows and is run on a low power device (comparable to a 3 year old mobile phone).
I have this task: insert new URI with some values into INFO table, however, if URI is already present, then I need to update uid text field by appending extra text to it if the extra text to be appended isn't found within existing uid string; all other fields should remain unchanged.
As an example: INFO already has uri="http://example.com" with this uid string: "|123||abc||xxx|".
I need to add uri="http://example.com" and uid="|abc|". Since "|abc|" is a substring within existing field for the uri, then nothing should be updated. In any case, remaining fields shouldn't be updated
To get it done I have these options:
build some sql query (if it's possible to do something like that with sqlite in one sql statement),
Do everything manually in two steps: a) retrieve row for uid, do all processing manually and b) update existing or insert a new row if needed
Considering this is constrained device, which way is preferable? What if I omit the the extra requirement of sub-string match and always append uid to existing uid field?
"If it is possible with sqlite in one sql statement":
Yes, it is possible. The "UPSERT" statement has been nicely discussed in this question.
Applied to your extended case, you could do it like this in one statement:
insert or replace into info (uri, cap, uid)
values ( 'http://example.com',
coalesce((select cap from info where uri = 'http://example.com'),'new cap'),
(select case
when (select uid
from info
where uri = 'http://example.com') is null
then '|abc|'
when instr( (select uid
from info
where uri = 'http://example.com'),'|abc|') = 0
then (select uid
from info
where uri = 'http://example.com') || '|abc|'
else (select uid
from info
where uri = 'http://example.com')
end )
);
Checking the EXPLAIN QUERY PLAN gives us
selectid order from detail
---------- ---------- ---------- -------------------------
0 0 0 EXECUTE SCALAR SUBQUERY 0
0 0 0 SEARCH TABLE info USING INDEX sqlite_autoindex_INFO_1 (uri=?)
0 0 0 EXECUTE SCALAR SUBQUERY 1
1 0 0 EXECUTE SCALAR SUBQUERY 2
2 0 0 SEARCH TABLE info USING INDEX sqlite_autoindex_INFO_1 (uri=?)
1 0 0 EXECUTE SCALAR SUBQUERY 3
3 0 0 SEARCH TABLE info USING INDEX sqlite_autoindex_INFO_1 (uri=?)
1 0 0 EXECUTE SCALAR SUBQUERY 4
4 0 0 SEARCH TABLE info USING INDEX sqlite_autoindex_INFO_1 (uri=?)
1 0 0 EXECUTE SCALAR SUBQUERY 5
5 0 0 SEARCH TABLE info USING INDEX sqlite_autoindex_INFO_1 (uri=?)
As far as I know, sqlite will not cache the results of scalar sub-queries (I could not find any evidence of chaching when looking at the VM code produced by EXPLAIN for the above statement). Hence, since sqlite is an in-process db, doing things with two separate statements will most likely perform better than this.
You might want to benchmark the runtimes for this - results will of course depend on your host language and the interface you use (C, JDBC, ODBC, ...).
EDIT
A little performance benchmark using the JDBC driver and sqlite 3.7.2, running 100.000 modifications on a base data set of 5000 rows in table info, 50% updates, 50% inserts, confirms the above conclusion:
Using three prepared statements (first a select, then followed by an update or insert, depending on the selected data): 702ms
Using the above combined statement: 1802ms
The runtimes are quite stable across several runs.

Resources