I need to update a column in a table X with values that are written in another table Y. Mind you that there are gaps in the Id column as several/some rows were deleted. It starts with 63450 the Id column in table X, and it is not really sequential as shown below:
table X
Id Name Value
-------------------------------------------
63450 cmd NULL
63451 Jong NULL
63456 Xau-Min NULL
63457 bgf NULL
63458 tcr NULL
63459 cro NULL
63500 344453f NULL
63501 stackoverflow NULL
Table Y (parametrization)
Id Acronym Code
-------------------------------
1 cmd 545654
2 bgf 454565
3 cro 555555
4 rtg 465456
5 ert 546546
6 tcr 878787
Now after updating table X it should appear the following. It will update the vALUE in table X according to the matches in table Y...
table X after updating....
Id Name Value
-------------------------------------------
63450 cmd 545654
63451 Jong NULL
63456 Xau-Min NULL
63457 bgf 454565
63458 tcr 878787
63459 cro 555555
63500 344453f NULL
63501 stackoverflow NULL
if I try with
USE Database
DECLARE @counter int
SET @counter=(select count(*) from table_X)
WHILE @counter>0
BEGIN
UPDATE table_X
SET Value=(select Code
from table_Y b inner join table_X a on a.Name=b.Acronym
where a.Id= max(a.Id)-@counter+1)
SET @value=@value-1
END
it WILL not work as the Id is not sequential... how to achieve the updated table X as shown?
2nd: it would be nice to have a function to detect only letters (in the column Name of table X).. is there any in SQL? I only know to detect numeric values in strings: the isnumeric() function.
Thanks. :)