SQL query for update [message #304853] |
Thu, 06 March 2008 23:49  |
jeer
Messages: 37 Registered: February 2008 Location: pakistan
|
Member |
|
|
hi
I got two tables
BOOK (DDC,TITLE,ISBN,AUTH_NAME)
AUTHOR (DDC,AUTH_NAME)
Table "BOOK" contains about 50000 records but its
AUTH_NAME column contains NULL for all 50000 records.
I want insert AUTH_NAME column's values from AUTHOR table
into BOOK's AUTH_NAME column where DDC number match.
Please write me the query for it.
thanks
[LF] Several topics have been merged during last few days.
@Jeer, please, stop opening a new topic for every new idea - continue discussion in the original topic you have opened in order to solve this problem.
[Updated on: Mon, 10 March 2008 05:54] by Moderator Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
Re: SQL Query for insertion [message #304876 is a reply to message #304853] |
Fri, 07 March 2008 00:52   |
mshrkshl
Messages: 247 Registered: September 2006 Location: New Delhi
|
Senior Member |
|
|
1* select * from book
SQL> /
DDC AUTHOR
---------- --------------------
1
SQL> select * from author;
DDC NAME
---------- --------------------
1 rita
SQL> ed
Wrote file afiedt.buf
1* update book set book.author=author.name where book.ddc=author.ddc
SQL> /
update book set book.author=author.name where book.ddc=author.ddc
*
ERROR at line 1:
ORA-00904: "AUTHOR"."DDC": invalid identifier
SQL> ed
Wrote file afiedt.buf
1* update book set author=(select name from author where book.ddc=author.ddc)
SQL> /
1 row updated.
SQL> select * from book;
DDC AUTHOR
---------- --------------------
1 rita
regards,
|
|
|
|
|
Re: SQL Query for insertion [message #304899 is a reply to message #304853] |
Fri, 07 March 2008 01:56   |
mshrkshl
Messages: 247 Registered: September 2006 Location: New Delhi
|
Senior Member |
|
|
Quote: | @mshrkshl
Once again your solution is WRONG.
It works if you only have 1 row.
|
what is wrong?
SQL> select * from book;
DDC AUTHOR
---------- --------------------
1
2
3
4
2
4
6 rows selected.
SQL> select * from author;
DDC NAME
---------- --------------------
1 ghf
2 hjdfs
3 tygy
4 mkhy
SQL> update book set author=(select name from author where book.ddc=author.ddc);
6 rows updated.
SQL> select * from book;
DDC AUTHOR
---------- --------------------
1 ghf
2 hjdfs
3 tygy
4 mkhy
2 hjdfs
4 mkhy
6 rows selected.
regards,
|
|
|
|
Re: SQL Query for insertion [message #304907 is a reply to message #304853] |
Fri, 07 March 2008 02:31   |
mshrkshl
Messages: 247 Registered: September 2006 Location: New Delhi
|
Senior Member |
|
|
if we update book table from selecting name from author table as matching ddc then ddc must be unique in author table,otherwise it will be an another case.
regards,
|
|
|
|
Re: SQL Query for insertion [message #305170 is a reply to message #304912] |
Sun, 09 March 2008 23:00   |
jeer
Messages: 37 Registered: February 2008 Location: pakistan
|
Member |
|
|
DDC Number is not unique. I want to update the table BOOK's column when it get the first matching value. After first matching value, rest should be ignored. Query should store the first matching AUTHOR_NAME in table BOOK.
|
|
|
SQL Query for update [message #305173 is a reply to message #304853] |
Sun, 09 March 2008 23:43   |
jeer
Messages: 37 Registered: February 2008 Location: pakistan
|
Member |
|
|
hi experts
I got two tables
BOOK (DDC,TITLE,ISBN,AUTH_NAME)
AUTHOR (ID,DDC,AUTH_NAME)
AUTH_NAME column in BOOK's table contains NULL for all records.
I want update table BOOK's column AUTH_NAME from AUTH_NAME column in AUTHOR table where DDC numbers match. DDC is not unique in AUTHOR's table. I want to update BOOK table with the first matching AUTH_NAME. Rest should be ignored.
Please write me the query for it.
thanks
|
|
|
|
|
updating table [message #305289 is a reply to message #304853] |
Mon, 10 March 2008 05:42   |
jeer
Messages: 37 Registered: February 2008 Location: pakistan
|
Member |
|
|
hi experts
I got two tables
BOOK (DDC,TITLE,ISBN,AUTH_NAME)
AUTHOR (ID,DDC,AUTH_NAME)
AUTH_NAME column in BOOK's table contains NULL for all records.
I want update table BOOK's column AUTH_NAME from AUTHOR's column in AUTHOR table where DDC numbers match. DDC is not unique in AUTHOR's table. I want to update BOOK table with the second matching AUTH_NAME. Rest should be ignored.
I used the query:
update BOOK
set AUTH_NAME = (
select AUTHOR.AUTH_NAME FROM AUTHOR
WHERE AUTHOR.DDC = BOOK.DDC AND rownum = 1);
But this query is applicable only for the first occurance.
But I want to update table BOOK with second occurence.
Guide me for it please.
Thanks
|
|
|
|
Re: updating table [message #305299 is a reply to message #305293] |
Mon, 10 March 2008 06:05  |
Frank Naude
Messages: 4596 Registered: April 1998
|
Senior Member |
|
|
A function like this should help:
CREATE OR REPLACE FUNCTION all_authors(p_ddc NUMBER) RETURN varchar2
AS
v_authors VARCHAR2(4000) := NULL;
BEGIN
FOR c1 IN (SELECT * FROM author WHERE ddc = p_ddc) LOOP
IF v_authors IS NULL THEN
v_authors := c1.auth_name;
ELSE
v_authors := v_authors ||', '|| c1.auth_name;
END IF;
END LOOP;
RETURN v_authors;
END;
/
|
|
|