February 28, 2007 at 9:55 am
hi ,
i need help on this.
i have two tables A and B
In table A there is a field called ID which has values like
A10
A20
and so on.
i want to update Table B with THE values above in two columns ID and Code which should have values like this.Table B has two columns ID and Code
ID Code
A 10
A 20
February 28, 2007 at 10:18 am
Will all of the values have the same format of A10? If so, this will work:
DECLARE @table TABLE (value char(3))
DECLARE @table2 TABLE (id CHAR(1), code CHAR(2))
INSERT INTO @table
SELECT 'A10' UNION ALL
SELECT 'A20'
INSERT INTO @table2
SELECT SUBSTRING(value,1,1) AS ID, SUBSTRING(value,2,2) AS Code
FROM @table
SELECT *
FROM @table2
February 28, 2007 at 10:19 am
thanks for the help
February 28, 2007 at 10:19 am
do you want to insert the records from table a into table b or do a update? should your output be somewhat like this
ID Code
A 10
A 20
here is the insert statement
insert into #tableb (id,code)
select left(id,1),substring(id,2,10) from #tableA
February 28, 2007 at 10:26 am
i want to write an update and not an insert statement so how do i do that?
February 28, 2007 at 10:34 am
can you explain as to how your input and output will be. your explanation looks as if you want to insert into table b.
February 28, 2007 at 10:39 am
i want to update table b which has two columns called id and code
from table a which has one column called id
table a has values like A10,A20 and so on
now table b has two columns id and code which needs to be updated so they have values like as below
table b
id code
A 10
A 20
February 28, 2007 at 11:20 am
can somebody help on this please
February 28, 2007 at 11:49 am
what do you want to update tableb with? can you send a sample output? i dont think i understood your question clearly.
February 28, 2007 at 12:05 pm
i have a table A which has column name as FlagID with datatype bit
i want to update the whole column with a value 1
February 28, 2007 at 12:11 pm
Whoa, this is totaly different from what you've previously described. It would be very helpful if you would post your table DDL for both tables as well as sample data before and after the update.
February 28, 2007 at 6:48 pm
... and, BEST, you need to identify how table A and table B relate to each other... what would the join columns be?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 1, 2007 at 2:55 am
UPDATE A SET FlagId = 1;
Unless I am overlooking something, this is basic SQL.
March 1, 2007 at 3:08 am
Hi ,
This query might help you....
Update B set Code=substring(a.id,2,len(a.id)) from table a join table b on left(a.id,1)=b.id
Regards ,
Amit Gupta
March 1, 2007 at 3:17 am
The UPDATE statement is only valid for changing existing RECORDS.
To "update" a table, you use UPDATE (records that are already present), INSERT (records that are not yet present) and DELETE (records that are superfluous).
In your case, if table B must contain the "same" records as table A and no others, the simplest solution is a general DELETE followed by pinky's suggestion:
DELETE FROM B;
INSERT INTO B(id,code)
SELECT LEFT(id,1),SUBSTRING(id,2,10) FROM A;
If you want to add the new records that do not yet exist in B, leave out the DELETE and INSERT only the new ones:
INSERT INTO B(id,code)
( SELECT LEFT(id,1),SUBSTRING(id,2,10)FROM A
EXCEPT
SELECT id, code FROM B );
If you want to delete the records from B that do not exist in A:
DELETE FROM B
WHERE NOT EXISTS
( SELECT * FROM A
WHERE A.id = B.id + B.code );
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply