In teradata, can you insert a cte into a table? - teradata

I've looked at the other postings but I haven't seen a resolution for this.
CREATE TABLE newtable AS (SELECT * FROM yourtable) with no data ;
Then
with cte as (
select top 10* from yourtable
)
insert into newtable
select * from cte
select * from newtable
I get an error saying it expects a select and not an insert.
How would I accomplish inserting the cte into a table?

Related

bulk update in SQLite

I have 2 tables with identical structure I want to update one table using data from the other, matching on primary key. SQLite has a with (CTE) statement but the following doesn't work (sqlite3 v. 3.29.0):
sqlite> select * from main;
1|A
2|B
4|D
5|E
6|F
sqlite> select * from temp;
1|aa
2|bb
3|cc
4|dd
5|ee
sqlite> with mapping as (select main.ID, temp.Desc from main join temp on temp.ID=main.ID) update main set Desc=mapping.Desc where main.ID=mapping.ID;
Error: no such column: mapping.Desc
I've tried using "select main.ID as ID, temp.Desc as Desc", but get the same error message.
To update your main table from your cte, use a subquery, since sqlite doesn't support update from
with mapping as
(select main.ID, temp.Desc
from main
join temp on temp.ID=main.ID)
update main set Desc=
(select Desc from mapping where ID = main.ID limit 1);
see dbfiddle

Insert random row to table and then delete

I've got two tables: tableA and tableB. I would like to insert random row from tableA to tableB and then delete this row from tableA. How can i do it? Is it possible at all? Below is my insert code but i dont know how to delete this row.
INSERT INTO tableB
SELECT * FROM tableA ORDER BY RANDOM() LIMIT 1;
You can use rowid:
DELETE from tableA
WHERE elementA = (
SELECT elementA from tableB
WHERE rowid = (SELECT MAX(rowid) FROM tableB)
)
Provided that elementA is unique in both tables, if this is executed after your INSERT statement it will find elementA from the inserted row and delete the row from TableA with that elementA.

string_agg function with IN operator not working in PostgreSQL Query

here is my query
select *
from table
where id in ( select string_agg(CAST(id as varchar), '","') FROM table)
string_agg() is completely useless and unnecessary for that:
select *
from table_one
where id in (select id FROM other_table)
I assume you are doing that for two different tables, otherwise that would be a very expensive way of writing: select * from table where id is not null

SQLite insert into table using the id from an inner insert

I'm trying to insert a parent and child at the same time.
My idea is to insert the parent, get the id using SELECT last_insert_rowid() AS [Id] and use this id to insert the child
I can get each part of this working independently but not as a whole. This is what I currently have:
INSERT INTO ParentTable (Col1)
VALUES( 'test')
SELECT last_insert_rowid() AS [Id]
The above works - so far so good. Now I want to use the result of this in the child insert. This is what I have:
INSERT INTO ChildTable (col1, col2, ParentId)
VALUES( 1, 2, SELECT Id FROM (
INSERT INTO ParentTable (Col1)
VALUES( 'test')
SELECT last_insert_rowid() AS [Id]
);
I get this error:
near "SELECT": syntax error:
Can anyone point me in the right direction?
You can't use INSERT in SELECT statement. You should first insert and then use last inserted id:
INSERT INTO ParentTable (Col1) VALUES( 'test');
INSERT INTO ChildTable (col1, col2, ParentId)
VALUES(1,2, (SELECT last_insert_rowid()));
Since you want to insert many records with parent ID, here is a workaround:
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE IF NOT EXISTS temp(id integer);
DELETE FROM temp;
INSERT INTO ParentTable (Col1) VALUES( 'test');
INSERT INTO temp SELECT last_insert_rowid();
INSERT INTO ChildTable (col1, col2, ParentId)
VALUES(1,2, (SELECT id FROM temp LIMIT 1));
.............
COMMIT;
DROP TABLE temp;
Or you can create a permanent table to this effect.
That SQLite.Net PCL driver assumes that you use the ORM: inserting an object will automatically read back and assign the autoincremented ID value.
If you're using raw SQL, you have to manage the last_insert_rowid() calls yourself.
Your idea is correct, but you have to do everything in separate SQL statements:
BEGIN; -- better use RunInTransaction()
INSERT INTO Parent ...;
SELECT last_insert_rowid(); --> store in a variable in your program
INSERT INTO Child ...;
...
END;
(SQLite is an embedded database and has no client/server communication overhead; there is no reason to try to squeeze everything into a single statement.)

% Wildcard not working with numbers?

I have the following sqlite db:
BEGIN TRANSACTION;
CREATE TABLE `table1` (
`Field1` TEXT
);
INSERT INTO `table1` VALUES ('testing');
INSERT INTO `table1` VALUES ('123');
INSERT INTO `table1` VALUES ('87654');
COMMIT;
This select returns the correct result:
select * from table1 where Field1 like '%e%';
However this one returns nothing?
select * from table1 where Field1 like '%2%';
Even Stranger in DB Browser for SQLite:
select * from table1 where CAST(Field1 AS Text) LIKE '%2%'
Returns:
1 Rows returned from: select * from table1 where CAST(Field1 AS Text) LIKE '2%' (took %3ms)
Maybe a bug? Drops the first %
A bug in DB browser. I raised an issue and its now been fixed in the nightlies.

Resources