英文:
Placeholder in prepared statement
问题
我在预处理语句中使用了以下查询。之前我是在存储过程中使用的,并且使用了可调用的过程,但现在我想在JDBC预处理语句中使用select查询。
我知道在预处理语句中我们会写类似 insert into abc values(?,?,?);
这样的语法。
但是这里我有一个插入-选择(insert-select)的情况。同样的变量在很多地方被使用。在这个查询中,我有两个变量 p_entity_type
和 p_update_mode
。
INSERT INTO dynamicEntitynotgett
(entity_type, entity_id, entity_code, synonyms, action)
WITH data_view AS
( -- ITEM table
SELECT 'ITEM' entity_type, -- This separates inserted values
item_id data_id,
item_name data_name,
item_desc data_desc,
creation_date
FROM itemde
UNION ALL
-- ORG table
SELECT 'ORG' entity_type, -- This separates inserted values
org_id,
org_name,
org_desc,
creation_date
FROM orgde
UNION ALL
-- Feature table
SELECT 'FEATURES' entity_type, -- This separates inserted values
FEATURE_id data_id,
FEATURE_NAME data_name,
FEATURE_DESC data_desc,
CREATION_DATE
FROM FEATURESDE
)
SELECT upper(t.entity_type),
t.data_id,
t.data_name,
t.data_desc,
CASE lower(p_update_mode)
WHEN 'INCREMENTAL' THEN
CASE
WHEN t.creation_date > b.last_update_date THEN
'update'
WHEN t.creation_date < b.last_update_date THEN
'add'
END
WHEN 'full' THEN
'add'
END action
FROM data_view t
LEFT JOIN ODA_REFRESH_DETAILS b
ON b.entity_type = t.entity_type
AND lower(p_update_mode )='incremental'
WHERE (upper(p_entity_type) = t.entity_type OR p_entity_type IS NULL)
AND (lower(p_update_mode) = 'full'
OR (lower(p_update_mode) = 'incremental' AND b.entity_type IS NOT NULL)
);
我会从上游接收 p_entity_type
和 p_update_mode
。哪种解决方案更好?使用 ResultSet 还是 PreparedStatement,以及如何在查询中替换这些值或者使用 setXXX() 方法。
英文:
I am using the below query in a prepared statement. Earlier I was using in procedure and using callable but now I am trying to use select query in jdbc prepared statement.
I know in preparestatement we write insert into abc values(?,?,?);
but here I have insert-select. same variable has been used many places. in this query I have 2 variable
p_entity_type
and p_update_mode
<!-- language: lang-sql -->
INSERT INTO dynamicEntitynotgett
(entity_type, entity_id, entity_code, synonyms, action)
WITH data_view AS
( -- ITEM table
SELECT 'ITEM' entity_type, -- This separates inserted values
item_id data_id,
item_name data_name,
item_desc data_desc,
creation_date
FROM itemde
UNION ALL
-- ORG table
SELECT 'ORG' entity_type, -- This separates inserted values
org_id,
org_name,
org_desc,
creation_date
FROM orgde
UNION ALL
-- Feature table
SELECT 'FEATURES' entity_type, -- This separates inserted values
FEATURE_id data_id,
FEATURE_NAME data_name,
FEATURE_DESC data_desc,
CREATION_DATE
FROM FEATURESDE
)
SELECT upper(t.entity_type),
t.data_id,
t.data_name,
t.data_desc,
CASE lower(p_update_mode)
WHEN 'INCREMENTAL' THEN
CASE
WHEN t.creation_date > b.last_update_date THEN
'update'
WHEN t.creation_date < b.last_update_date THEN
'add'
END
WHEN 'full' THEN
'add'
END action
FROM data_view t
LEFT JOIN ODA_REFRESH_DETAILS b
ON b.entity_type = t.entity_type
AND lower(p_update_mode )='incremental'
WHERE (upper(p_entity_type) = t.entity_type OR p_entity_type IS NULL)
AND (lower(p_update_mode) = 'full'
OR (lower(p_update_mode) = 'incremental' AND b.entity_type IS NOT NULL)
);
I will receive p_entity_type and p_update_mode from upper stream. which solution would be better? Resultset or Preparedstatement and how can I replace those values in query or use setXXX().
答案1
得分: 1
我认为你正在寻找 namedParameterStatement
。这将允许你对参数进行命名。
我不太确定你在陈述中指的是什么,但例如,这行代码:
SELECT 'ITEM' entity_type
可以替换为:
SELECT :ITEM as entity_type
其中 :ITEM
就像一个 ?
一样传递,但可以在语句中多次使用。
英文:
I think you are looking for namedParameterStatement
. This would allow you to name the parameters.
I'm not exactly sure what you are referring to in your statement, but for instance, this line:
SELECT 'ITEM' entity_type
could be replaced with:
SELECT :ITEM as entity_type
where :ITEM
is passed in just like a ?
, but could be used multiple times in the statement.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论