根据两个不同列之间的比较值更新一列

时间:2017-07-19 03:01:12

标签: sql-server sql-server-2008

我有这样的电话号码:

表1

rownum TelephoneCode --TelephoneCode is integer datatype
-------------------        
  1       444
  2       555
  3       666
  4       777

我有另一张有3列的表格(国家栏目是空白的)

表2

  rownum country telephonenumber -- telephonenumber is varchar datatype
  -------------------------------
    1                 6661234(C)
    2                 4440987
    3                 9999870
    4                 555-1245

我想要的是,我的查询应该读取" TelephoneCode"来自"表1和#34;并在" tepephonenumber"中搜索该代码列" table2"。   如果" TelephoneCode"在"表2"然后更新"国家" "表2"到" AU"。

因此我的最终表(即table2)结果应该是这样的:

表2

  rownum country telephonenumber
  -------------------------------
    1        AU         6661234
    2        AU         4440987
    3                   9999870
    4        AU         5551245

这是我尝试过的。此查询正在成功执行;但是,不更新"国家" "表2"

的列
 declare @code int
 declare @parsedcode int
 declare @numcount int
 declare @numcount1 int
 declare @telephonenumberread varchar(max)



 while(@numcount <= 356)
 begin


   select @code= [TelephoneCode] from table1 where rownum = @numcount
 --select @code

 while(@numcount1 <=137992)
   begin
     select @telephonenumberread = TELPHONH from table2 where rownum = 
     @numcount1
   --print @telephonenumberread
   set @parsedcode = substring(@telephonenumberread,1,3)

   if(@code = @parsedcode)
     begin

         update table2 set country = 'AU'
         --return
     end
  --else

     set @numcount1 = @numcount1+1

 end

   set @numcount = @numcount+1
end

希望我已正确解释。 注意:我的表在国家/地区列中的空白超过100K。

如何实现这一目标?甚至,我无法打印调试的值?我究竟做错了什么?   感谢。

0 个答案:

没有答案