tags:

views:

31

answers:

1

I have 2 tables - A and B . Tbale A has columns -pkey(primary key) and col1.Tbale B has columns pr_key(primary key, not a reference key) and column1.Table B has no values,while table A is populated with 4 rows.I want to update table B taking exactly the same values as table A at one go. Please help!

+1  A: 

Should be something like that (depends on SQL implementation you use, but in general, the following is rather standard. In particular should work in MS-SQL and in MySQL.

INSERT INTO tblB (pr_key, column1)
  SELECT pkey, col1
  FROM tblA
  -- WHERE some condition (if you don't want 100% of A to be copied)

The question is a bit unclear as to the nature of tblB's pr_key, if for some reason this was a default/auto-incremented key for that table, it could just then be omitted from both the column list (in parenthesis) and in the SELECT that follows. In this fashion upon insertion of each new row, a new value would be generated.

Edit: It appears the OP actually wants to update table B with values from A. The syntax for this should then be something like

UPDATE tblB 
SET Column1 = A.Col1
FROM tblA AS A
JOIN tblB AS B ON B.pr_key = A.pkey
mjv
Ok,I am sorry.Since pr_key of table B is a primary key,so null values cannot be inserted in table B.Hence the values for pr_key is same as pkey column of Table A.Table A has been arbitrarily populated with 4 rows.
Anupam Ray
Table Apkey col1A 10B 20 C 30D 40Table Bpr_key column1A nullB nullC nullD nullI want to updaet table B with col1 of table A at one go.
Anupam Ray
@Anupam Ray, check the edit where I added UPDATE syntax. (Not 100% sure this is ok with Oracle 10g [now that you specified it], but again, should be damn close, check Oracle's docs)
mjv