1

我是 Spring Source 和 SQL Server 的新手,很抱歉这个问题。我正在尝试将行插入表 ( IN136_DMV_USED_CAR_SALES)。这个表有一个复合键,它连接到另一个父表EXT_INF_LOAD

到目前为止,我能够从输入文件中读取所有行并将所有行加载到IN136_DMV_USED_CAR_SALES具有两个键的表中:EXT_INF_LOAD_IDIN136_ID使用以下 SQL 语句:

插入语句:

 String sql = "insert into dbo.IN136_DMV_USED_CAR_SALES (EXT_INF_LOAD_ID, IN136_ID, " + 
              "DEALER_NUM, " +
            "VSL_PURCHASE_DATE," +
                    "OWNERSHIP_ISSUED_DATE, " +
        "LAST_TRANSFER_DATE, " +
        "ODOMETER_CURRENT_DATE, " +
        "WORK_DATE, " +
                          "PURCHASE_PRICE, " +
        "VIN_HIN, " +
        "CURRENT_LICENSE, " +
        "REG_OWNER_NAME, " +
        "REG_OWNER_ADDRESS2, " +
        "REG_OWNER_ADDRESS3, " +
        "REG_OWNER_CITY_OR_STATE," +
        "LEGAL_OWNER_NAME," +
        "LEGAL_OWNER_ADDRESS2," +
        "LEGAL_OWNER_ADDRESS3," +
        "LEGAL_OWNER_CITY_OR_STATE," +
        "MAKE_BUILDER," +
        "YEAR_MODEL," +
        "VLF_CLASS," +
        "IRP_ACCOUNT_MX_NUM," +
        "IRP_ACCOUNT_NUM," +
        "IRP_ACCOUNT_USDOT_NUM," +
        "IRP_FLEET_MX_NUM," +
        "IRP_FLEET_NUM," +
        "IRP_FLEET_USDOT_NUM," +
        "IRP_VEH_MX_NUM," + 
        "IRP_VEH_USDOT_NUM) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

JdbcTemplate jdbctempl = new JdbcTemplate(dataSource);
jdbctempl.update(sql, new Object[] {id1, id2++, 
        rec.getDealerNum(),
              rec.getVslPurchaseDate(),
              rec.getOwnershipIssueDate(), 
              rec.getLastTransferDate(), 
        rec.getOdometerCurrentDate(),
        rec.getWorkDate(), 
        rec.getPurchasePrice(), 
        rec.getVinHin(),
        rec.getCurrentLicense(),
        rec.getRegOwnerName(),
        rec.getRegOwnerAddress2(),
        rec.getRegOwnerAddress3(),
        rec.getRegOwnerCityOrState(),
        rec.getLegalOwnerName(),
        rec.getLegalOwnerAddress2(),
        rec.getLegalOwnerAddress3(),
        rec.getLegalOwnerCityOrState(),
        rec.getMakeBuilder(),
        rec.getYearModel(),
        rec.getVlfClass(),
        rec.getIrpAccountMxNum(),
        rec.getIrpAccountNum(),
        rec.getIrpAccountUsdotNum(),
        rec.getIrpFleetMxNum(),
        rec.getIrpFleetNum(),
        rec.getIrpFleetUsdotNum(),
        rec.getIrpVehMxNum(),
        rec.getIrpVehUsdotNum()});

我的问题是在IN136_DMV_USED_CAR_SALES(子表)中,复合键()的列之一EXT_INF_LOAD_ID连接到EXT_INF_LOAD父表。

所以,我想我真正需要做的(但我不知道怎么做)是:

  1. 有关的信息EXT_INF_ID将通过参数传递(我现在称其为“计数器”)。我将不得不阅读EXT_INF表格,看看是否能找到匹配项 ( Counter = EXT_INF_ID)。所以现在我在EXT_INF表中找到了行,我可以获取EXT_INF_NAME要填充到EXT_INF_LOAD表(EXT_INF_FILE_NAME列)中的列的值。

  2. 由于我知道该行存在于EXT_INF表中,因此我可以先将新行插入父表 ( EXT_INF_LOAD),然后再开始将新行添加到子表 ( IN136_DMV_USED_CAR_SALES) 中。
    要插入父表的新行将具有以下值:
    2.a EXT_INF_ID = Counter
    2.b EXT_INF_FILE_NAME = EXT_INF_NAME(来自EXT_INF表)
    2.c EXT_INF_LOAD_DATE= todays-date
    2.d 但是 forEXT_INF_FILE_NAME呢?

  3. IN136_DMV_USED_CAR_SALES这样,当它们出现时,我将能够继续将其他新输入文件添加到表中。只要我对(父表)和(子表) EXT_INF_LOAD_ID都使用相同的值,对吗?EXT_INF_LOADIN136_DMV_USED_CAR_SALES

但后来我意识到 (2.d) 也不能通过另一个counter2参数传递,例如,因为EXT_INF_IDand的值EXT_INF_LOAD_ID必须是唯一的,即不允许重复...

我希望有人可以帮助我找到创建所有必需 SQL 语句的正确方法,因为我遇到了很多问题/异常错误。(我也是 SQL Server 的新手)

提前感谢您对此事的关注。抱歉,无法上传表格的图片,但以下是所有 3 个表格的 SQL 语句:

表 1:EXT_INF

SELECT [EXT_INF_ID]
      ,[EXT_INF_NAME]
      ,[EXT_INF_DESC]
  FROM [boeafspdev1].[dbo].[EXT_INF]
 where EXT_INF_ID = counter  

表 2:

SELECT [EXT_INF_ID]
  ,[EXT_INF_LOAD_ID]
  ,[EXT_INF_FILE_NAME]
  ,[EXT_INF_LOAD_DATE]
  ,[EXT_INF_COMMENTS]
FROM [boeafspdev1].[dbo].[EXT_INF_LOAD]
where EXT_INF_ID = counter
4

0 回答 0