I have a table that I need to Update by adding a new field.. I can alter the table and update each row . but is there a way of appending the result of a query to the table? ( I know that the result will have the same number of rows)
EDIT: So let me make it clear
I have
table1 | col1,col2
I generate another single column table
table2 | col1
I want
table3 | table1.col1,table1.col2,table2.col1
By the way table1 & table2 have no common fields so I cant join them meaningfully.
I think so. I haven't tested this, but what I can find it seems that you can use a SubQuery to do something along the lines of
UPDATE Table1 SET Column1 = Column1 & (SELECT Column2 FROM Table2 WHERE xxxx)
Related
I'm trying to use an update statement using a combination of unique keys. These unique key are used in the where clause which are dragged from a select query using a sub query. Not sure how to use these two together.
Query looks something like this
1 UPDATE table1
2 SET column1 = .. , column2 = ..
3 WHERE TOOL_NO, TOOL_SERIAL_NO IN
4 (SELECT TOOL_NO, TOOL_SERIAL_NO FROM TABLE2 WHERE condition)
TOOL_NO and TOOL_SERIAL_NO are the unique keys and are dependent on each other.
For example Tool_No will have multiple Tool_Serial_No.
The problem is with line number 3. Not sure how to use two fields in the same where clause which depends on the same sub - query,
Any help is appreciated.
You're quite close - just enclose those columns in line #3 into brackets:
UPDATE table1
SET column1 = .. , column2 = ..
WHERE (TOOL_NO, TOOL_SERIAL_NO) IN
(SELECT TOOL_NO, TOOL_SERIAL_NO FROM TABLE2 WHERE condition)
You will also need to correlate the sub query against table2 with the outer where in table1 SQL.
While matching multiple columns in where clause together always enclose it into brackets. So in your case WHERE TOOL_NO, TOOL_SERIAL_NO should be WHERE (TOOL_NO, TOOL_SERIAL_NO)
UPDATE table1
SET column1 = .. , column2 = ..
WHERE (TOOL_NO, TOOL_SERIAL_NO) IN
(SELECT TOOL_NO, TOOL_SERIAL_NO FROM TABLE2 WHERE condition)
I have two tables that look like this:
table1:
table2:
and I'd like to merge them, to create a table with a header like this:
variablenname | mean | stddev | ms_form | dependent | fvalue | probf
The first nine rows should be the first table with empty values in the last 3 columns followed by the three rows from the second table with empty values in the first 4 columns. I'm sorry I can't post pictures or a third link, but my reputation is too low. I hope it's understandable.
I tried to create a new table and select everything from both with union but that didn't work. Can anybody help me?
Thanks!
You can use ALTER to add the last 3 columns to table 1. From there, INSERT table 2 onto table 1. So something like this.
ALTER table1 ADD dependent varchar(16)
ALTER table1 ADD favalue varchar(16)
ALTER table1 ADD probf varchar(16)
INSERT INTO table1(dependent, fvalue, probf)
SELECT * FROM table2
You can use a combination of joins and unions
LEFT JOIN the two tables in first query on fields that won't have a match
then UNION the second query with a RIGHT JOIN of the same tables.
The LEFT JOIN will only show results from the first table, then the RIGHT JOIN will only show results from the second
Something Like:
SELECT * FROM table1 as t1
LEFT JOIN table1 as t2 on t1.variablenname = t2.fValue
UNION
SELECT * FROM table1 as t1
RIGHT JOIN table1 as t2 on t1.variablenname = t2.fValue;
This is how I would like the final table to look like:
I am trying to execute a query which is something like:
update table set column=(select column1 from table1);
I just want to store the value from other table to my column
but when i try my sql query it says
ERROR 1242 (21000): Subquery returns more than 1 row
definitely this means my table1 contains more than 1 row so i want to know that is there any way to store data into column from other table with multiple row.
or basically saving content of other table as a text something like
update table set column='Data in text from other table';
You probably need a correlation clause:
update table
set column = (select column1 from table1 where table.col = table1.col);
You need to decide what column(s) are used for the correlation.
it will work as i am getting your requirement.Please let me know if your requirement is other i ll make changes in query
update table_name t1
inner join table1 t2 on t1.id =t2.id
set column =column1
Your nested query returns multiple rows so you encountered this error.
Try in this way
UPDATE FirstTable
SET FirstTable.ColumnName =tbl2.ColumnName
FROM SecondTable tbl2 WHERE tbl2.Id = FirstTable.Id
There should be a common id or something that will help to find exact row.
I have created a new column in my table(table1) . I am trying to populate it with data from another table, table2.
Table1 has a column called 'Name'. 'Name' contains a substring indicating the language of the column. I wish to compare this substring with the 'Language' column of table2, which contains the substring in the name column and insert the corresponding LanguageID into my new column.
So, for instance :
table1
Name
xxXxxxXxxxxxzxzxzxz xxxazxzxxXXXZxxzxzx 2183909213 ENG-UK nfjksdnfnd 723984782347
and table2 :
table2
Language | ID
ENG-uk | 1
In the table1 name column, the string before and after the Language can take any form, a varying number of characters. The language will always have a space before and after it.
So, I want to end up with :
table1
Name | LanguageID
xx... | 1
I have this query which I believe should work :
INSERT INTO table1 (LanguageID)
SELECT t2.ID FROM table2 t2, table1 t1 WHERE CHARINDEX(LOWER(t2.Language), LOWER(t1.Name)) != null
The problem is, when I run this...."(0 row(s) affected)", which should not be the case.
Does anyone have any ideas ?
The reason that you don't get any matches at all is that you can't use the != operator to compare null values, you have to use is not null for that.
However, that will give you a very big result, as the return value from charindex is never null. When the string isn't found it returns zero, so that is what you should compare against.
Also, you can't insert columns, you have to first add the column to the table, then update the records:
update t1
set LanguageID = t2.ID
from table1 t1
inner join table2 t2 on charindex(lower(t2.Language), lower(t1.Name)) != 0
1st, CHARINDEX returns 0 when search string does not exist. It doesn't do null.
See http://msdn.microsoft.com/en-us/library/ms186323.aspx
2nd, I think you should UPDATE, not INSERT.
example (this won't work correctly if t2.Language is not UNIQUE):
UPDATE table1 t1
SET t1.LanguageID = (SELECT t2.ID from table2 t2 where CHARINDEX(LOWER(t1.Name), LOWER(t2.Language))>0)
where exists (SELECT t2.ID from table2 t2 where CHARINDEX(LOWER(t1.Name), LOWER(t2.Language))>0)
You should consider adding individual columns for the first table information. Otherwise, you will end up with Performance Issues due to the SubString Operation.
It's clear from the first table that the table schema is not Normalized. Moreover, the First table schema is not suitable for any Search/Sorting operations.
I would like to know if there is anyway I can compare two columns in SQL Server.
The two columns are located in two different tables.
When the column 1's value is smaller than the column 2's value:
I want to replace the value of the column 1 with the value of the column 2.
update table1 t1
set t1.col1 = (select t2.col2
from table2 t2
where t2.id = t1.id
and t1.col1 < t1.col2)
Something like that should do it easily.
The only tricky point I see is matching the row from table2 to the row from table1. In my example, I supposed both tables share an unique "id" column which enables easy matching. Modify the query with something more appropriate.
You should be able to do something like this:
update tablename set column1=column2
from table1 inner join table2 on joincondition
where column1 < column2;
Hard to be more spesific without the actual table structure.