Sql Case expression in JPA criteria update - sql

I have a named query :
"UPDATE student SET student.marks = " +
" CASE WHEN student.name in :" + nameListOne +
" THEN 10 ELSE marks " +
" END , student.class = 6 WHERE student.name in :" + namelistOneAndTwo
how can i achieve this using criteria update.
This is just a example i want to understand how case expression can be used in criteria update.

Might be easier just to have two updates. Not sure what "nameListOne" is, I'm assuming this is some parameter in a dynamic SQL query string.
UPDATE student
SET marks = 10
WHERE name IN nameListOne;
UPDATE student
SET class = 6
WHERE name IN nameListOneAndTwo;
There's some value in keeping things simple and intuitive.

Related

how can join two query in PostgreSQL

I want to join two querys into one query.
What retrieved in the first query is a tables with column of resourceindex that sorts ascending:
String loadRates = "SELECT * FROM ratings WHERE userindex="
+ uindex
+ " ORDER BY rank DESC";
And in the second query, what should retrieved is rows of resourceindexes:
String loadResources = "SELECT * FROM resourceinfo WHERE resourceindex = "
+ rs.getInt("resourceindex");
How can I combine these into a single query?
Do not use old style join but use the keyword join.
Never ever write an SQL string like that with concatenation of parameters but use parameters instead.
"SELECT * FROM public.resourceinfo"
+ " inner join public.ratings ON ratings.resourceindex = resourceinfo.index"
+ " WHERE ratings.userindex = $1" +
+ " ORDER BY ratings.rank DESC;";
How you would apply the parameters depend on the language you are using which you didn't tag.
EDIT: If you meant it would also filtered by a resourceindex parameter then add it too as:
AND resourceinfo.index = $2

SQL select column if attribute is true

I'm overthinking a lot about this, but what I'm trying to do is to select only the columns that is true from object.attribute.
Scenario: I have one function java, inside of this function I have an object that has 50 attribute that has true or false. I'm trying to create a dynamic query that select only the columns if object.attribute is true. For example, Person is my object and has 50 attribute, name, age, sex, wage and so on. If Person.name is true, Person.sex is true, Person.wage is false, Person.age is false, select the name and sex column only.
I tried to use String sql = "...IF ELSE ..." or String sql = "...CASE WHEN..." but I couldn't create the logic where the true come in, if do column_name = object.attribute(true), will return null. Is it better to create a procedure? Or it can done in a query? How can I achieve this?
If only shows name and sex, must appear "John", "31" as example
You would need to craft dynamic SQL and use parameters to properly do this. So you would be building a SQL string for the SELECT, but any non-static parameters needed for the query would be parameters. to the query itself.
So, you would have something in code like:
String Sql = ""
Sql=sql + " SELECT "
IF attribute.value==true then Sql=sql + value.name + " "
IF attribute.value==true then Sql=sql + value.name + " "
IF attribute.value==true then Sql=sql + value.name + " "
Sql=sql + "FROM TABLE WHERE StaticField=StaticValue or StaticField=#Var1"
You never should concatenate user input; that is where the parameters (#Var1 in the example above) come into play.

My C# SQL Select statement is not picking up any records in my table

I have 4 records in my SQL table that and I'm doing a SQL select statement to select records based on a certain criteria but I'm not picking any records up. Can someone please help?
Here is my SELECT statement:
string Sql = "";
Sql = #"SELECT * FROM " + _dtlName + "
WHERE Shipper_No = '" + sidNo + "'
AND Job_Code != '" + "R" + "'";
I have 3 records that have a Null for Job_Code and 1 record that has an R.
Based on that, I should pickup the 3 records with the NULL Job_Code but it returns 0 records.
I suspect the problem is that a comparison between any non-null value and a null value doesn't return a value of true or false, but null. So your query should probably be:
string sql = "SELECT * FROM " + _dtlName + " WHERE Shipper_No = #ShipperNo " +
"AND (Job_Code IS NULL OR Job_Code != 'R')";
(Note that I've extracted a parameter for Shipper_No - you shouldn't be including values directly in your SQL like that. Obviously you'll need to then set the parameter value in the SQL command.)
Also note that <> is more common in SQL to represent "not equal to", but I believe T-SQL allows either form.
Try,
Sql = #"SELECT * FROM " + _dtlName + " WHERE Shipper_No = '" + sidNo + "' AND Job_Code NOT IN ('R')";
I think your query should be more like this since you want to select the NULL values.
string Sql = String.Format("SELECT * " +
"FROM {0} " +
"WHERE Shipper_No = {1} AND " +
" Job_Code IS NULL",
_dtlName, sidNo)

How do I get around this common SQL problem

Haven't come across this in ages and when I searched for the solution I couldn't find one. I think its called overloading in SQL. Basically when I have "" (an empty string) for any parameter in this SQL I don't want to set a value in the database...
NOTE: I want to do it at a SQL level not do it at a C# level because its sloppy that way.
string Sql = "IF NOT EXISTS (SELECT * FROM tbl_FileSystemReferences) "
+ "INSERT INTO tbl_FileSystemReferences (UploadDir) VALUES (null) "
+ "UPDATE tbl_FileSystemReferences SET "
+ "UploadDir=#UploadDir, "
+ "ThumbnailDir=#ThumbnailDir, "
+ "ArchiveDir=#ArchiveDir, "
+ "RealDir=#RealDir, "
+ "FlashDir=#FlashDir, "
+ "AssociatedFilesDir=#AssociatedFilesDir, "
+ "EnableArchiving=#EnableArchiving, "
+ "AppWideDir=#AppWideDir, "
+ "FFmpegDir=#FFmpegDir, "
+ "InstallationDir=#InstallationDir ";
SqlCommand Command = new SqlCommand(Sql);
Command.Parameters.AddWithValue("#UploadDir", f.UploadDir);
Command.Parameters.AddWithValue("#ThumbnailDir", f.ThumbnailDir);
Command.Parameters.AddWithValue("#ArchiveDir", f.ArchiveDir);
Command.Parameters.AddWithValue("#RealDir", f.RealDir);
Command.Parameters.AddWithValue("#FlashDir", f.FlashDir);
Command.Parameters.AddWithValue("#AssociatedFilesDir", f.AssociatedFilesDir);
Command.Parameters.AddWithValue("#EnableArchiving", f.EnableArchiving);
Command.Parameters.AddWithValue("#AppWideDir", f.AppWideDir);
Command.Parameters.AddWithValue("#FFmpegDir", f.FFmpegDir);
Command.Parameters.AddWithValue("#InstallationDir", f.InstallationDir);
ExecuteNonQuery(Command);
I know there is a way I used to do this with stored procedure I just cant remember how (I think it's called overloading)....
Cheers,
Can you create a stored procedure rather than passing the command as text?
That way you can break each of the lines like "UploadDir=#UploadDir," into its own variable and only add it to the command if it is not null or not empty string
one way would be on a stored procedure, where you would receive all those parameters, then before the query either:
you allow to pass null
you convert each parameter to null if they are empty as:
select #UploadDir = null where #UploadDir = ''
you would do that for all your parameters, then on update query:
IF NOT EXISTS (SELECT * FROM tbl_FileSystemReferences)
INSERT INTO tbl_FileSystemReferences (UploadDir) VALUES (null)
UPDATE tbl_FileSystemReferences SET
UploadDir=coalesce(#UploadDir, UploadDir),
ThumbnailDir=coalesce(#ThumbnailDir, ThumbnailDir),
ArchiveDir=coalesce(#ArchiveDir, ArchiveDir),
RealDir=coalesce(#RealDir, RealDir),
FlashDir=coalesce(#FlashDir, FlashDir),
AssociatedFilesDir=coalesce(#AssociatedFilesDir, AssociatedFilesDir),
EnableArchiving=coalesce(#EnableArchiving, EnableArchiving),
AppWideDir=coalesce(#AppWideDir, AppWideDir),
FFmpegDir=coalesce(#FFmpegDir, FFmpegDir),
InstallationDir=coalesce(#InstallationDir, InstallationDir)

Convert SQL to HQL?

How can I convert the following SQL query into HQL?
SELECT count(sa.AID)
FROM A sa
, B sal,C m
WHERE sa.AID = sal.AID(+) and sa.BID = m.BID and sa.AID ='0001'
You need to transfer each table/column into it's associated Entity/Class in JAVA, then build the query with Hibernate ORM as below.
Suppose
- The entity name for the table sa is saEntity, for the table B is bEntity, and for the table C is cEntity.
- The class name for the column AID is AidClass, and for the column BID is BidClass
Then the Hibernate ORM query can be written as per the following (I like formating HQL queries inside annotations on multiple lines to make it easier to read & adapt).
#Query( "SELECT COUNT(sa.AidClass) "
+ "FROM saEntity sa, "
+ " bEntity sal "
+ " cEntity m"
+ "WHERE sa.AidClass = sal.AidClass"
+ " AND sa.BidClass = m.BidClass "
+ " AND sa.AidClass ='0001'")
public List <?> runMyQueryMethod();
Try looking at the answer to this question.
HQL to SQL converter
Or this article may help..
https://forum.hibernate.org/viewtopic.php?t=972441
If you showed some of the mappings I could probably help with the HQL. You could just use
Session.CreateSQLQuery instead??