First, let me preface with; my SQL knowledge is limited, obviously.
I'm trying to draw data from one table and insert it into another table. To be more specific, draw data that only meets specific criteria and place it with data that only meets specific criteria.
table_03.index = foreign index table
table_03.emailAddress = Foreign email address
table_91.emailIndex = the index number of the foreign index
table_91.emailAddress = Where I want the data to go.
What I've tried is:
SET #varEmail = form_91.emailIndex;
SET #newEmail = SELECT form_03.emailAddress FROM form_03 WHERE CONVERT(form_03.index, INT) = CONVERT(form_91.emailIndex, INT);
UPDATE form_91 SET emailAddress = #newEmail WHERE form_91.emailIndex = table_03.index;
...told you it wasn't going to be pretty.
Messing around with this combination, I've been able to produce a "NULL" value in the desired location, but not actually pull the correct email address.
Basically,
if [table_91.emailIndex] == [table_03.index]
then update [table_91.emailAddress]
with [table_03.emailAddress]
... simple right?
All help is certainly appreciated. Thank you ahead of time.
Related
For example, if I want to frequently update a user's score during a session,
is there a more performant way than - UPDATE score FROM [databaseName] WHERE name = [username]
I feel like I should not be continuously needing to search through the entire database when the value's location has been previously found.
Thanks in advance
You wouldn't search through the entire database if you had an index on name:
create index idx_<table>_name on <table>(name);
where <table> is the name of the table you are referring to, not the database.
Incidentally, if you wanted to update the table, you would use update, not select. But update can still use the index.
You can use properties here you can search and update only the database when you need it.
private int score;
public int Score
{
get
{
score = searchDatabase();
return score;
}
set
{
UpdateDatabase(value);
score = value;
}
}
Debug.Log(Score);
The only thing that comes to mind that resembles what you mention as the value's location, is the value of the primary key column of the row with the value that you search for.
So, if the primary key in the table is id and you only have the name of the person, you can query once to get the row's id:
SELECT id FROM tablename WHERE name = ?
and then use that id in all subsequent updates:
UPDATE tablename SET score = ? WHERE id = ?
This is the fastest way to do the updates.
I have a table with 400.000 rows and I added a new column on it. That table has an index id and I want to update each row with a different value that I have already calculated.
For example, I have the update statements like these:
UPDATE TABLE SET SECOND_NAME = 'Alfred' WHERE id = 510675;
UPDATE TABLE SET SECOND_NAME = 'Pedro' WHERE id = 123123;
UPDATE TABLE SET SECOND_NAME = 'Robert' WHERE id = 123123;
SECOND_NAME is the new column that I want to populate, lets say I have around 400.000 update statements, is there a way to massive update them in a faster way? If not, is there a way to know beforehand how long it could take to update them in that way?
I think your best bet, is probably to use an EXTERNAL TABLE. This AskTom answer basically gives you everything you need: AskTom. However, to customize it for your question. You could take the information you have, save it to a CSV and load it in as follows.
CREATE TABLE new_names (id NUMBER(10) PRIMARY KEY, second_name VARCHAR2(50))
ORGANIZATION EXTERNAL
(type oracle_loader
default directory data_dir
access parameters ( fields terminated by ',' )
location ('names.csv')
)
/
UPDATE (SELECT t.second_name empty_name, n.second_name loaded_name
FROM table_name t
INNER JOIN new_names n ON t.id = n.id)
SET empty_name = loaded_name;
/
All you need to do to make that work is create the directory and place your file in it.
After you are done, you can drop the the external table.
I have a table(ENTITY) that needs to be updated based on an ID(FUNNCODE) but the ID(FUNNCODE) is linked between two other tables(from JOINT then to POSITION)
and is independent of where the data is at(table NEORSD). The only parameter I can bind is the position name between the NEORSD table and POSITION table. When I place my LIKE statement into the where clause I get an error in return. If anyone can point me in the right direction it would be greatly appreciated!
Tables:
NEORSD: Contains the range information and 'position name(= Tag_No)'
ENTITY: Needs to update and accept the range information (Holds FUNCCODE)
JOINT: Holds FUNCCODE(named POSFUNCCODE) and corresponding POSCODE
POSITION: Contains POSCODE and 'position name(=POSID)'
UPDATE ENTITY
SET
RANGE0 = (
SELECT RANGE0
FROM NEORSD_1199
WHERE Tag_No like ('%PIT%'))
WHERE
FUNCCODE = (
SELECT POSFUNCCODE
FROM JOINT
WHERE POSCODE = (
SELECT POSCODE
FROM POSITION
WHERE POSID like ('%PIT%'))
If NEORSD_1199 has more than one row with a tag_no like '%PIT%', which NEORSD_1199.RANGE0 value should it use to update ENTITY.RANGE0?
This is the db engine's problem with your SQL.
To better understand, read the SQL backwards:
First you're getting a list of every Position Code from the POSITION table where the Position ID is like '%PIT%'. That might be one code, and it might be one hundred codes.
Then you're getting every Position Function Code from the JOINT table where the Position Code is in the list of Position Codes you just gathered. Again, could be one, could be a hundred.
Then you're getting a list of all values of RANGE0 from the NEORSD1199 table where Tag_No is like '%PIT%'. Again, this could be one value, or a list of one hundred.
Then, you're getting every row from the ENTITY table where the Function Code is in the list of Position Function Codes you gathered from the JOINT table (step 2 above), and you're updating RANGE0 in each of these rows to the value you captured in step 3.
The problem is that the 'value' returned in step 3 could be a list of values. If
NEORSD1199 has four rows where tag number is like '%PIT%'
(e.g. PIT01,PIT02,PIT03,APIT00), and each of those rows has a different
RANGE0 (e.g. 1,2,3,99), then which of those four values should the DB engine use to update RANGE0 in the rows in the ENTITY table?
Thank you to #SQLCliff for the questions that help to find the solution. I created an ID column inside my NEORSD table, created a temporary table holding the link between FUNCCODE and the ranges in NEORSD. Then I updated ENTITY using a join on. I can insert the where clause at the end of the temporary table for filtering if needed. Since it is a mass update I no longer require a where clause. My brain just likes making things more complicated than they need to be XD
with t as(
select f.funccode as funccode ,n.range0, n.range100
from func as f
join NEORSD_1199_With_Ranges_Updated as n on n.id = f.poscode or n.id =f.devcode
/* WHERE nessecrary ;P*/)
update entity
set
range0 = t.range0,
range100 = t.range100
from entity as e
join t on e.funccode = t.funccode
I want to add another row in my existing table and I'm a bit hesitant if I'm doing the right thing because it might skew the database. I have my script below and would like to hear your thoughts about it.
I want to add another row for 'Jane' in the table, which will be 'SKATING" in the ACT column.
Table: [Emp_table].[ACT].[LIST_EMP]
My script is:
INSERT INTO [Emp_table].[ACT].[LIST_EMP]
([ENTITY],[TYPE],[EMP_COD],[DATE],[LINE_NO],[ACT],[NAME])
VALUES
('REG','EMP','45233','2016-06-20 00:00:00:00','2','SKATING','JANE')
Will this do the trick?
Your statement looks ok. If the database has a problem with it (for example, due to a foreign key constraint violation), it will reject the statement.
If any of the fields in your table are numeric (and not varchar or char), just remove the quotes around the corresponding field. For example, if emp_cod and line_no are int, insert the following values instead:
('REG','EMP',45233,'2016-06-20 00:00:00:00',2,'SKATING','JANE')
Inserting records into a database has always been the most common reason why I've lost a lot of my hairs on my head!
SQL is great when it comes to SELECT or even UPDATEs but when it comes to INSERTs it's like someone from another planet came into the SQL standards commitee and managed to get their way of doing it implemented into the final SQL standard!
If your table does not have an automatic primary key that automatically gets generated on every insert, then you have to code it yourself to manage avoiding duplicates.
Start by writing a normal SELECT to see if the record(s) you're going to add don't already exist. But as Robert implied, your table may not have a primary key because it looks like a LOG table to me. So insert away!
If it does require to have a unique record everytime, then I strongly suggest you create a primary key for the table, either an auto generated one or a combination of your existing columns.
Assuming the first five combined columns make a unique key, this select will determine if your data you're inserting does not already exist...
SELECT COUNT(*) AS FoundRec FROM [Emp_table].[ACT].[LIST_EMP]
WHERE [ENTITY] = wsEntity AND [TYPE] = wsType AND [EMP_COD] = wsEmpCod AND [DATE] = wsDate AND [LINE_NO] = wsLineno
The wsXXX declarations, you will have to replace them with direct values or have them DECLAREd earlier in your script.
If you ran this alone and recieved a value of 1 or more, then the data exists already in your table, at least those 5 first columns. A true duplicate test will require you to test EVERY column in your table, but it should give you an idea.
In the INSERT, to do it all as one statement, you can do this ...
INSERT INTO [Emp_table].[ACT].[LIST_EMP]
([ENTITY],[TYPE],[EMP_COD],[DATE],[LINE_NO],[ACT],[NAME])
VALUES
('REG','EMP','45233','2016-06-20 00:00:00:00','2','SKATING','JANE')
WHERE (SELECT COUNT(*) AS FoundRec FROM [Emp_table].[ACT].[LIST_EMP]
WHERE [ENTITY] = wsEntity AND [TYPE] = wsType AND
[EMP_COD] = wsEmpCod AND [DATE] = wsDate AND
[LINE_NO] = wsLineno) = 0
Just replace the wsXXX variables with the values you want to insert.
I hope that made sense.
I've been researching the concat function extensively but hit a wall creating a temp table. I have two columns: ID (ex. 4323) and Source (ex. PHI). I want to add a column that includes a prefix of "API-" to the ID column (ex. API-4332). Anyone have any insight?
All rows?
UPDATE TheTable
SET id = 'API-'||id;
Or,
UPDATE TheTable
SET id = CONCAT('API-',id);
EDIT:
Your problem statement led me to believe you wanted to create a new column ("I want to add a column..."). Sorry for the confusion. I have changed the answer to update the ID column.