求助,jdbctemplate调用sqlServer存储过程传入表参数,不支持从 UNKNOWN 到 OTHER 的转换,要怎么正确传入表参数

qq_21322817 2024-08-16 10:59:19

List<TableBoxVO> tableBoxVOS = new ArrayList<>();
        List<TableWipItemTypeVO> tableWipItemTypeVOS = new ArrayList<>();
        TableBoxVO tableBoxVO;
        TableWipItemTypeVO tableWipItemTypeVO;
        Map<String, Object> tableData;
        Map<String, Object> tableBoxData;
        List<Map<String, Object>> tableValues = new ArrayList<>();
        List<Map<String, Object>> tableBoxValues = new ArrayList<>();
        for(BoxInfo boxInfo: processTransferVO.getTableBoxVOS()){
            tableBoxData = new HashMap<>();
            tableBoxData.put("FlowBox",boxInfo.getBoxCode());
            tableBoxData.put("qty",boxInfo.getQty());
            tableBoxVO = new TableBoxVO(boxInfo);
            tableBoxVOS.add(tableBoxVO);
            tableBoxValues.add(tableBoxData);
        }
        for(MaterialVO materialVO: processTransferVO.getTableWipItemTypeVOS()){
            tableData = new HashMap<>();
            tableData.put("itemID",122);
            tableData.put("itemCode",null);
            tableData.put("itemName",null);
            tableData.put("supplierCode",null);
            tableData.put("lot",null);
            tableData.put("boxCode",null);
            tableData.put("qty", null);
            tableWipItemTypeVO = new TableWipItemTypeVO(materialVO);
            tableWipItemTypeVOS.add(tableWipItemTypeVO);
            tableValues.add(tableData);
        }
        SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
                .withProcedureName("saveWipProductData_Batch_p")
                .declareParameters(
                        new SqlParameter("WipNo", Types.VARCHAR),//工单号
                        new SqlParameter("WipQty", Types.INTEGER),//工单数量
                        new SqlParameter("ItemID", Types.INTEGER),//产品Id
                        new SqlParameter("RouteEntry_ID", Types.INTEGER),//工艺路线表体ID
                        new SqlParameter("MachineID", Types.VARCHAR),//机台号
                        new SqlParameter("UserID", Types.VARCHAR),//用户ID
                        new SqlParameter("UserName", Types.VARCHAR),//用户名称
                        new SqlParameter("GoodProductsQty", Types.DECIMAL),//良品数
                        new SqlParameter("RejectsQty", Types.DECIMAL),//不良数
                        new SqlParameter("remark", Types.VARCHAR),//备注
                        new SqlParameter("CurrentBox", Types.VARCHAR),//当前框
                        new SqlParameter("FlowBox", Types.VARCHAR),//转移框
                        new SqlParameter("dt", Types.OTHER, "Wip_Item_type")//领料信息
//                        new SqlParameter("dt_FlowBox_Qty", Types.OTHER, "FlowBox_Qty_type")//转移框类别
                );
        Map<String, Object> inParams = new HashMap<>();
        inParams.put("WipNo", processTransferVO.getOrderNo());
        inParams.put("WipQty", processTransferVO.getOrderQty());
        inParams.put("ItemID", processTransferVO.getMaterialId());
        inParams.put("RouteEntry_ID", processTransferVO.getProcessVO().getSysId());
        inParams.put("MachineID", processTransferVO.getMachineId());
        inParams.put("UserID", processTransferVO.getUserId());
        inParams.put("UserName", processTransferVO.getUserName());
        inParams.put("GoodProductsQty", processTransferVO.getGoodQty());
        inParams.put("RejectsQty", 0);
        inParams.put("remark", processTransferVO.getRemark());
        inParams.put("CurrentBox", processTransferVO.getCurrentBox());
        inParams.put("FlowBox", processTransferVO.getFlowBox());
        inParams.put("dt", tableValues);
//        inParams.put("dt_FlowBox_Qty", new MapSqlParameterSource(tableBoxData));
        String returnMsg = jdbcCall.execute(inParams).get("Fmsg").toString();
        if(returnMsg.indexOf("保存成功") !=-1){
            return new ResultVO(ResultVO.Result.SUCCESS);
        }
        return new ResultVO(ResultVO.Result.FAIL, returnMsg);
    }

 

 

 


Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: CallableStatementCallback; uncategorized SQLException for SQL [{call saveWipProductData_Batch_p(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}]; SQL state [null]; error code [0]; 不支持从 UNKNOWN 到 OTHER 的转换。; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: 不支持从 UNKNOWN 到 OTHER 的转换。] with root cause

...全文
510 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_21322817 2024-08-19
  • 打赏
  • 举报
回复
  new SqlParameter("WipNo", Types.VARCHAR),
                    new SqlParameter("WipQty", Types.INTEGER),
                    new SqlParameter("ItemID", Types.INTEGER),
                    new SqlParameter("RouteEntry_ID", Types.INTEGER),
                    new SqlParameter("MachineID", Types.VARCHAR),
                    new SqlParameter("UserID", Types.VARCHAR),
                    new SqlParameter("UserName", Types.VARCHAR),
                    new SqlParameter("GoodProductsQty", Types.DECIMAL),
                    new SqlParameter("RejectsQty", Types.DECIMAL),
                    new SqlParameter("remark", Types.VARCHAR),
                    new SqlParameter("CurrentBox", Types.VARCHAR),
                    new SqlParameter("FlowBox", Types.VARCHAR),
                    //new SqlParameter("dt", Types.STRUCT, "Wip_Item_type")//注释掉指定的类型就不会报错啦,保存成功




愿时光不负. 2024-08-16
  • 打赏
  • 举报
回复

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>9.4.1.jre8</version>
</dependency>

import com.microsoft.sqlserver.jdbc.SQLServerDataTable;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;

import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class YourClass {
    public void yourMethod() throws SQLServerException {
        // 创建 SQLServerDataTable
        SQLServerDataTable table = new SQLServerDataTable();
        table.addColumnMetadata("itemID", Types.INTEGER);
        table.addColumnMetadata("itemCode", Types.VARCHAR);
        table.addColumnMetadata("itemName", Types.VARCHAR);
        table.addColumnMetadata("supplierCode", Types.VARCHAR);
        table.addColumnMetadata("lot", Types.VARCHAR);
        table.addColumnMetadata("boxCode", Types.VARCHAR);
        table.addColumnMetadata("qty", Types.DECIMAL);

        // 添加数据
        for (Map<String, Object> data : tableValues) {
            table.addRow(data.get("itemID"), data.get("itemCode"), data.get("itemName"), 
                         data.get("supplierCode"), data.get("lot"), data.get("boxCode"), data.get("qty"));
        }

        // 调用存储过程
        SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
                .withProcedureName("saveWipProductData_Batch_p")
                .declareParameters(
                        new SqlParameter("WipNo", Types.VARCHAR),
                        new SqlParameter("WipQty", Types.INTEGER),
                        new SqlParameter("ItemID", Types.INTEGER),
                        new SqlParameter("RouteEntry_ID", Types.INTEGER),
                        new SqlParameter("MachineID", Types.VARCHAR),
                        new SqlParameter("UserID", Types.VARCHAR),
                        new SqlParameter("UserName", Types.VARCHAR),
                        new SqlParameter("GoodProductsQty", Types.DECIMAL),
                        new SqlParameter("RejectsQty", Types.DECIMAL),
                        new SqlParameter("remark", Types.VARCHAR),
                        new SqlParameter("CurrentBox", Types.VARCHAR),
                        new SqlParameter("FlowBox", Types.VARCHAR),
                        new SqlParameter("dt", Types.STRUCT, "Wip_Item_type")
                );

        Map<String, Object> inParams = new HashMap<>();
        inParams.put("WipNo", processTransferVO.getOrderNo());
        inParams.put("WipQty", processTransferVO.getOrderQty());
        inParams.put("ItemID", processTransferVO.getMaterialId());
        inParams.put("RouteEntry_ID", processTransferVO.getProcessVO().getSysId());
        inParams.put("MachineID", processTransferVO.getMachineId());
        inParams.put("UserID", processTransferVO.getUserId());
        inParams.put("UserName", processTransferVO.getUserName());
        inParams.put("GoodProductsQty", processTransferVO.getGoodQty());
        inParams.put("RejectsQty", 0);
        inParams.put("remark", processTransferVO.getRemark());
        inParams.put("CurrentBox", processTransferVO.getCurrentBox());
        inParams.put("FlowBox", processTransferVO.getFlowBox());
        inParams.put("dt", table); // 使用 SQLServerDataTable 作为参数传递

        String returnMsg = jdbcCall.execute(inParams).get("Fmsg").toString();
        if (returnMsg.contains("保存成功")) {
            System.out.println("Success");
        } else {
            System.out.println("Fail: " + returnMsg);
        }
    }
}

qq_21322817 2024-08-16
  • 举报
回复
@愿时光不负. 不支持从 TVP 到 STRUCT 的转换。
qq_21322817 2024-08-16
  • 举报
回复
@qq_21322817 public ResultVO submitOrder(ProcessTransferVO processTransferVO) throws SQLServerException { SQLServerDataTable dtWipItem = new SQLServerDataTable(); dtWipItem.addColumnMetadata("ItemID", Types.INTEGER); dtWipItem.addColumnMetadata("ItemCode", Types.VARCHAR); dtWipItem.addColumnMetadata("ItemName", Types.VARCHAR); dtWipItem.addColumnMetadata("SupplierCode", Types.VARCHAR); dtWipItem.addColumnMetadata("Lot", Types.VARCHAR); dtWipItem.addColumnMetadata("BoxCode", Types.VARCHAR); dtWipItem.addColumnMetadata("Qty", Types.DECIMAL); for (MaterialVO materialVO : processTransferVO.getTableWipItemTypeVOS()) { dtWipItem.addRow(materialVO.getMaterialId(), materialVO.getMaterialCode(), materialVO.getMaterialName(), materialVO.getSupplierCode(), materialVO.getBatchNo(), materialVO.getBoxCode(), materialVO.getQty()); } SQLServerDataTable dtFlowBoxQty = new SQLServerDataTable(); dtFlowBoxQty.addColumnMetadata("FlowBox", Types.VARCHAR); dtFlowBoxQty.addColumnMetadata("Qty", Types.DECIMAL); for (BoxInfo boxInfo : processTransferVO.getTableBoxVOS()) { dtFlowBoxQty.addRow(boxInfo.getBoxCode(), boxInfo.getQty()); } SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate) .withProcedureName("saveWipProductData_Batch_p") .declareParameters( new SqlParameter("WipNo", Types.VARCHAR), new SqlParameter("WipQty", Types.INTEGER), new SqlParameter("ItemID", Types.INTEGER), new SqlParameter("RouteEntry_ID", Types.INTEGER), new SqlParameter("MachineID", Types.VARCHAR), new SqlParameter("UserID", Types.VARCHAR), new SqlParameter("UserName", Types.VARCHAR), new SqlParameter("GoodProductsQty", Types.DECIMAL), new SqlParameter("RejectsQty", Types.DECIMAL), new SqlParameter("remark", Types.VARCHAR), new SqlParameter("CurrentBox", Types.VARCHAR), new SqlParameter("FlowBox", Types.VARCHAR), new SqlParameter("dt", Types.STRUCT, "dbo.Wip_Item_type"), new SqlParameter("dt_FlowBox_Qty", Types.STRUCT, "dbo.FlowBox_Qty_type") ); Map&lt;String, Object&gt; inParams = new HashMap&lt;&gt;(); inParams.put("WipNo", processTransferVO.getOrderNo()); inParams.put("WipQty", processTransferVO.getOrderQty()); inParams.put("ItemID", processTransferVO.getMaterialId()); inParams.put("RouteEntry_ID", processTransferVO.getProcessVO().getSysId()); inParams.put("MachineID", processTransferVO.getMachineId()); inParams.put("UserID", processTransferVO.getUserId()); inParams.put("UserName", processTransferVO.getUserName()); inParams.put("GoodProductsQty", processTransferVO.getGoodQty()); inParams.put("RejectsQty", 0); inParams.put("remark", processTransferVO.getRemark()); inParams.put("CurrentBox", processTransferVO.getCurrentBox()); inParams.put("FlowBox", processTransferVO.getFlowBox()); inParams.put("dt", dtWipItem); inParams.put("dt_FlowBox_Qty", dtFlowBoxQty); String returnMsg = jdbcCall.execute(inParams).get("Fmsg").toString(); if(returnMsg.contains("保存成功")){ return new ResultVO(ResultVO.Result.SUCCESS); } return new ResultVO(ResultVO.Result.FAIL, returnMsg); }
qq_21322817 2024-08-16
  • 举报
回复
@qq_21322817 还是不行🙂‍↔️
1条回复
愿时光不负. 2024-08-16
  • 打赏
  • 举报
回复

SqlParameterSource[] paramSourceArray = tableValues.stream()
    .map(MapSqlParameterSource::new)
    .toArray(SqlParameterSource[]::new);

Map<String, Object> inParams = new HashMap<>();
inParams.put("WipNo", processTransferVO.getOrderNo());
inParams.put("WipQty", processTransferVO.getOrderQty());
inParams.put("ItemID", processTransferVO.getMaterialId());
inParams.put("RouteEntry_ID", processTransferVO.getProcessVO().getSysId());
inParams.put("MachineID", processTransferVO.getMachineId());
inParams.put("UserID", processTransferVO.getUserId());
inParams.put("UserName", processTransferVO.getUserName());
inParams.put("GoodProductsQty", processTransferVO.getGoodQty());
inParams.put("RejectsQty", 0);
inParams.put("remark", processTransferVO.getRemark());
inParams.put("CurrentBox", processTransferVO.getCurrentBox());
inParams.put("FlowBox", processTransferVO.getFlowBox());
inParams.put("dt", paramSourceArray);

String returnMsg = jdbcCall.execute(inParams).get("Fmsg").toString();

qq_21322817 2024-08-16
  • 举报
回复
@愿时光不负. 还是提示‘不支持从 UNKNOWN 到 ARRAY 的转换’,如果类型换成new SqlParameter("dt", Types.VARCHAR, "Wip_Item_type"),就会提示 操作数类型冲突: nvarchar 与 Wip_Item_type 不兼容
内容概要:本文介绍了如何利用 GitHub Copilot 辅助进行程序调试与 Bug 分析,强调 Copilot 不仅可用于代码生成,更是强大的代码分析与调试工具。文章详细阐述了 Copilot 在调试复杂问题、老旧项目维护和难以复现 Bug 场景下的优势,提出了“先分析、再修改”的四步流程:分析原因→评估风险→提出方案→修改代码,并推荐结合错误日志、用户操作等信息精准提问,提升 AI 回答质量。同时展示了如何通过 Copilot 增强调试能力,如自动加日志、异常保护、生成测试数据和性能分析。最后通过游戏拾取系统的实际案例,说明如何结构化描述问题以获得有效反馈。; 适合人群:具备一定开发经验,正在参与项目调试或维护工作的程序员,尤其是面对复杂逻辑、历史代码或难复现 Bug 的 1-3 年开发者;也适合希望提升 AI 协作能力的技术人员。; 使用场景及目标:①快速定位偶发性崩溃、数据异常等问题根源;②理解无文档或结构混乱的老代码模块;③优化调试流程,借助 AI 生成诊断建议、修复方案与测试用例;④构建更具健壮性的程序,提前发现潜在缺陷。; 阅读建议:学习者应结合自身项目中的真实问题,按照文中提供的结构化提问模板实践,逐步训练与 Copilot 的协作能力,重视问题描述的完整性与准确性,避免直接要求修改代码,优先通过分析提升对系统的理解。
内容概要:本文针对高精度电流控制下的永磁同步电机(PMSM)参数辨识难题,提出一种基于粒子群优化算法(PSO)的多参数辨识模型,并在Simulink环境中完成系统级仿真实现。研究旨在克服传统控制中因电机参数(如定子电阻、交直轴电感、永磁磁链等)随温度、负载变化而失配所导致的电流控制性能下降问题。通过构建以电流跟踪误差为核心的适应度函数,利用PSO算法全局寻优能力强的特点,实现对关键电机参数的在线或离线精确辨识。文中详述了PSO算法的实现机制、参数初始化策略、收敛判据设计以及与PMSM矢量控制系统的集成方法,验证了该方案在不同运行工况下的辨识精度、收敛速度与鲁棒性,显著提升了电流环的动态响应品质与稳态控制精度。; 适合人群:具备电机驱动控制、现代控制理论及优化算法基础,熟悉MATLAB/Simulink仿真平台,从事高性能PMSM控制系统研发的研究生、高校科研人员及自动化、电力电子领域的工程师;特别适合正在开展参数自适应、智能控制算法应用等相关课题的研究者。; 使用场景及目标:①应用于高端制造装备、电动汽车驱动系统、精密伺服系统等对电流控制精度要求严苛的场合;②解决实际工程中因电机温升、老化等因素引发的参数漂移问题,提升系统长期运行稳定性;③作为智能优化算法与电机控制深度融合的教学案例,帮助理解PSO在复杂非线性系统参数辨识中的应用逻辑与实现路径。; 阅读建议:建议读者结合提供的Simulink仿真模型进行复现实验,重点剖析PSO算法模块与电机控制模型的接口设计、适应度函数的构建原则及参数敏感性分析方法,可进一步尝试引入其他先进优化算法(如GWO、HHO)进行性能对比,以深入掌握不同智能算法在工程辨识问题中的适用性与优劣。

21,889

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧