Update multiple tables using a Stored Procedure - sql-server-2008

I want use a stored procedure to update multiple tables in a db. Each table has a GUID as the PK and there are FK's between the tables.
For example, one table is "Tool" with a column ID (the guid) and another table is "Type" with ID as guid again. There is a column in Tool called "TYPE_ID" that is a FK to the table Type with Type's Guid stored in it. I want to first update the Tool table and then after, update the Type table based on that FK.
UPDATE Tool
SET Name=#Name, [Enabled]=#Enabled, TestMode=#TestMode, SerialNumber=#SerialNumber,
Andon=#Andon, ChimeZone=#ChimeZone, Number=#ToolNumber
WHERE ID=#ID
Update Type
SET Type=#Type
WHERE Tool.ID=#ID AND
Tool.TYPE_ID=Type.ID
I know that this code is incorrect for the second update, but this is the gist of how I would like to be able to do it. Is there a way to not have to SELECT the FK Guid, store it, and use it the next update? If that is the only way, how do I do that?

Write your second update like this, joining the Tool table to the Type table:
UPDATE ty
SET Type = #Type
FROM Tool to
INNER JOIN Type ty
ON to.TYPE_ID = ty.ID
WHERE to.ID = #ID

Actually you can UPDATE FROM clause:
UPDATE Type
SET Type=#Type
FROM Tool INNER JOIN TYPE
ON Tool.TYPE_ID=Type.ID
WHERE Tool.ID=#ID;
See http://msdn.microsoft.com/en-us/library/ms177523.aspx for full UPDATE syntax.

Related

insert into select with external key reference

INSERT INTO trees (preview)
select galleries.preview
from galleries,trees
where trees.id=galleries.idTree;
I am trying to move a column from a table to another, I have set an empty column with the same data type as the original.
Where idTree is equal to id to the destination table (in the source table idTree is foreign key reference to id on trees that is the destination).
The "select" works and give me back the right set of values (at least ordered by id and all) but the insert into part, do nothings and the field on trees is still empty. what am I doing wrong ?
INSERT INTO.. will create a new row; but you are looking to update the existing rows in trees table which has id. Try with UPDATE query instead:
UPDATE trees
JOIN galleries ON galleries.idTree = trees.id
SET trees.preview = galleries.preview

How to I functionally access an SQL table's table name?

I have a set of tables that I am conflating together. I want to be able to go back and access the 'raw' data later if I need to add more. The way I'm doing this by adding a reference column to the conflation table that will contain the table name of the table from which the data came from.
How can I access the names of my tables as I query from them?
EDIT: Details:
The table I'm creating looks like:
CREATE TABLE combined_things WITH OIDS AS
(SELECT
thing1.name
thing1.shape
FROM
public.thing1_source_table
UNION
SELECT
thing2.name
thing2.shape
FROM
public.thing2_source_table);
And I want to add the "source" field:
ALTER TABLE combined_things ADD COLUMN source_id character varying(100);
ALTER TABLE comnined_things SET COLUMN source_id = {table_name}
And I don't know how to pull the {table_name}
You could add them as a string constant when you create the table
CREATE TABLE combined_things WITH OIDS AS
(SELECT
thing1.name,
thing1.shape,
CAST('public.thing1_source_table' AS CHAR(100)) source_id
FROM
public.thing1_source_table
UNION
SELECT
thing2.name,
thing2.shape,
'public.thing2_source_table'
FROM
public.thing2_source_table);
Note that there is no way of casting the column to varchar

Adding column from one table to the other table in mysql

How to add or insert specific column from one table to other ? I tried writing like this
ALTER TABLE info_apie_zaideja
ADD SELECT info_apie_match.Rank AFTER 'Nick'
FROM info_apie_match;
or this
UPDATE info_apie_zaideja
ADD COLUMN SELECT info_apie_match.Rank AFTER 'Nick'
FROM info_apie_match;
but that did not work. Oh, and the table where I want to insert column is view table if that helps somehow. All answers will be appreciated.
You need to do this in two steps. First alter the table to add the new column:
ALTER TABLE info_apie_zaideja
ADD COLUMN Rank INT AFTER Nick;
Then fill it in by copying from corresponding rows in the other table:
UPDATE info_apie_zaideja AS z
JOIN info_apie_match AS m ON z.id = m.zaideja_id
SET z.Rank = m.Rank
I had to guess at the column that relates the two tables. Correct the ON clause to match your actual table relations.
Also, consider whether you really need the column in both tables. With this redundancy, you'll need to make sure that whenever you update one table, the other one is updated as well. Instead, you could just use a JOIN whenever you need the value from the other table.

inserting data into a new column of an already exsisting table

I added a new column to an already existing table, I just need to get data into this new column...I assume I can use a variant of the insert command, yet I do not know how to specify the column name and then how to order the values going into the new column.
Ok, after some conversation through the comments lets go to an answer.
I suppose your table is something like id, name, age, dateBirth, etc fields. But whoever create this table forget to add the gender for the registries. As you said that the new column is an sex enum('m', 'f') you will have to update every registry on this table one by one. Like this:
update matches set sex = 'm' where id = 1;
Pay attention that with this command I just updated the row on the table where the id=1 and Im assuming that id is your primary key. On the where caluse you have to put your primary key, otherwise you may update more then one column.
If your table has many registries there is a way that you can do it cuting down the heavy work (at least a little)
In order to update many rows at once you have to do an Update with a LIKE filter, you will set a filter that can identifie many womans at a time then many men at time as this:
update matches set sex = 'f' where name like '%Jheniffer%'
Since Jheniffer is a female name most likely you will update every registry which has part of the name as Jheniffer like 'Jheniffer Smith'. So repeat this process for the common names until the work is done. For all womens then repeat for the men.
Hope it help you to understand
you have to use update command, insert is for adding new rows.
update myTABLE set NewColumn = ?
Why INSERT here? You need to UPDATE data to column inserted.
Here is the list of steps
Alter the table then add column with constraint is NULLABLE.
Process the update the column added using UPDATE command.
If you want the column added is NOT nullable. Just re-alter the column and change it to NOT nullable.
You can use UPDATE command.
UPDATE my_table SET new_col='new_val'
if you need to set new column value just in few rows add WHERE clause
UPDATE my_table SET new_col='new_val' WHERE condition='true'

Syncing primary key between two tables stored in two different database

I have two MySQL database that contain two table, let's call them TABLE_A and TABLE_B. Both these tables have as fields id and title. Both databases are on the same server and the same user can access both.
Now, TABLE_A is a subset of TABLE_B for what concern the title field. It means that every title in TABLE_A is present in TABLE_B, too. While id fields of the two table are in no way related.
What I need is to sync id fields in TABLE_A with id fields in TABLE_B, according to the title, i.e. same title, same id. If it's not clear, I have to save TABLE_B id and override TABLE_A ones. And I DON'T have to add missing title from TABLE_B to TABLE_A.
Someone suggested to use a temporal table where to copy all TABLE_B fields in common with TABLE_A and then rename it as TABLE_A. I cannot follow this way, as TABLE_A actually has also other fields that I need to maintain. So, I cannot entirely drop the old TABLE_A.
Moreover, id is the primary key for both tables. It means that I cannot simply copy from TABLE_B to TABLE_A as the query will fail as soon as I try to change one id to another one that is already present in TABLE_A but linked to a different title.
I know how to write a Perl or PHP script to do it, but I would like to know if a pure MySQL solution exists.
You can do this
CREATE TABLE TableA_TMP AS
SELECT * FROM TableA;
ALTER TABLE TableA_TMP ADD id_new int;
UPDATE TableA_TMP A INNER JOIN TableB B ON lower(A.title) = lower(B.title)
SET id_new = B.id;
RENAME TABLE TableA TO TableA_backup;
CREATE TableA AS
select id_new as id, title,.... from TableA_TMP;