Insert column from one table to another - sql

I want to copy a column from one table to another.
The number of rows is equal in both tables. The values I want to copy from table2 to table1 are unique. I've tried a few thing, but none so far work. My code is:
insert into alleoppdragpunkter3
select Idtall
from IDtall
Msg 2809, Level 16, State 1, Line 2
The request for procedure 'IDtall' failed because 'IDtall' is a table object.
I would like my column from table2 to be in table1.

You can try below-
insert into alleoppdragpunkter3(col1,col2,col3,....)
select col1,col2,col3,.... from IDtall

You do not copy columns between tables. You can insert rows and update columns.
Perhaps you want:
update p
set p.<col> = i.<col>
from alleoppdragpunkter3 p join
idtall i
on p.? = i.?;
The ? is for the column that specify the join conditions between the tables. The set references the column you want to update and which value to take.

Related

Copy all the values from table1 to table2 only if condition met

Let's say var_Environment = QA1,QA2.
I have two tables table 1 and table 2. Table 1 has some column name var_Environment =QA1; where as table 2 does have the same column name but no values
Now my task is, I need to copy all the values from table1 to table2 only if condition meets var_Environment =QA2 How will I perform this operation ? Can I use Union All with some condition? Can someone help?
You can use INSERT INTO SELECT like below.
The WHERE clause may need adjustment, so run the the SELECT query by itself to check if the correct rows are selected.
INSERT INTO table2
SELECT var_Environment
FROM Table1
WHERE var_Environment LIKE '%QA2%'

How can I insert query value to the column with existing data?

I want to insert this query into the new column and below is my query:
INSERT INTO T_SG_WICA_POL_DATA (SubClassGroup)
SELECT M.[Subclass_Main]
FROM [WICA subclass mapping] AS M
LEFT JOIN T_SG_WICA_TRANSACTION_VIEW ON M.[Subclass Code] = Subclass;
But I received this error when trying to run it:
Cannot insert the value NULL into column 'AccountTenure', table 'analytics.dbo.T_SG_WICA_POL_DATA'; column does not allow nulls. INSERT fails.`
I want to insert to SubClassGroup column not AccountTenure column so I'm not sure why I get this error and how can I insert the value to SubClassGroup column?
By starting the query with INSERT INTO you are telling the system to insert an entirely new row in the table. You would need to provide a value for at least every column that does not define a default or auto increment value and that is not nullable.
INSERT INTO T_SG_WICA_POL_DATA (SubClassGroup, AccountTenure, ...)
SELECT M.Subclass_Main, M.AccountTenure, ...
FROM [WICA subclass mapping] AS M
If you want to update the value of one or more columns in one or more existing table rows you can use an update query:
UPDATE T_SG_WICA_POL_DATA SET SubClassGroup = M.Subclass_Main --, AccountTenure = M.AccountTenure, ...
FROM WICA subclass mapping AS M
JOIN T_SG_WICA_TRANSACTION_VIEW AS T ON M.[Subclass Code] = T.Subclass
--WHERE some_other_condition
Note that I changed the left join into a regular join since a left join will not add any constraints to the query, causing the update to apply to all rows instead of only the ones where a record is present in T_SG_WICA_TRANSACTION_VIEW with a matching Subclass code.

Update all rows for one column in a table with data in another table

I appreciate any advice on this..
I have two tables where I have to update a column in my primary table with data that resides in another secondary table. I cannot rely on views, etc as this data has to be able to be edited by the user in APEX in the future. I am basically pre-populating the data for the users to reduce their manual entry.
Primary Table = Table 1
Secondary Table = Table 2
Columns to be updated in Table 1 = FTE_ID, ACCOUNT_TYPE
Columns where the data will come from Table 2 = R_ID, ACCOUNT_TYPE
Common column in both tables = TABLE1.FID AND TABLE2.FID
Here is what I have tried, but I get "single-row subquery returns more than one row" because there are multiple table1.fid rows in table1. I basically want to perform this update for ALL rows where TABLE1.FID = TABLE2.FID.
Here is my attempt:
UPDATE TABLE1
SET TABLE1.FTE_ID =
(SELECT TABLE2.R_ID FROM TABLE2 WHERE TABLE1.FID = TABLE2.FID);
Error:
single-row subquery returns more than one row
Thanks for your help,
You can fix the proximate problem by using aggregation or row number:
UPDATE TABLE1
SET TABLE1.FTE_ID = (SELECT MAX(TABLE2.R_ID)
FROM TABLE2
WHERE TABLE1.FID = TABLE2.FID
);
The subquery can only return one row; it is an "arbitrary" value from the possible matching values.
If the field is a character field and you want all matching values, then perhaps listagg is more appropriate:
UPDATE TABLE1
SET TABLE1.FTE_ID = (SELECT LISTAGG(t2.R_ID, ',') WITHIN GROUP (ORDER BY t2.R_ID)
FROM TABLE2 t2
WHERE TABLE1.FID = t2.FID
);

SQL: How to update an empty column with pre-defined set of values

I have a table with, let's say, 100 records. The table has two columns. The first column (A) has unique values. The second column (B) has NULL values
For 4 elements from column A I'd like to associate some earlier defined values, and they are unique as well.
I don't care about which value from column B will be associated with the value from column A. I'd like to associate 4 unique values with another 4 unique values. Basically, like I'd cut and paste a block of values from one column to another in excel.
How can I do it without using cursors?
I'd like to use one Update statement for ALL rows instead one Update statement for EVERY row as I do now.
Try this:
UPDATE t
SET ColumnB = BValue
FROM Table t
INNER JOIN
(
SELECT 1 AValue, 'Mouse' BValue UNION
SELECT 2, 'Cat' UNION
SELECT 3, 'Dog' UNION
SELECT 4, 'Wolf'
) PreDefined ON(t.ColumnA = PreDefined.AValue)
Use any number you want in the 'PreDefined' table, as long as they are unique and within the range of values in columnA of your original table.
If you are only trying to fill a table for testing purposes, I guess you could:
A) Use the value from Column A itself (as it is already unique).
B) If they are to be different, use some function on the column A's value to obtain a column B value (something simple, like (ColumnA * 10), and this would give youA)
C) Create a temp table with a "dictionary" setting a B value for each possible A value, and then update the rows desired on your table looking up from values on this dictionary table.
Anyway, if you explain a little further your purpose it will be easier to try suggesting you a solution.
if your animal data is already in a database table, then you can use a single update statement like this:
update target_table t4
set columnb = (
select animal_name
from (select columna, animal_name
from (select rownum rowNumber, animal_name from animal_table) t1
join (select rownum rowNumber, columna from target_table t1 where columnb is null) t2
on t1.rowNumber = t2.rowNumber
) t3
where t4.columna = t3.columna
)
;
this works by selecting a sequence number and animal name from the source table, then selecting a sequence number and columna value from your target table. by joining those records on the sequence number you guarantee you get exactly 1 animal name for each columna value. you can then join those columna-to-animal records to your target table to do an update of columnb.
for more background on updating one table from values in another, you might consider the solutions presented here: Update rows in one table with data from another table based on one column in each being equal. the only difference is that in your example, you do not have any column that matches between your target table and your animal names table, so you need to use the rownum to create an arbitrary 1-to-1 matching of records.
if your unique options are in a text file or spreadsheet, then you can format them into a fixed-width space-padded string and pick the one you want using the rownum index like so:
update table_name
set columnb = trim(substr('mouse cat dog wolf ', rownum*6-6, 6))
where columnb is null;

How to copy column values from one database to empty column in other database?

I have two databases.
Alarm
TMP
I have a table in Alarm, where in a table there is one empty column with null values.
And I have a single column table in TMP.
I want to copy this single column values to my table in Alarm database.
What I tried so far is,
update [Alarm].[dbo].[AlarmDetails] set Alarm_Message = (select * from [TMP].[dbo].[AlarmDetails$])
where 1=1
The error is
Subquery returned more than 1 value.
Please note this,
NOTE: There is no id column in source table. Only one table & one column, Alarm Message.
I know the cause of error, but how should I modify my SQL.
Thank You.
Here's an example of copying a column:
update dst
set Alarm_Message = src.AlarmMessage
from Alarm.dbo.AlarmDetails dst
join TMP.dbo.AlarmDetails src
on dst.id = src.id
You did not specify how the tables are related, so I assumed they both have an id column.
You need something like this.
update t1
set
t1.<something1> = t2.<something2>
from
[Alarm].[dbo].[AlarmDetails] t1
join [TMP].[dbo].[AlarmDetails] t2 on (t1.<cols1> = t2.<cols2>)
UPDATE results SET results.platform_to_insert = (
SELECT correct_platform
FROM build
WHERE results.BuildID=build.BuildID LIMIT 1
);