How to insert data from dump file to another table with different data structure? - database-schema

I have two tables tb1 (Columns c1, c2, c3, c4, c5...) and tb2 (Columns C1, C2, C3, C4, CN4, C5, CN6), Tb2 was the same schema description as tb1, but I altered tb2 adding more columns, my question is: could I dump data from tb1 and then insert it to tb2 even this table have more columns using mysqldump process?

Instead of inserting from dump file, you can just do a insert into ... select from like below. Point to notice: this would work only if CN4 and CN6 are nullable columns (they don't have a not null constraint on them).
insert into tb2(C1, C2, C3, C4, CN4, C5, CN6)
select C1, C2, C3, C4, null, C5, null
from tb1;

Related

Getting error "could not identify an equality operator for type json" with insert on conflict

When doing a bulk upload into a table that has a json column, I am getting the error "could not identify an equality operator for type json". The json column is not part of any comparison (as far as I can tell), so I am mystified as to why I am getting the error.
Looking at the data being inserted, this everything appears correct.
The table is:
create table foo (
c0 serial not null,
c1 int4 not null,
c2 timestamp not null,
c3 timestamp not null,
c4 bool not null
c5 char(1) not null default 'I',
c6 json not null default '[]'::json,
constraint foo_pkey primary key (c0)
);
create unique index foo_idx on foo using btree (c1, c2);
And the python code using psycopg2 is:
def upsert_foo(cursor, data):
sql = """
INSERT INTO foo
(c1, c2, c3, c4, c5, c6)
VALUES
(%s,%s,%s,%s,%s,%s)
ON CONFLICT (c1, c2)
DO UPDATE SET
c3 = %s,
c4 = %s,
c5 = %s,
c6 = %s;
"""
execute_batch(cursor, sql, data)
The full error is:
psycopg2.errors.UndefinedFunction: could not identify an equality operator for type json
The problem was the trigger on the table that had a when clause. This failed because the table has a json column, which cannot be compared. By removing the when clause from the trigger, the problem went away.

Using CASE in PostgreSQL to SELECT different FROMs

I'll try to create a query where the result can be from two different tables depending on the size of the first table.
Is something comparable even possible?
SELECT
CASE WHEN COUNT(table1.column1) > 5
THEN
column1,
column2,
column3
FROM table1
ELSE
column1,
column2,
column3
FROM table2
END
With this code I got something like this:
ERROR: syntax error at or near ","
LINE 4: column1,
with c (c) as (select count(c1) from t)
select c1, c2, c3
from t
where (select c from c) > 5
union all
select c1, c2, c3
from r
where (select c from c) <= 5
The corresponding columns must be of the same type. Or be casted to the same type.
WITH clause
UNION clause

Crystal Reports, SUM FORMULA for union of 2 tables that creates too many records

I've created a crystal report based on 1 table with multiple database fields and it works perfectly for what I need, however, when I add (via union) another table it duplicates all my records from the first table.
For example:
Table1 A1, Table1 B1, Table1 C1
Table1 A2, Table1 B2, Table1 C2
Is perfect.
I add D from table 2 and it begins looking like this:
Table1 A1, Table1 B1, Table1 C1 , Table2 D1
Table1 A1, Table1 B1, Table1 C1 , Table2 D2
Table1 A1, Table1 B1, Table1 C1 , Table2 D3
Table1 A1, Table1 B1, Table1 C1 , Table2 D4
Table1 A2, Table1 B2, Table1 C2 , Table2 D5
Table1 A2, Table1 B2, Table1 C2 , Table2 D6
Table1 A2, Table1 B2, Table1 C2 , Table2 D7
Table1 A2, Table1 B2, Table1 C2 , Table2 D8
The 2 tables are unioned correctly. Basically what I want to do here is add a formula that will sum the corresponding D's so I can go back to my original number of records and not have so many duplicates:
Table1 A1, Table1 B1, Table1 C1, SUM of table2 D
Table1 A2, Table1 B2, Table1 C2, SUM of table2 D
Any help is appreciated
Thank you
Create a group on A1 field then put B1,C1 #SumOfD in group header.Suppress the details section.
#SumOfD
Sum ({D Column}, {A1});

TSQL Bulk insert data while returning created id's to original table

I have two tables. One called #tempImportedData, another called #tempEngine.
I have data in #tempImportedData I would like to put this data into #tempEngine, once inserted into #tempEngine an id gets created. I would like that id to be placed back into #tempImportedData in the corresponding row. I believe this this the purpose of OUTPUT statement. I almost have a working copy please see below.
Declare #tempEngine as table(
id int identity(4,1) not null
,c1 int
,c2 int
);
Declare #tempImportedData as table(
c1 int
,c2 int
,engine_id int
);
insert into #tempImportedData (c1, c2)
select 1,1
union all select 1,2
union all select 1,3
union all select 1,4
union all select 2,1
union all select 2,2
union all select 2,3
union all select 2,4
;
INSERT INTO #tempEngine ( c1, c2 )
--OUTPUT INSERTED.c1, INSERTED.c2, INSERTED.id INTO #tempImportedData (c1, c2, engine_id) --dups with full data
--OUTPUT INSERTED.id INTO #tempImportedData (engine_id) -- new rows with wanted data, but nulls for rest
SELECT
c1
,c2
FROM
#tempImportedData
;
select * from #tempEngine ;
select * from #tempImportedData ;
I've commented out two lines starting with OUTPUT.
The problem with the first is that it inserts all of the correct data into #tempImportedData, so the end result is that 16 rows exist, the first 8 are the same with a null value for engine_id while the third column is null; the remaining 8 have all three columns populated. The end result should have 8 rows not 16.
The second OUTPUT statement has the same problem as the first - 16 rows instead of 8. However the new 8 rows contain null, null, engine_id
So how can I alter this TSQL to get #tempImportedData.engine_id updated without inserting new rows?
You need another table variable (#temp) to capture the output from the insert and then run a update statement using the #temp against #tempImportedData joining on c1 and c2. This requires that the combination of c1 and c2 is unique in #tempImportedData.
Declare #temp as table(
id int
,c1 int
,c2 int
);
INSERT INTO #tempEngine ( c1, c2 )
OUTPUT INSERTED.id, INSERTED.c1, INSERTED.c2 INTO #temp
SELECT
c1
,c2
FROM
#tempImportedData
;
UPDATE T1
SET engine_id = T2.id
FROM #tempImportedData as T1
INNER JOIN #temp as T2
on T1.c1 = T2.c1 and
T1.c2 = T2.c2
;
#tempImportedData still has the old data still in it. The first OUTPUT statement seems to be inserting the right data in the new rows, but the old rows are still there. If you run a DELETE on #tempImportedData taking away all rows where engine_id is null at the end of your script, you should be left the correct eight rows.

T-SQL insert into tablewith values coming from QUERY + VARIABLE

I want to insert into table single record in one shot with values coming from QUERY + VARIABLE.
E.g.
Insert into Table2(c1,c2,c3,c4)
c1,c2,c3 are coming from select col1,col2,col3 from Table1
c4 is coming from variable #var
As the volume of data is huge, I can not have one insert and second update.
Thanks in advance.
That's a pretty straightforward insert:
insert Table2
(c1, c2, c3, c4)
select col1, col2, col3, #var
from Table1
Try this:
INSERT INTO Table2 (c1,c2,c3,c4)
SELECT col1, col2, col3, #var FROM table1