save union (merging) command directly in new database - database

Does anyone have an idea how if we could get the results of a "union" sqlite3 command and stock the returning database directly in a new database object?
For ex.:
we have table 1 (t1) in database 1 and table 2 (t2) in database 2.
Command tool:
sqlite> select * from t1 union select * from t2;
It prompts the new table in the command line tool but I cannot figure out how to stock the results in a new database?

CREATE TABLE t3(...);
INSERT INTO t3 SELECT * FROM t1 UNION SELECT * FROM t2;
Or if you don't care about column types and constraints:
CREATE TABLE t3 AS SELECT * FROM t1 UNION SELECT * FROM t2;

Related

How do you save a join as a Snowflake table?

I understand how a Join is made in Snowflake but I need to temporarily save it as a table, how can it be possible make it using consola?
(For this project I can't do it with any connectors)
You are looking for the CREATE TABLE AS SELECT or CTAS pattern:
CREATE TABLE tmp_table AS
SELECT a.*, b.*
FROM (SELECT * FROM VALUES ('a'),('b')) a(string)
CROSS JOIN (SELECT * FROM VALUES (1),(2)) b(vals);
SELECT * FROM tmp_table;
gives:
STRING
VALS
a
1
a
2
b
1
b
2
and if you want it to be temporary for the session you might want to add TEMP
CREATE TEMP TABLE tmp_table AS

Can we use Parameterized Views in Snowflake?

Can we use Parameterized Views in Snowflake. Such as pass the table name or database name as parameters instead of hardcoding it?
I think your best bet is to use session variables in conjunction with a regular view.
A session variable can be referenced in the view DDL, and will need to be set in any sessions querying the view.
To do this, you can make use of the IDENTIFIER function in Snowflake, which lets you use text as an object identifier.
create table t1 (col1 number, col2 number);
create table t2 (col1 number, col2 number);
set ti = 't1';
create view v1 as select col1, col2 from identifier($ti);
Before you query the view, you will need to set the session variable (ti in this case) to the table name (fully qualified if need be).
set ti = 't1';
select * from v1; -- returns data from t1
set ti = 't2';
select * from v1; -- returns data from t2
I have not found a way to do this, so I've created what I call a "wrapper view" in the past when I need something like this, example as follows.
I hope this helps...Rich
--create source tables and test records
CREATE TABLE t1 (id NUMBER, str VARCHAR);
CREATE TABLE t2 (id NUMBER, str VARCHAR);
CREATE TABLE t3 (id NUMBER, str VARCHAR);
INSERT INTO t1 VALUES(1, 'record from t1');
INSERT INTO t1 VALUES(2, 'record from t1');
INSERT INTO t2 VALUES(100, 'record from t2');
INSERT INTO t2 VALUES(101, 'record from t2');
INSERT INTO t3 VALUES(998, 'record from t3');
INSERT INTO t3 VALUES(999, 'record from t3');
--create the "wrapper" view
CREATE VIEW vw_t AS (
SELECT 't1' as table_name, * FROM t1
UNION ALL
SELECT 't2' as table_name, * FROM t2
UNION ALL
SELECT 't3' as table_name, * FROM t3);
--try it out
SELECT *
FROM vw_t
WHERE table_name = 't3';
--results
TABLE_NAME ID STR
t3 998 record from t3
t3 999 record from t3
I think the best way to handle something like this would be to create a UDTF that acts like a view that has been parameterized. So, in essence, you'd reference the UDTF like a view and pass the parameters into the UDTF, which would then return the data that you wish to use. Note that Snowflake has 2 options for UDTF (SQL and Javascript):
https://docs.snowflake.net/manuals/sql-reference/udf-table-functions.html
https://docs.snowflake.net/manuals/sql-reference/udf-js-table-functions.html
Although when using the interactive SQL worksheet on Snowflake, you can do this:
SET target_table_name='myTable';
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=$target_table_name
That does not work programmatically. Instead, as described here, a parameterized query such as a view uses this syntax:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=(?)
And yes, the name of that first (?) parameter is 1.
I'm working on my SnowflakeSQLHelper (an adaptation of the Microsoft Patterns and Practices) that will help when attaching parameters.

Copy data from table and insert in different SQL Server database

I have two databases
databaseone
databasetwo
and I have a similar table in the database, name of the table is tableemployeedetails.
In my databaseone, I have 500 columns in the table tableemployeedetails.
In my databasetwo, I have 10 columns in the table tableemployeedetails.
I cannot use insert into select query because I want to insert the data into different database.
What is the best way to do this in my situation?
I just want to merge tableemployeedetails in both the databases
Try this,
insert into databasetwo..tableemployeedetails
SELECT * FROM databaseone..tableemployeedetails A
WHERE NOT EXISTS (SELECT 1 FROM databasetwo..tableemployeedetails B
WHERE A.COLUMN=B.COLUMN
)
If both databases has different records then you need two insert statements as below. If they have same then you need to prefer which database records are latest and then write an update in addition to the below insert.
insert into databasetwo..tableemployeedetails
SELECT * FROM databaseone..tableemployeedetails d1
left join databasetwo..tableemployeedetails d2 on A.PKKEY=B.PKKEY
where d2.PKKEY is null
insert into databaseone..tableemployeedetails
SELECT * FROM databasetwo..tableemployeedetails d2
left join databaseone..tableemployeedetails d1 on A.PKKEY=B.PKKEY
where d1.PKKEY is null

Copy data from one table into another new table where columns are unknown

I'm trying to copy all data from Table1 into Table2.
I don't know what and how many columns are their in table 1. I mean I want to copy even column names from table 1 to table 2.
There is option like
insert *
into #table2
from Table1
but I even can't use this because there are many select query which has already been written at past. So I have to do something like this.
insert *
into #table2
from (select * from Table1)
This is throwing an error
Incorrect syntax near )
Try This:
Select * into #table2 from (select * from table1 ) as X
---To copy along with data..
select * into newtable from oldtable
--to copy only schema..
select * into newtable from oldtable where 1=2

SQL Server 2008 Union Select 'creating' non-existent records

The results of a union select statement in SQL Server 2008 is including records that are not found in either source table. Example:
theid is an integer ID value within each source table. Neither table includes theid value of 277741.
Select *
From DataTable1
WHere theid = 277741
-- 0 records returned
Select *
From DataTable2
Where theid = 277741
-- 0 records returned
However, when you run the Union Select statement below, a record is generated for theid.
Select *
Into ConjoinedData
From DataTable1
Union
Select *
From DataTable2
Where theid Not In (Select theid From DataTable1)
Select
From ConjoinedData
Where theid = 27741
-- 1 record returned
The theid field is not an identity field in either source table. Ultimately, the data for DataTable1 and DataTable2 came from the same parent, whose content includes an unrelated record with ID 277741. However, there are no foreign key relationships to it or any other table on either of the source tables. I have tried changing to a Union All query with no success. I have created an index on theid in both source tables and the 'created' record appears. I have dropped and recreated the source tables numerous times to no avail. Why is SQL Server getting the unrelated record from the disconnected parent table (source talbes were both cereated from the same parent using Select..Into statements and no foreign key relationship to either table back to the parent)?
277741 is different from 27741. The following script reproduces exactly what you describe:
create table Table1 (id int);
create table Table2 (id int);
go
insert into Table1(id) values (277742);
insert into Table2(id) values (27741);
go
Select * From Table1 WHere id = 277741;
Select * From Table2 Where id = 277741;
go
Select *
Into ConjoinedData
From Table1
Union
Select *
From Table2
Where id Not In (Select id From Table1);
Select *
From ConjoinedData
Where id = 27741;

Resources