如何避免交叉连接?

如何避免交叉连接?

我想使用批处理文件更新/替换表数据。我将 old_data 存储在 old.txt 文件中,将 new_data 存储在 new.txt 文件中。

old.txt:
101
India

new.txt:
1001
0891

这是我的批处理脚本:demo.bat

@echo off
for /f "tokens=1* delims= " %%a in (old.txt) do (
for /f "tokens=1* delims= " %%b in (new.txt) do (

mysql -u root -p tiger -host localhost -database empsrc -e "update table_name set col_name='%%b' where col_name='%%a'"

)
)
pause

当我执行上述批处理文件时

101 is replaced with 1001 and 
india is also replaced with 1001

但是,我的预期输出是

101 is replaced  with 1001
India should be replaced with 0891

我该如何解决这个问题?有人能帮我吗

答案1

FOR 命令将在一个命令内部执行,从而创建以下组合:

101, 1001
India, 1001
101, 0891
India, 0891

您需要一个循环从每个文件中读取一个条目,然后执行代码。

基于这个答案,下面是一个将成对回显字符串的脚本:

@echo off
setlocal EnableDelayedExpansion
Rem First file is read with FOR /F command
Rem Second file is read via standard handle 3
3< old.txt (for /F "delims=" %%a in (new.txt) do (
  Rem Read next line from file2.txt
  set /P line2=<&3
  Rem Echo lines of both files
  echo %%a,!line2!
))

输出如下:

在此处输入图片描述

相关内容