Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to know if there is a way to select values from table into X so that X can hold more than 1 value in order to update another table with those values , what i am doing is this :
select number into @number from acct;
(This query return more than 1 number )
and it show this error (which is expected) "ERROR 1172 (42000): Result consisted of more than one row"

How can i use this variable (@number) to update another table when it meet a certain criteria ?


plz help and thnx for advance.
Posted

1 solution

If you are able to identify which records need to be updated in the table you can use whichever keys make the tables relate to update multiple rows.

So,
UPDATE Table1 as T1, Table2 as T2 Set T1.X=T2.X WHERE T1.ID = T2.ID AND (Any other relationships required to uniquely identify each row you want to update)

Also, if you are doing an insert and not an update you are able to do:

INSERT INTO Table1 (SELECT X, ID, OtherVariable1, OtherVariable1, DefaultValue) FROM Table2 WHERE (certain conditions are met));

The order of the variables in the SELECT statment have to match the schema of the table you are inserting into. Also, what I meant by DefaultValue was that inside of the SELECT statement you could place a default value for one of the columns. So if the Column was called Active and it was either a 1 or 0 you could simply replace DefaultValue with 1.

I hope this helps although I am not 100% sure what your situation is. If you have any questions about something I didn't clarify well enough please post them with a comment on this question. Also, if I missed the boat completely could you please post what your schemas are for each table and what you want to do.
 
Share this answer
 
Comments
KaRaMiLo 6-Jun-10 8:59am    
I am sorry but i really guess that u miss the point .
abt ur reply i already know that but thnx anyway.

now here is the scenario :

i got 2 tables :
1- Account_info : with fields : (foreign Key {Prefix_id to prefix into table} , number , subscriber_number )
there is a trigger for this table in order to get the subscriber number using both prefix id and number as in example :
insert into account_info (prefix_id, number ) values (1,9090909) ; ( will automatically insert the subscriber_number with the value that ID 1 refers to and the rest of the number concat (Value(prefix_id(1),number)

2- Prefix_info : with fields: (primary key prefix_id , prefix_value) , this also got a trigger but this trigger works differently .
this trigger is fired when u try to update the prefix value so it looks for all values in account_info table which got the same prefix_id and it will update the subscriber number according to new values.

here is the second trigger code :
Trigger: before_update
Event: UPDATE
Table: prefix_info
Statement: BEGIN
SET @curr_number=0;
SELECT number INTO @curr_number FROM account_info WHERE NEW.prefix_id = account_info.sub_prefix_id;
UPDATE account_info SET account_info.subscriber_id = concat(NEW.prefix_value,@curr_number) WHERE NEW.prefix_id = account_info.sub_prefix_id ;
END

You will notice in this statement "SELECT number INTO @curr_number FROM account_info WHERE NEW.prefix_id = account_info.sub_prefix_id;" that i try to load the number into variable but the query returns more than a single number which cause this error "ERROR 1172 (42000): Result consisted of more than one row" , so i want to enhance that trigger behavior in order to let it update all records not a single one only.

i hope that i make it clear for u know.

and thnx for ur time

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900