I connected to database in my java program. I have a situation where I need to pass filter values dynamically, filter values depends of others parts of java code
Example query: select * from table1 where id in (dynamic and multiple)?
How to pass these dynamic and multiple values using Java connection.
Try this
String query = "select * from emp where id in(##)";
create the in clause like this
String inClause = "'abcd', 'cedf', '1234'";
String finalQuery = query.replace("##", inClause );
If you are using iBatis, you can try with below sql query:-
<select id="table1Result" resultMap="table1Map">
select * from table1 where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"</select>
While calling it from java, pass a List of ids.
Related
Postgres query:
select t
from TableName t
where t.isActive=true
order by t.extcol->'desc'
This work's fine.
Need help with HQL query which will do the same job.
String query = "select t from TableName t where t.isActive=true order by t.extcol->'desc'"
newList = TableNameModel.executeQuery(query);
Method threw 'java.lang.IllegalArgumentException' exception.
context: there is a table TableName where one column is extcol which contains object. Need to sort TableName based on a property desc which present in extcol json object.
For HQL you have the wrong syntax for sorting.
It should be order by t.extcol desc without the arrow and quotes.
see https://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-ordering
I try to formulate the following PostgreSQL Query into JOOQ:
SELECT * FROM
(SELECT *, row_number() OVER (PARTITION BY propertyid ORDER BY validfrom DESC)
AS rownum FROM propertyvalue WHERE validfrom <= '1978-01-01T00:00:00Z') X
WHERE rownum = 1;
So I want to write a query with a nested query which uses window functions and start with the inner query first. I came that far:
ctx.select(PROPERTYALUE.fields(), DSL.rowNumber().over().
partitionBy(PROPERTYVALUE.propertyid).orderBy(PROPERTYVALUE.validfrom).???)
After the orderBy command, I don't have a DESC option to be used anymore in JOOQ. There are just commands like rangeFollowing or rowsCurrentRow and an inherited command like desc() which returns a SortField-object on which I can't call my required subsequent commands anymore.
So is there any way in JOOQ to formulate my desired SQL query with the DESC order in a window function?
Write this:
DSL.rowNumber()
.over()
.partitionBy(PROPERTYVALUE.propertyid)
.orderBy(PROPERTYVALUE.validfrom.desc()))
Observe the fact that I called desc() on the column validfrom in order to pass the resulting SortField to the orderBy() clause of the window function call.
What you tried to do would have been to order by the window function expression, e.g. in an ORDER BY clause of the SELECT statement. E.g.:
SELECT *
FROM propertyvalue
WHERE validfrom <= '1978-01-01T00:00:00Z'
ORDER BY row_number() OVER (PARTITION BY propertyid ORDER BY validfrom) DESC
I need some help writing an SQL statement for the below requirement.
I have list of employee_id which I need to check whether they are exist in the DB or not from the java layer I want to use one query for this.
Sample query:
SELECT *
FROM employee
WHERE employee_id IN (1001,1002,1002,10000004).
In this query 10000004 does not exist in DB.
One approach in my mind is to use the below query:
SELECT Count(employee_id)
FROM employee
WHERE employee_id IN (1001,1002,1002,10000004).
Then check the list size and the result from the query in java layer. But I don’t want this because I need all those employee_id which does not exist in DB.
declare #employeeids varchar(1000) --to store ids as comma seperated string
declare #tmpEmployee table (employee_id varchar(50)) --temp employee table to store ids from string
declare #pointer int
select #employeeids = '1001,1002,1002,10000004' --list of ids to check against database
while (charindex(',', #employeeids, 0) > 0)
begin
set #pointer = charindex(',', #employeeids, 0)
insert into #tmpEmployee (employee_id)
--remove white spaces if exists
select ltrim(rtrim(substring(#employeeids, 0, #pointer)))
set #employeeids = stuff(#employeeids, 1, #pointer, '')
end
insert into #tmpEmployee (employee_id)
select ltrim(rtrim(#employeeids))
select r.employee_id -- required ids which does not exists in database
,e.employee_id
from #tmpEmployee r
left join employee e on r.employee_id=e.employee_id
where e.employee_id is null
If you are using Oracle, then this link may help you. It's all about usage of built in SYS.DBMS_DEBUG_VC2COLL function
Exist a very bad way to do that is this:
SELECT * FROM (SELECT 5930 id UNION SELECT 8109 id
UNION SELECT 8110 id UNION SELECT 8115 id UNION SELECT 8112 id
UNION SELECT 8113 id UNION SELECT -1 id) b
WHERE b.id NOT IN (SELECT f.id FROM employee f)
I recommed you do that in other way.
I have a list of Integers which I want to (batch) insert into an SQL Server table with a single integer column.
The problem is that some of the values being inserted might already exist in the table. Is there a way of performing a batch "insert if missing" into Sql Server and MyBatis?
The following mapper worked for me :
<insert id="batchAddIntegers" parameterType="java.util.List">
DECLARE #ValuesToInsertTempTable TABLE (ColumnName integer)
DECLARE #UpdateVariable integer
SET NOCOUNT ON
INSERT INTO #ValuesToInsertTempTable (ColumnName) VALUES
<foreach item="item" index="index" collection="list" open="(" separator="),(" close=")">
#{item}
</foreach>
SET NOCOUNT OFF
MERGE TargetTable
USING #ValuesToInsertTempTable AS S
ON TargetTable.ColumnName=S.ColumnName
WHEN NOT MATCHED THEN
INSERT (ColumnName) VALUES (S.ColumnName)
WHEN MATCHED THEN
UPDATE SET #UpdateVariable = #UpdateVariable + 1;
</insert>
I've tried to insert a list in a database but I've got some the error: org.springframework.jdbc.BadSqlGrammarException: SqlSession operation; bad SQL grammar []; nested exception is java.sql.SQLException: ORA-00913: too many values (...).
The code that I've used:
<insert id="insertListMyObject" parameterType="java.util.List" >
INSERT INTO my_table
(ID_ITEM,
ATT1,
ATT2)
VALUES
<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
#{item.idItem, jdbcType=BIGINT},
#{item.att1, jdbcType=INTEGER},
#{item.att2, jdbcType=STRING}
</foreach>
</insert>
My dao cals the method:
SqlSessionTemplate().insert(MAPPER+".insertListMyObject", parameterList);
Where the parameterList is:
List<MyObjects>.
Does someone have a clue about what's this error? Or if does exists a better way to do multiples inserts operation.
Many thanks!
Set the separator as given below
separator="),("
by using following query you may insert multiple records at a time using Mybatis and Oracle.
<insert id="insertListMyObject" parameterType="map" >
BEGIN
insert into table_name values (11,11);
insert into table_name2 values (11,112);
END;
</insert>
this is how i did for oracle and it works. Note that parameterType=map is not necessary a map it can be anything according to your needs.
config log4j to mybatis ,you can find the bugs.
trying
<insert id="insertListMyObject" parameterType="java.util.List" >
INSERT INTO my_table
(ID_ITEM,
ATT1,
ATT2)
VALUES
<foreach collection="list" item="item" index="index" separator=",">
(#{item.idItem, jdbcType=BIGINT},
#{item.att1, jdbcType=INTEGER},
#{item.att2, jdbcType=STRING})
</foreach>
</insert>
I wonder if you can do this with an oracle INSERT statement. The INSERT with VALUES (other than the one with the subquery) does allow values for one row only!
To do so, try a batch insert. An MyBatis example can be found here.
something like this in your DAO layer might help
public class MyBatisDao
{
private SqlSessionFactory mSqlSessionFactory;
public void insertUsers(List<User> users) {
SqlSession session = mSqlSessionFactory.openSession(ExecutorType.BATCH);
try {
for(User user:users)
{
session.insert("com.you.insertUsers",user);
}
}catch(Exception e) {
} finally {
session.close();
}
}
}
I suggest you to use batch, it is much better, and it is standard. This query will not work on some databases (Oracle for example).