SQL Server

I need to compare records between two tables. I need to get the first record from table1 and compare it with all records in table 2 and replace the value from table 1 with the value from table 2

Table1

Column1

A

B

C

AA

Table2

Column1

AA

A2

A

B

C

DD

September 4th, 2015 4:47pm

I don't understand what you are trying to achieve.  How can you replace a row in Table 1 with ALL the rows in Table 2?  In addition, you haven't specified how you are mapping rows from Table 1 to Table 2?  You could also add the criteria of how you intend to update Table 1 with Table 2.
Free Windows Admin Tool Kit Click here and download it now
September 4th, 2015 5:02pm

One way is to use a cursor to process all records (rows) in table 1 in a WHILE loop:

Example for cursor usage:

How to architect running total using cursor?

Cursors do not scale well, so the first choice is set-based operations.  Nonetheless, in your case you may find the logic easier to build with cursor. Later you may try to convert the logic to set-based.

September 5th, 2015 10:17am

And what will be desired result based on above example?

CREATE TABLE w
(
 COL1 VARCHAR(20)
)

INSERT INTO W VALUES ('URIDIMANT')
INSERT INTO W VALUES ('DIMANT')
INSERT INTO W VALUES ('URIRON')
INSERT INTO W VALUES ('RR')

CREATE TABLE w1
(
 COL1 VARCHAR(20)
)

INSERT INTO W1 VALUES ('URIDIMANT')
INSERT INTO W1 VALUES ('DIMANTRON')
INSERT INTO W1 VALUES ('URI')
INSERT INTO W1 VALUES ('URIRON')


SELECT W.COL1,W1.COL1 FROM W JOIN W1
ON W.col1 LIKE '%' +  W1.col1 + '%'

DROP TABLE W
DROP TABLE W1

Free Windows Admin Tool Kit Click here and download it now
September 6th, 2015 2:49am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics