ColdFusion 9 ORM Update multiple rows at one time - orm

Whats the best way to do a query like this, which will update more than one row:
UPDATE B
SET col_a = 1
Where col_a = 0
I can load the entities then loop them to do the EntitySave for each one, however this seems like overkill. Should I use HQL to do the update? Can I use HQL to do the update?

You can absolutely run an update on more than one row using ORMExecuteQuery. This is an example of a project I worked on:
ORMExecuteQuery("UPDATE Part SET ReleaseDate = :ReleaseDate, ChangeNote = :ChangeNote WHERE ID IN (#Arguments.PartIDs#)", {ReleaseDate = Arguments.ReleaseDate, ChangeNote = Arguments.ChangeNote});
Easy as that... Notice that you specify the parameters as :VarName and feed them in as the second argument of the ORMExecuteQuery function.

Related

Oracle Apex update specific row of table

Hi i want to update a specific row of my oracle database via apex. So i need to reach/address the specific row via where clause.
example entries:
GATTUNG
ART
UNTERART
ABART
VARIETÄT
AAA
AAA
AAA
NULL
NULL
AAA
AAA
NULL
NULL
AAA
AAA
AAA
NULL
NULL
NULL
Now i have two approaches.
first:
UPDATE TBL_PFLANZE
SET NAMEN =:P1_NAMEN
WHERE (GATTUNG = :P1_GATTUNG AND ART = :P1_ART_KREUZUNG)
AND ((UNTERART=:P1_UNTERART OR :P1_UNTERART IS NULL) AND (VARIETÄT=:P1_VARIETAET OR :P1_VARIETAET IS NULL) AND (ABART=:P1_ABART OR :P1_ABART IS NULL));
second:
UPDATE TBL_PFLANZE
SET NAMEN ='&P1_NAMEN.'
WHERE (GATTUNG = '&P1_GATTUNG.' AND ART = '&P1_ART_KREUZUNG.')
AND (UNTERART &P1_WHERE_UNTERART. AND VARIETÄT &P1_WHERE_VARIETAET. AND ABART &P1_WHERE_ABART.);
the differences of both approaches are the P1_WHERE_...variables.
the first one use for example :P1_UNTERART and contains the whole value
the second one use for example &P1_WHERE_UNTERART. and contains = '&P1_UNTERART.' OR IS NULL
my problem is:
the first one updates all entries if i only set GATTUNG and ART (if i do not specify one of the other variables)
the second will work, but is not the right approach as I should be using binding variables instead of &VAR.
So my question is, how can i use the first approach which the desired result... :(
Does this do the trick ? It will update the rows if any of the columns have the same value as the corresponding page item or the database column and page item are both null:
UPDATE tbl_pflanze
SET
namen = :P1_NAMEN
WHERE
( gattung = :P1_GATTUNG AND art = :P1_ART_KREUZUNG)
AND
( ( NVL(unterart,'X') = NVL(:P1_UNTERART,'X')) AND
( NVL(varietät,'X') = NVL(:P1_VARIETAET,'X')) AND
( NVL(abart,'X') = NVL(:P1_ABART,'X')) );
The way I see it, the 1st query is correct while the 2nd is wrong.
Why is it wrong? Because you're using strings. This:
SET NAMEN ='&P1_NAMEN.'
WHERE (GATTUNG = '&P1_GATTUNG.'
would want to put litally &P1_NAMEN. string into the NAMEN column for all rows whose GATTUNG column contains literally &P1_GATTUNG. string in it (and there's most probably none, so the 2nd query won't do anything, ever).
So, what is your problem, exactly? Why do you think that the 1st query doesn't work? Did you run the page in debug mode and reviewed debug results? Could it be that P1 page's items aren't stored into session state so UPDATE statement doesn't see them?
Also, where do you execute that UPDATE? As a process which runs after button is pressed? Something else?

SQL statement that returns sums of records and returns both make names in SQL Server

I'm a big confused on this one. I'm trying to use a SQL statement that sums up two different records but displays both 'Makes' of each row. I currently have this statement..
SELECT
tDealerships.CapShortName,
SUM(tObjective.CommitObj) AS CommitObj,
SUM(tObjective.ActualMTD) AS MTD,
SUM(tObjective.DelToday) AS DelT,
SUM(tObjective.ActualMTD)/Sum(tObjective.CommitObj) AS CommitUnit,
SUM(tObjective.CommitGrossObj) AS CommitGrossObj,
SUM(tObjective.GrossActual) AS GrossActual,
SUM(tObjective.GrossActual)/Sum(tObjective.CommitGrossObj) as CommitGross,
Sum(tObjective.ActualMTD)/Sum(tObjective.GrossActual) AS MTDPRU
FROM
tObjective, tMake, tDealerships
WHERE
tObjective.DealershipID = 10
AND NewUsed = 'New'
AND tObjective.MakeID = tMake.MakeID
AND tObjective.DealershipID = tDealerships.DealershipID
AND (tMake.Make LIKE '%BUICK%' OR tMake.Make LIKE '%GMC%')
GROUP BY
tDealerships.CapShortName
which returns this..
However, I need to display the two makes that it is summing up which is Buick and GMC.
If I add the make in the statement, I must group by the make which then separates them into two row sums.. one for Buick and then one for GMC. Is there a better way or doing this? I am able to make it do what I need it to do? I have been stuck on this one for a bit now. Any suggestions/help is greatly appreciated!
EDIT: The ideal result is having 1 result with an additional column named Make that displays both Buick/GMC together.
You had to hard-code the makes for the LIKE strings. Why not just hard code them in a literal for the make column?
SELECT
tDealerships.CapShortName,
SUM(tObjective.CommitObj) AS CommitObj,
SUM(tObjective.ActualMTD) AS MTD,
SUM(tObjective.DelToday) AS DelT,
SUM(tObjective.ActualMTD)/Sum(tObjective.CommitObj) AS CommitUnit,
SUM(tObjective.CommitGrossObj) AS CommitGrossObj,
SUM(tObjective.GrossActual) AS GrossActual,
SUM(tObjective.GrossActual)/Sum(tObjective.CommitGrossObj) as CommitGross,
Sum(tObjective.ActualMTD)/Sum(tObjective.GrossActual) AS MTDPRU
------
'GMC/Buick' As Make
------
FROM tObjective, tMake, tDealerships
WHERE tObjective.DealershipID = 10
AND NewUsed = 'New'
AND tObjective.MakeID = tMake.MakeID
AND tObjective.DealershipID = tDealerships.DealershipID
AND (tMake.Make LIKE '%BUICK%'
OR tMake.Make LIKE '%GMC%')
GROUP BY tDealerships.CapShortName
I think that something as simple as grouping on the make as well would solve your problem, if I am reading it correctly?
SELECT
tDealerships.CapShortName,
SUM(tObjective.CommitObj) AS CommitObj,
SUM(tObjective.ActualMTD) AS MTD,
SUM(tObjective.DelToday) AS DelT,
SUM(tObjective.ActualMTD)/Sum(tObjective.CommitObj) AS CommitUnit,
SUM(tObjective.CommitGrossObj) AS CommitGrossObj,
SUM(tObjective.GrossActual) AS GrossActual,
SUM(tObjective.GrossActual)/Sum(tObjective.CommitGrossObj) as CommitGross,
Sum(tObjective.ActualMTD)/Sum(tObjective.GrossActual) AS MTDPRU
FROM tObjective, tMake, tDealerships
WHERE tObjective.DealershipID = 10
AND NewUsed = 'New'
AND tObjective.MakeID = tMake.MakeID
AND tObjective.DealershipID = tDealerships.DealershipID
AND (tMake.Make LIKE '%BUICK%'
OR tMake.Make LIKE '%GMC%')
GROUP BY tMake.Make, tDealerships.CapShortName

MS SQL complex update query

I've multiple tables and try to update some tables based on matching translation in master tables. I could update the table based on the first available translation, but I would like to do it based on the most frequent one. I found quite a lot of samples on how to achieve that in simple queries, but can't have it work in this complex query below.
In brief I just need to take the most frequent translation [target] from [EC3_800_FR_M] and copy it to [EC3_800_FR], instead of taking the first occurence in the table.
UPDATE [ec3_800_fr]
SET [ec3_800_fr].[target] = (SELECT TOP 1
[ec3_800_fr_m].[target]
FROM [ec3_800_fr_m]
WHERE (
[ec3_800_fr].[enus] = [ec3_800_fr_m].[enus] AND
[ec3_800_fr].[length] = [ec3_800_fr_m].[length] AND
[ec3_800_fr].[key6_domainname] = [ec3_800_fr_m].[key6_domainname] AND
[ec3_800_fr_m].[status] > 1
)
);
who can help me???
merci - thank you - Dank u - Danke
BR
lolo

ORA-00904 Invalid Identifier - Update Statement With Two Tables

I'm working with PeopleSoft Campus Solutions, and we need to update about 22,000 rows of data. This is data between the tables of ACAD_PLAN_VW and ACAD_PROG. Students are listed on both, so their IDs match between the two.
Basically what we are trying to do is say that when the ID, academic career, student career number, effective sequence, and effective date match, AND where the academic plan (their degree, as stored on the ACAD_PLAN_VW) is a specific value, update the ACAD_PROG on the ACAD_PROG table to X value.
I tried to do some very interesting combinations of FROM statements, constantly getting errors. After some researching, I found out that SQLTools doesn't really like FROM statements within UPDATE statements, so I rewrote it to just make the connections manually. I'm assuming I'm doing this right, unless I need to reword it.
The statement I have is:
UPDATE PS_ACAD_PROG SET PS_ACAD_PROG.ACAD_PROG = 'UGDS'
WHERE PS_ACAD_PLAN_VW.EMPLID = PS_ACAD_PROG.EMPLID
AND PS_ACAD_PLAN_VW.ACAD_CAREER = PS_ACAD_PROG.ACAD_CAREER
AND PS_ACAD_PLAN_VW.STDNT_CAR_NBR = PS_ACAD_PROG.STDNT_CAR_NBR
AND PS_ACAD_PLAN_VW.EFFSEQ = PS_ACAD_PROG.EFFSEQ
AND PS_ACAD_PLAN_VW.EFFDT = PS_ACAD_PROG.EFFDT
AND PS_ACAD_PLAN_VW.ACAD_PLAN = 'DSTDS'
Theoretically, I would assume that this would update any student who has those connections. However, the error that I'm currently getting is as follows:
ORA-00904: "PS_ACAD_PLAN_VW"."ACAD_PLAN": invalid identifier
I have, as of yet, been unable to figure out the issue. I do have the correct access to view and update those fields, and the field does indeed exist.
Oracle doesn't know it should use the PS_ACAD_PLAN_VW table. Somehow you should reference it.
For example can you try this way?
UPDATE (
select
PS_ACAD_PROG.ACAD_PROG,
PS_ACAD_PLAN_VW.ACAD_PLAN
from
PS_ACAD_PROG,
PS_ACAD_PLAN_VW
where
PS_ACAD_PLAN_VW.EMPLID = PS_ACAD_PROG.EMPLID
AND PS_ACAD_PLAN_VW.ACAD_CAREER = PS_ACAD_PROG.ACAD_CAREER
AND PS_ACAD_PLAN_VW.STDNT_CAR_NBR = PS_ACAD_PROG.STDNT_CAR_NBR
AND PS_ACAD_PLAN_VW.EFFSEQ = PS_ACAD_PROG.EFFSEQ
AND PS_ACAD_PLAN_VW.EFFDT = PS_ACAD_PROG.EFFDT
)
SET
ACAD_PROG = 'UGDS'
WHERE
ACAD_PLAN = 'DSTDS'
Try to use the table which is not getting identified,by keeping in where clause so that you can use the select statement.That will help identifying the tables.
The following implementation should work:
UPDATE PS_ACAD_PROG
SET ACAD_PROG = 'UGDS'
WHERE ROWID IN(SELECT PROG.ROWID
FROM PS_ACAD_PROG PROG
, PS_ACAD_PLAN_VW PLAN
WHERE PLAN.EMPLID = PROG.EMPLID
AND PLAN.ACAD_CAREER = PROG.ACAD_CAREER
AND PLAN.STDNT_CAR_NBR = PROG.STDNT_CAR_NBR
AND PLAN.EFFSEQ = PROG.EFFSEQ
AND PLAN.EFFDT = PROG.EFFDT
AND PLAN.ACAD_PLAN = 'DSTDS');

Multiple actions in a single Query in MS_Access

Is it possible for me to do multiple actions in a single query or do I have to make a query for every change I would like to make?
I have a Column "Type" wich can have the string values "BOOL", "WORD" or "DINT"
In one single query, I would like to:
change all the "BOOL" into "DIGITAL"
change all the "WORD" into "UINT"
change all the "DINT" into "LONG"
Is it possible to do this in 1 single query (if yes, how?)
OR do i have to make several queries like this:
UPDATE DB_Total SET Type = 'DIGITAL'
WHERE Type='BOOL';
Thanks in advance!
Consider Switch() as an alternative to nested IIf() expressions.
With this data in DB_Total ...
id Type
1 BOOL
2 abc
3 <-- Type is Null
4 WORD
5 DINT
... this query updates DB_Total as shown below ...
UPDATE DB_Total
SET [Type] = Switch(
[Type]='BOOL','DIGITAL',
[Type]='WORD','UINT',
[Type]='DINT','LONG'
)
WHERE [Type] IN ('BOOL', 'WORD', 'DINT');
DB_Total after:
id Type
1 DIGITAL
2 abc
3
4 UINT
5 LONG
However you may find a different approach more convenient. Create a replacements table:
id old_type new_type
1 BOOL DIGITAL
2 WORD UINT
3 DINT LONG
Then this UPDATE statement will produce the same changes to DB_Total as the Switch() version.
UPDATE DB_Total AS d
INNER JOIN replacements AS r
ON d.Type = r.old_type
SET d.Type = [r].[new_type];
In the future, if you need to change or add/remove pairs of word replacements, you would only need to edit the replacements table. You would then not need to revise the query.
You can use a Nested IIF statement:
UPDATE DB_Total
SET Type = IIF(Type='BOOL','DIGITAL',
IIF(Type='WORD','UINT',
IIF(Type='DINT','LONG',Type)
)
)
WHERE Type IN ('BOOL', 'WORD', 'DINT');
(I'd usually write the IIF part all on one line, but I've done line breaks just so it is easier to read)