Using partition with volatile tables? - teradata

This is Teradata
I have a multiset volatile table
I tried to use the following:
SELECT
col1, col2, ROW_NUMBER()
OVER (PARTITION BY col1
ORDER BY col2) AS row
FROM mytable
)
GROUP BY 1;
Why do I get No import parameters ( ?, ?? , ?B or ?C ) found in this query

This is not your actual query, row is a keyword and the alias for the Derived Table is missing.
Nevertheless the error is easy to explan as it's independent of any query: You're using SQL Assistant and clicked somehow on the Import Data icon. Simply uncheck it in the File menu.

Related

how to use LIKE with column name with SQLite [duplicate]

I have a SQLite table with columns like these:
id, name_1, name_2, name_nick, phone, email
and I am looking for a way to search in all columns which begins with name_.
Like LIKE but for the column names.
I find a lot for MySQL etc, but nothing for SQLite.
These queries do not work as I want:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'Customers'
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'Foods'
AND table_schema = 'YourDB'
AND column_name LIKE 'Vegetable%'
I get this:
ERROR: no such table: information_schema.columns
I have seen this question, which is how to obtain column names using SQL. However I think I would need a scripting language to run the query to get column names, filter them in some fashion, and then construct a new query using the filtered set of names. Instead I would like to try to do this all in SQL, if it is possible.

Having trouble running COUNT in my INSERT INTO statement

I'm having a little trouble getting my head around this statement. The idea is it's meant to initialize a table with a single row of values for each player in the database, but I can't figure out from a browser full of search tabs what I'm doing wrong. All I know is apparently my syntax is rubbish.
INSERT INTO tblKebabs
(TransactionID, PlayerID, Amount, Description, Timestamp)
SELECT
(COUNT(tblPlayers.PlayerID)) AS TransactionID,
tblPlayers.PlayerID AS PlayerID,0 as Amount,
"Initializer" as Description,"now" AS Timestamp)
FROM tblPlayers
WHERE tblPlayers.PlayerID > 0;
If you want the TransactionID column to be AUTOINCREMENT you have to define it in the CREATE statement.
If you already have defined it as INTEGER PRIMARY KEY it is already AUTOINCREMENT and you don't need to change something.
If you have nothing of the above then you have to recreate the table with INTEGER PRIMARY KEY for this column because SQLite does not allow such changes with ALTER. Now you can omit this column from your statement:
INSERT INTO tblKebabs
(PlayerID, Amount, Description, Timestamp)
SELECT
PlayerID,
0 as Amount,
'Initializer',
CURRENT_TIMESTAMP
FROM tblPlayers
WHERE PlayerID > 0;
You don't need aliases in the SELECT statement.
Also I used CURRENT_TIMESTAMP.
I'm not sure what you're trying to do here but if it's just about the count, try this
ROW_NUMBER() OVER(ORDER BY tblPlayers.PlayerID) AS TransactionID

sqlite column constraint not in set of values

In sqlite, how do I restrict the values of a column to being not in another table/view column?
For example
sqlite> create table tab1(col1 check (col1 not in (1,2)));
does what I want except that it seems only to exclude hard-coded values. However, the following does not work -
sqlite> create table tab2(vals_to_exclude);
sqlite> insert into tab2 values(1);
sqlite> insert into tab2 values(2);
sqlite> create table tab3(col1 check (col1 not in (select vals_to_exclude from tab2)));
Error: subqueries prohibited in CHECK constraints
Is it possible to constrain a column to exclude a dynamically determined set of values?
If the built-in mechanisms are not sufficient, implement the check manually with a trigger:
CREATE TRIGGER tab3_col1_not_in_tab2
BEFORE INSERT ON tab3 -- you also need a trigger for UPDATEs
FOR EACH ROW
WHEN EXISTS (SELECT 1
FROM tab2
WHERE vals_to_exclude = NEW.col1)
BEGIN
SELECT RAISE(FAIL, "col1 conflicts with tab2");
END;
or, alternatively:
CREATE TRIGGER tab3_col1_not_in_tab2
BEFORE INSERT ON tab3 -- you also need a trigger for UPDATEs
FOR EACH ROW
BEGIN
SELECT RAISE(FAIL, "col1 conflicts with tab2")
FROM tab2
WHERE vals_to_exclude = NEW.col1;
END;

SQLITE - INSERT or UPDATE without changing ROWID value

I need to update a table row IF EXISTS, otherwise INSERT a new row.
I tried:
INSERT OR REPLACE INTO table VALUES ...
but if the row row exist this statement changes the row's ROWID, and that's what I'm trying to avoid (I need the rowid :D)
I also tried to find a way to get some sort of return value from the update, in the case where an update has taken place, but I still don't understand how... If I could get the return value from the update statement, I could choose wether to proceed with an insert or not.
Do you have any suggestion or solution to this problem? Or do I need to make a copy of the ROWID and use that instead of the "pure" table ROWID?
Thanks in advance, best regards
ps: I was looking HERE and I was wondering if sqlite has the OUTPUT special word too, but google didn't help me..
---- EDIT after reading comments:
table schema example
CREATE TABLE test (
table_id TEXT NOT NULL,
some_field TEXT NOT NULL,
PRIMARY KEY(table_id)
)
INSERT or REPLACE INTO test (table_id, some_field) VALUES ("foo","bar")
I tested Chris suggestion but the rowid still gets changed. I think the best alternative is to do a SELECT to see if a row with that key already exist. If so, UPDATE, otherwise, INSERT... good old fashion but guaranteed to work.
Combine it with select, like this
INSERT or REPLACE INTO test (ROWID, table_id, some_field)
VALUES ((SELECT ROWID from test WHERE table_id = 'foo' UNION SELECT max(ROWID) + 1 from test limit 1), 'foo','bar')
You need to specify that your table_id is unique in addition to being the primary key:
sqlite> CREATE TABLE test (
table_id TEXT NOT NULL,
some_field TEXT NOT NULL,
PRIMARY KEY(table_id),
UNIQUE(table_id)
);
sqlite> insert or replace into test values("xyz", "other");
sqlite> select * FROM test;
xyz|other
sqlite> insert or replace into test values("abc", "something");
sqlite> insert or replace into test values("xyz", "whatever");
sqlite> select * FROM test;
abc|something
xyz|whatever
From version 3.24.0 (2018-06-04), SQLite now supports an UPSERT clause that will do exactly what the OP needed: https://www.sqlite.org/lang_UPSERT.html
The insert would now look like this:
INSERT INTO test (table_id, some_field) VALUES ("foo","baz")
ON CONFLICT(table_id) DO UPDATE SET some_field=excluded.some_field;

Converting SQL Server to Oracle

In my project, I have a database in SQL which was working fine. But now I have to make the application support oracle db too.
Some limitations I found out was that in Oracle, there is no bit field and the table name cannot be greater than 30 char. Is there any other limitation that I need to keep in mind.
Any suggestion from past experience will be helpful.
If I recall correctly from my earlier Oracle days:
there's no IDENTITY column specification in Oracle (you need to use sequences instead)
you cannot simply return a SELECT (columns) from a stored procedure (you need to use REF CURSOR)
of course, all stored procs/funcs are different (Oracle's PL/SQL is not the same as T-SQL)
The SQL ISNULL counterpart in Oracle is NVL
select ISNULL(col, 0)...
select NVL(col, 0)...
You will also struggle if you attempt to select without a from in Oracle. Use dual:
select 'Hello' from DUAL
Bear in mind also, that in Oracle there is the distinction between PL/SQL (Procedural SQL) and pure SQL. They are two distinct and separate languages, that are commonly combined.
Varchar in Oracle Databases called
varchar2 is limited to 4000
characters
Oracles concept of temporary tables is different, they have a global redefined structure
by default sort order and string compare is case-sensitive
When you add a column to a select *
Select * from table_1 order by id;
you must prefix the * by the table_name or an alias
Select
(row_number() over (order by id)) rn,
t.*
from table_1 t
order by id;
Oracle doesn't distinguish between null and '' (empty string). For insert and update you ca use '', but to query you must use null
create table t1 (
id NUMBER(10),
val varchar2(20)
);
Insert into t1 values (1, '');
Insert into t1 values (2, null);
Select * from t1 where stringval = 0; -- correct but empty
Select * from t1 where stringval is null; -- returns both rows
ORACLE do not support TOP clause. Instead of TOP you can use ROWNUM.
SQL Server: TOP (Transact-SQL)
SELECT TOP 3 * FROM CUSTOMERS
ORACLE: ROWNUM Pseudocolumn
SELECT * FROM CUSTOMERS WHERE ROWNUM <= 3

Resources