gpt4 book ai didi

java - 使用准备好的语句动态更改行的更新语句

转载 作者:行者123 更新时间:2023-11-30 05:32:43 25 4
gpt4 key购买 nike

我计划使用准备好的语句执行更新语句,该语句利用动态变化的列数。例如:在第一个更新语句中,我仅更新表的名称和年龄。在第二种情况下,我更新年龄、城市、州、国家等。在下一个实例中,我将像这样更新 150 列。

有人可以给我提供java中的完美方法吗?以下是示例

如果用户输入姓名和年龄,那么我会更新
UPDATE table1 set name = <> ,age = <>;

如果用户提供 city 的输入name state countrypin那么更新语句应该是这样的-

UPDATE table1 set name = <>, city = <>,state= <>,country=<>, pin = <>;

最佳答案

像这样构建你的 SQL 查询

  update demotable set col1 = case when @col1 is null then col1 else @col1 end  

或者

这里@col作为值从前端传递。

您可以从中创建动态sql

  declare @col1 nvarchar(max)  /// from front you can pass your column value with its column name like: col1 = 'col1'

declare @Query = 'update demotable set = ' + @col1 /// it create query as update demotable set col1 = 'col1'

PREPARE stmt1 FROM @Query ;
EXECUTE stmt1
DEALLOCATE PREPARE stmt1;

我是 MYSQL 新手,但这个逻辑肯定会起作用。

关于java - 使用准备好的语句动态更改行的更新语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57217972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com