Insert column from one table to another after copying - sql

I am trying to run this query on SQL Server Management Studio. But I am getting an error. What I did initially was to copy a table from another table. the table from which I am copying is the painting table and I am trying to make a replica of that painting table, named as painting1. But the task was to copy all the columns except for 1.
painting
col1, col2, col3, col4
The query used for this task is as below
SELECT col1, col2, col3
INTO PAINTING1
FROM PAINTING
After the creation of painting1, it has col1, col2 and col3 from painting table
Now the task was to insert the one remaining column into the replica table, so what I did to insert that remaining column to insert the replica table painting1 from painting table.
INSERT INTO PAINTING1 (col4)
SELECT col4 FROM PAINTING
And I run into this error:
invalid column name 'col4'
Any suggestions on what I might be doing wrong,

First add the new column in the table PAINTING1:
ALTER TABLE PAINTING1 ADD col4 INTEGER;
Change INTEGER to the data type of col4 that you want.
What you actually need is to update PAINTING1 with the values of the column col4 from PAINTING.
This can only be done if one of the columns col1, col2 or col3 is unique, like a primary key, so it can be used as the link between the 2 tables:
UPDATE p1
SET p1.col4 = p.col4
FROM PAINTING1 p1 INNER JOIN PAINTING p
ON p.col1 = p1.col1
I used col1 as the link between the tables.
If col1 is not unique, use all 3 columns in the ON clause (if their combination is unique):
ON p.col1 = p1.col1 AND p.col2 = p1.col2 AND p.col3 = p1.col3

You're creating the table with col1, col2 and col3. How can it be anything but an error to refeference col4 that doesn't exist?
You can of course add additional columns to the table at any time, but what you could do is add col4 as part of your select into.
SELECT col1, col2, col3, '' as Col4 /* use / cast whatever data type it should be */
INTO PAINTING1
FROM PAINTING

Related

How to check array values using IN operator?

I have a table where each cell in columns has an array list.
COL1 COL2
row1: ('hi','hello') ('hi','hello')
row2: ('hihi','below') ('pi','by')
I am trying to use an in operator on such data but my query is not returning anything.
Query: select * from table where col1 in ('hi')
Also, if I have another table (table2) that looks like this:
col3
____
'hi'
'bye'
'guy'
and I want to use the same concept where I'll be checking if 'hi','bye','guy' from col3 exists in col1 row1
My Query: select * from table2 where col3 in (select col1 from table1);
The first argument is the "inside" one, the second one is the group of values.
You are comparing col1 (a tuple of 2 values) to a unique value:
I think that could help you: col1[0] -> gets the first value
select * from table where col1[0] in ('hi')
Whether you want to compare both, you can use:
select * from table where col1[0] in ('hi') or col1[1] in ('hi')

MSSQL trigger: fill in rest of table based

I have a table that looks like this:
Date
Col1
Col2
Col3 = Col1*Col2
03-03-2021
100
2
200
03-03-2020
200
1
200
04-20-2029
NULL
NULL
NULL
I want to create 2 triggers that do the following:
After u user inputs a new row, if col1 and col2 are null, search another table for the values of col1 and col2. If col1 is null, only search for col2. If col2 is null, only search for col1.
Upon update of col1, col2, recalculate col3.
Am having trouble getting started on this. Could someone point me in the right direction? Thanks

insert into fails to insert selected data

I have been working on breaking up a 68GB table into a more normalized structure for the last few weeks, and everything has bee going smoothly until today.
I am attempting to move a select few columns from the big table into the new table with this query:
insert into [destination] (col1, col2, col3...)
select col1, col2, col3
From [source]
where companyID = [source].companyID
I receive the message, (60113678 row(s) affected), but the data was not inserted into the destination, and the data in the source table hasn't been altered, so what has been affected, and why wasn't any of the data inserted into the destination?
The code you seem to want to execute is:
update d
set col1 = s.col1,
col2 = s.col2,
col3 = s.col3
from destination d join
sources s
on s.companyID = s.companyId;
The code you have written is equivalent to:
insert into [destination] (col1, col2, col3...)
select s.col1, s.col2, s.col3
From [source]
where s.companyID = s.companyID;
The where is equivalent to s.companyID is not null. Hence, you have inserted all 60,113,678 rows from source into new rows in destination.
Obviously, one moral of the story is to understand the difference between insert and update. More importantly, qualify all columns names in a query. If you had done so, your query would have have failed at source.CompanyID = destination.CompanyId -- and you wouldn't have to figure out how to delete 60,113,678 new rows.

sql query help, don't know how to term this

I'm not too sure how to term this question. Stay with me.
I want to create a query to retrieve the rows with x's in the row column in the table(this is a dummy table):
row col1 col2 col3
x 1 a c
x 2 b c
x 3 a c
4 b d
x 5 f g
So the way I want my query to work is to retrieve all rows where the value for col2 doesn't have a row in col3 where the value is d. Ie. value 'a' will be retrieved because it only has c's for col3, but value 'b' wont be retrieved because it has a d in col3 on the 4th row down.
I hope this is easy to understand.
Ps. Once I know how to do the query I expect I'll know how to phrase the title and will redo it. (although now I think about it, maybe this title is best for all those with questions like mine)
Based on everything you've provided, I'll have to make a few assumptions. Short of doing a self-join on the table, you could take advantage of an identity key on the table to use a simple query as such:
SELECT * FROM TABLE_NAME
WHERE id NOT IN (SELECT id FROM TABLE_NAME WHERE col3 = 'd');
If you don't have an identity key on the table, you could do something more like this:
SELECT * FROM TABLE_NAME
WHERE col2 NOT IN (SELECT col2 FROM TABLE_NAME WHERE col3 = 'd');
Both of these queries will return all tuples in the relation that have elements of col2 that are not in tuples where col3 contains 'd'.
Think of how you might do this manually, and put that into your query.
To get all the col3 values you'd write:
SELECT DISTINCT col3 FROM TABLE_NAME
Use that to filter rows from your selection via a not in clause:
SELECT
col1, col2, col3 FROM TABLE_NAME
WHERE col2 not in (SELECT DISTINCT col3 FROM TABLE_NAME);

Avoid duplicates on import of updated excel-sheets. Unique-Index can only hold 10 fields max

I am facing the following situation:
I import an Excel-Sheet, then some columns are modified (e.g. "comments")
After a while, I would receive an updated Excel-Sheet containing the records from the old Excel-sheet as well as new ones.
I do not want to import the records that already exist in the database.
Step-by-Step:
Initial Excel-sheet
col1 col2 comments
A A
A B
After import, some fields will get manipulated
col1 col2 comments
A A looks good
A B fine with me
Then I receive an excel sheet with updates
col1 col2 comments
A A
A B
A C
After this update-step, the database should look like
col1 col2 comments
A A looks good
A B fine with me
A C
I was planning to simply create a unique index on all fields that won't get manipulated, so only the new records will get imported. (sth like
ALTER TABLE tbl ADD CONSRAINT unique_key UNIQUE (col1,col2)
My problem now is that Access somehow only allows composite indices of max. 10 fields. My tables all have around 11-20 cols...
I could maybe import the updated xls to a temp. table, and do s.th like
INSERT INTO tbl_old SELECT col1,col2, "" FROM tbl_new WHERE (col1,col2) NOT IN (SELECT col1,col2 FROM tbl_old UNION SELECT col1,col2 FROM tbl_new)
But I'm wondering if there isn't a more straigt-forward way...
Any ideas how I can solve that?
Try the EXISTS condition:
INSERT INTO tbl_old (col1, col2, comments)
SELECT col1, col2, Null
FROM tbl_new
WHERE NOT EXISTS (SELECT col1, col2 FROM tbl_old WHERE tbl_old.col1 = tbl_new.col1 AND tbl_old.col2 = tbl_new.col2);
Considering you will use SQL approach:
INSERT INTO table_old (col1, col2)
SELECT col1, col2 FROM table_new
EXCEPT
SELECT col1, col2 FROM table_old
:)
It will insert null in comments column though. Use this:
INSERT INTO table_old
SELECT * FROM table_new
EXCEPT
SELECT * FROM table_old
to avoid null values. Also both tables have to have the same amount of columns. For Oracle go with minus instead of except. Equivalent SQL query would be made with LEFT OUTER JOIN.
INSERT INTO table_old (col1 , col2)
SELECT N.col1, N.col2
FROM table_new N
LEFT OUTER JOIN table_old O ON O.col2 = N.col2
WHERE O.col2 IS NULL
Which will also provide null values to comments column, as we are inserting only col1 and col2. All inserts tested on provided table examples.
I would just put PK ID column in those tables.