Get last record inserted
Hi i am inserting into 2 tables. I am first inserting into
client id. I then want to get that client id and insert it into the
sale table.
What is the easist way to get this.
You have a couple options depending on your database.
1. The best, in my opinion, is to wrap this functionality
into a stored procedure that will handle the multiple inserts, then
run that stored procedure inside a transaction (or include the
transaction inside the stored procedure).
2. Use the result object returned by cfquery on the first
insert to get the ID of the new record from result_name.IDENTITYCOL
(or whatever property is set for your database). Then use the ID
value in a second cfquery for the sales table.
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_p-q_17.html#1102316
What is your database product and version?
Similar Messages
-
Hi,
Can any one tell me how to get last record from any db(MSEG) table?
reward guaranteed
kakiHello,
Entries have no "natural order" within a database, apart from the primary key. If you need the "last entry" then you need to know what defines the last entry. If the last entry is the one with the highest year (MJAHR) the you could select it like this:
data: ls_mseg type mseg.
select * from mseg into ls_mseg.
where bwart = 321
and ebeln = 400000005
and mjahr = ( select max( mjahr ) from mseg
where bwart = 321
and ebeln = 400000005 ).
"process result in ls_mseg
endselect.
Note: This is a loop because there might be more than one matching entry.
best regards
Roland -
How to determine the last record inserted
hi
I have a table abc ( x number, y number, z date ) columns, there
is no primary or unique key.
I have been inserting values into it. there r some duplicate
values to.
but now I want to modify only the record inserted at the end.
i.e
if initially ABC is having
x y z
10 20 01-nov-2001
11 20 01-nov-2001
out of these records I want to change the value of x to 50 for
the record which was inserted last or shall I say whose z is the
greatest.
I have been trying to do but what I see that the z column is not
getting the time in increasing order. i.e even though I tried to
display the dated in dd-mon-yyyy 24hh:mm:ss format still and I
found that date is not consistant. the record I inserted first
is have a much more value than the record I inserted last.
please help...
Sreekantrowid refers to the current position of the record; if the
record has been moved (and there are any number of reasons why
the database might shift a record) the order of rowid will not
reflect the order of insertion.
the only way to guarantee order of insertion is to timestamp the
record. if I understand the original posting, you have been
doing this.
Now you should have been using SYSDATE. if you are saying that
SYSDATE is giving you inconsistent results then you have a real
problem: has somebody been tinkering with your database server -
changing the system clock or something?
Or are you using some other method to set the value for Z
column? If so, what?
rgds, APC -
How to get last Record ior Total rows in For Loop Cursor ?
Hi Friends
I would like to know , the last record in for loop cursor, i have the code in following format
cursor c1 is
select * from emp;
begin
for r1 in c1 loop
v_total_rec := ? ( i would like to know total rows in the cursor , say for example if cursor has 10 rows, i want10 into this variable )
v_count := v_count +1;
dbms_output.put_line(r1.emp_name);
end loop;
end;
Hope i am clear
Any suggestions?
Thanks
RaviEven though cursor loops are generally a Bad Idea ^tm^ as Dan says, here's an example of how you can get the information you wanted within the query itself...
SQL> ed
Wrote file afiedt.buf
1 declare
2 cursor c1 is
3 select emp.*
4 ,count(*) over (order by empno) as cnt
5 ,count(*) over () as total_cnt
6 from emp
7 order by empno;
8 begin
9 for r1 in c1 loop
10 dbms_output.put_line(r1.ename||' - row: '||r1.cnt||' of '||r1.total_cnt);
11 end loop;
12* end;
SQL> /
SMITH - row: 1 of 14
ALLEN - row: 2 of 14
WARD - row: 3 of 14
JONES - row: 4 of 14
MARTIN - row: 5 of 14
BLAKE - row: 6 of 14
CLARK - row: 7 of 14
SCOTT - row: 8 of 14
KING - row: 9 of 14
TURNER - row: 10 of 14
ADAMS - row: 11 of 14
JAMES - row: 12 of 14
FORD - row: 13 of 14
MILLER - row: 14 of 14
PL/SQL procedure successfully completed.
SQL> -
Formula to get last record entered for a specific code in a field
I work at an inpatient facility where our patients experience several movement actions -- transfers from unit to unit, Leaves, return from leaves, and change in commitment status. We collect all of these actions in one table. For our census report we use a subreport to show the list of patients who are on leave. The problem I am having is pulling the most current movement that was a leave if the last movement action code entered was a change in commitment status. We really should not have put the change in commitment in this table as it is not a movement. When a change in commitment occurs and the patient is on leave, they drop off my report because the last change was not a leave. We have to enter the leave again so it is the last record entered to fix.
The table contains these fields:
Doc_ID
Parent_ID
Folder_ID
Effective Date
From_Unit
To_Unit
Time_of_Change
Leave_status_Code
Move_Action_Code
Commit_Status_Code
The Move_Action_Code field contains these codes:
L = Leave
C = Change in Commitment Status
B = Return From Leave
I = Initial Admission
D = Discharge
R = Recommitment
V = Unit to unit transfer
A patient may have several leaves in the same admission (home visit, then back, etc.). I need a formula to show only most current leave if they haven't returned from leave and need to totally ignore any change in commitment status.
Thanks in advance to everyone for your help in this forum!Hi Jerri,
To identify a patient, I assume it would be Folder_ID. Create a group on this field. Also create a group on Effective Date.
Hide the Detail section and the Group Headers for both of these. You can also hide the Group Footer for Effective Date.
In the Group Footer for Folder_ID, drop in the Move_Action_Code and any other fields you want.
When you look at your report, this will show you the last Move_Action_Code for that patient.
If you can group your data properly, the Group header will always show the first record and the Group Footer will always show the last record.
Hope this helps,
Brian -
hi this is my table
select * from emp0;
NAME ID AGE SALARY DOJ
karthik 101 25 260000 01-JAN-04
mano 102 28 156000 08-FEB-04
padhu 103 26 50000 05-MAR-05
suresh 104 21 15000 01-JAN-05
kumarn 105 24 16000 08-MAY-06
sasmitha 107 4 1 11-FEB-06
6 rows selected.
how can i fetch last inserted row and last row in my table?tell me the query please,
sorry for putting this in a new thread, i dnt know how to reply to the moderator in my post,that's why i am using new threadThere isn't any easy way to do that. You can use a updt_timestamp column int he table and use a trigger for insertion. Or take a sequence primary key and find the max value. Or you can use the ROWID for this purpose. Like the one below.
SQL>select * from emp where
2 rowid = (select max(rowid) from emp)
3 /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
9999 ADAMS CLERK 9999 12-JAN-83 00:00:00 7000 20
SQL>Cheers
Sarma. -
How to get the last record from the database
I am using MS Access database and Swings as GUI. I want to get the last record of a particular column from the table and store it as a varaible.
Hi
To get Last record of resultset, you have pass some parameter in constructor of CreateStatement.In such case Resultset should be scrollable and Readonly
Example
objStatement=objCon.createStatement ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
mwwResultSet=cwwStatement.executeQuery(mwwSqlQuery);
while(mwwResultSet.next())
if(mwwResultSet.isLast())
//Fetch the required column record.
String abc=mwwResultSet.getString(1);
I think this will work. Try it.
bye -
How to get last row and sum of all columns in one query
Hi ,
is there a way to get last record for a column and sum of all record for another column in the same query.
Best Regards ,You should define your requirements properly for volunteers to help here..
Your data is not good enough to provide you accurate solution. Reason being you dont have a proper column which differentiates between first and last entry.
The solution becomes easy based on your desgin.
I have introduced a grouping column called "id" and a time column called "time_of_insert" (only this way you can confidently say that you can differentiate between first and last (also a fool proof solution) --- you may optionally use sequence(instead of date though if you say you may end up inserting two rows at the same time, then probably sequence would be a better choice to differentiate rather than a timestamp field) etc...)
With your sample data something like this can be done to achieve your desired result.
WITH dataset AS
(SELECT 1 id,10 used, 8 remain,systimestamp+1/24 time_of_insert FROM DUAL
UNION ALL
SELECT 1 id, 1, 7,systimestamp+2/24 FROM DUAL
UNION ALL
SELECT 1 id,2, 5,systimestamp+3/24 FROM DUAL
UNION ALL
SELECT 1 id,1, 0,systimestamp+4/24 FROM DUAL
UNION ALL
SELECT 1 id,0, 0,systimestamp+5/24 FROM DUAL
UNION ALL
SELECT 1 id,1, 4,systimestamp+6/24 FROM DUAL)
SELECT *
FROM (SELECT SUM (used) OVER () sum_all,
FIRST_VALUE (remain)
OVER (PARTITION BY id ORDER BY time_of_insert DESC)
last_row
FROM dataset)
WHERE ROWNUM = 1;
Output:
SUM_ALL LAST_ROW
15 4
Cheers,
Manik. -
Updating only the last record.
Hi,
I have a scenario where we get more than 1 record based on style. Everytime the new record comes in, I need to update the last record inserted. It requires a procedure to do it, but need some help on how to just update the last record only and not all the previous received records.
Here is the scenario.
create table test_1 as
with data_Set as
( select 'ABCD' style, 20080101 date_received, 2 duration,20080301 expire_Date from dual union all
select 'PQRS' style, 20080201 date_received, 2, 20080401 expire_Date from dual
) select * from data_set
so now on next run when i get another record
insert into test_1
select 'ABCD' style, 20080401 date_received, 3 duration,20080701 expire_Date from dual.
now the procedure should insert the incoming record and also update the expire_date for the previous record to a day before date_received for the new record. I am ok doing it when its only 2 records , but when i get another record of same style, i dont want to update all the previous received records. I only want to update the last record on file.
can anyone suggest?STYLE DATE_RECEIVED DURATION EXPIRE_DATE SEQ_INSERTED
110427 7/31/2006 0 7/31/2006 2
110427 9/1/2007 12 9/1/2008 2
110427 8/2/2008 24 8/2/2010 3
468130 3/13/1997 0 2
468130 3/13/1997 12 3/13/1998 2
468130 1/12/2008 12 1/12/2009 3
Here is my data set
and here is the query i use to update, does it look correct?
update test b
set ( expire_Date) =
(select start_date - 1
from test a
where (style_no, date_received) in
(select style_no, max(date_received)
from test c
group by style_no
and a.style_no = b.style_no
and style_no in ('468130','110427') -
Sql query - Selecting last recorded values for each date in specified period
Hello,
Can someone please help me with my problem.
I'm trying to get last recorded balance for each day for specific box (1 or 2) in specified period of days from ms access database using ADOTool.
I'm trying to get that information with SQL query but so far unsuccessfully...
My table looks like this:
Table name: TestTable
Date Time Location Box Balance
20.10.2014. 06:00:00 1 1 345
20.10.2014. 12:00:00 1 1 7356
20.10.2014. 18:45:00 1 1 5678
20.10.2014. 23:54:00 1 1 9845
20.10.2014. 06:00:02 1 2 35
20.10.2014. 12:00:04 1 2 756
20.10.2014. 18:45:06 1 2 578
20.10.2014. 23:54:10 1 2 845
21.10.2014. 06:00:00 1 1 34
21.10.2014. 12:05:03 1 1 5789
21.10.2014. 15:00:34 1 1 1237
21.10.2014. 06:00:00 1 2 374
21.10.2014. 12:05:03 1 2 54789
21.10.2014. 15:00:34 1 2 13237
22.10.2014. 06:00:00 1 1 8562
22.10.2014. 10:00:00 1 1 1234
22.10.2014. 17:03:45 1 1 3415
22.10.2014. 22:00:00 1 1 6742
22.10.2014. 06:00:05 1 2 562
22.10.2014. 10:00:16 1 2 123
22.10.2014. 17:03:50 1 2 415
22.10.2014. 22:00:10 1 2 642
23.10.2014. 06:00:00 1 1 9876
23.10.2014. 09:13:00 1 1 223
23.10.2014. 13:50:17 1 1 7768
23.10.2014. 19:47:40 1 1 3456
23.10.2014. 21:30:00 1 1 789
23.10.2014. 23:57:12 1 1 25
23.10.2014. 06:00:07 1 2 976
23.10.2014. 09:13:45 1 2 223
23.10.2014. 13:50:40 1 2 78
23.10.2014. 19:47:55 1 2 346
23.10.2014. 21:30:03 1 2 89
23.10.2014. 23:57:18 1 2 25
24.10.2014. 06:00:55 1 1 346
24.10.2014. 12:30:22 1 1 8329
24.10.2014. 23:50:19 1 1 2225
24.10.2014. 06:01:00 1 2 3546
24.10.2014. 12:30:26 1 2 89
24.10.2014. 23:51:10 1 2 25
Let's say the period is 21.10.2014. - 23.10.2014. and I want to get last recorded balance for box 1. for each day. The result should look like this:
Date Time Location Box Balance
21.10.2014. 15:00:34 1 1 1237
22.10.2014. 22:00:00 1 1 6742
23.10.2014. 23:57:12 1 1 25
So far I've managed to write a query that gives me balance for ONLY ONE date (date with highest time in whole table), but I need balance for EVERY date in specific period.
My incorrect code (didn't manage to implement "BETWEEN" for dates...):
SELECT TestTable.[Date], TestTable.[Time], TestTable.[Location], TestTable.[Box], TestTable.[Balance]
FROM TestTable
WHERE Time=(SELECT MAX(Time)
FROM TestTable
WHERE Location=1 AND Box=1 );
Tnx!
Solved!
Go to Solution.For loop
following query keep day (here 24 in below query) Variable from ( 1 to 28-29/30/31 as per month)
SELECT TOP 1 TestTable.[Date], TestTable.[Time], TestTable.[Location], TestTable.[Box], TestTable.[Balance]
FROM Test Table.
WHERE Time=(SELECT MAX(Time) FROM TestTable WHERE Location=1 AND Box=1 )
AND DATE = "2014-10-24";
PBP (CLAD)
Labview 6.1 - 2014
KUDOS ARE WELCOMED.
If your problem get solved then mark as solution. -
Compare 2 data sets and display the last record
I have one record in first table and 2 records in second table.
Ex:Table 1:
A 100 200 0
B 200 300 0
Table 2:
A 0 0 100
A 0 0 500
B 0 0 700
In one final template I want to display like below with only 2 records like below.
A 100 200 500
B 200 300 700
Anybody suggest me how to pick only last record if the count is greater than 1 in second table.not sure what are you trying to do
if you want to join two table then sql is more useful IMHO
Anybody suggest me how to pick only last record if the count is greater than 1 in second table.
you can get last record by last() function
<?for-each:SOMETAGorGROUP[position()=last()]?> -
I am fairly new to flex. So please bear with me no this
question.
I would like to get MYSQL ID of the last record I inserted.
MSQL has as function LAST_INSERT_ID() that is connection based and
will return the last id of the last inserted record. My thought was
in the function contained within the cfc to do 2 queries insert the
record and return the last id directly after (see below). However,
I don't know how I would read the results back into flex. I am
unsure how to write the event handler. Thanks
<cffunction name="insertUser" access="remote"
returntype="query" >
<cfargument name="SSN" required="false" type="string"
hint="SSN"/>
<cfargument name="FNAME" required="false" type="string"
hint="FNAME"/>
<cfargument name="LNAME" required="false" type="string"
hint="LNAME"/>
<cfargument name="CLIENTID" required="false"
type="string" hint="NEW CLIENTID"/>
<cfquery name="insUser" datasource="DBT">
INSERT INTO CLIENT (SSN, FNAME, LNAME) VALUES (
<cfqueryparam value="#ARGUMENTS.SSN#"
cfsqltype="CF_SQL_VARCHAR" maxlength="12"/>,
<cfqueryparam value="#ARGUMENTS.FNAME#"
cfsqltype="CF_SQL_VARCHAR" maxlength="20"/>,
<cfqueryparam value="#ARGUMENTS.LNAME#"
cfsqltype="CF_SQL_VARCHAR" maxlength="30"/>)
</cfquery>
<cfquery name="qRead" datasource="DBT">
SELECT last_insert_id() AS CLIENTID
</cfquery>
<cfreturn qRead >
</cffunction>I assume you have LCDS installed with CF and you are using
the remote object service,
<mx:RemoteObject
id="myService"
destination="ColdFusion"
source="full.path.to.cfc"
showBusyCursor="true">
<mx:method name="insertUser"
result="insertUser_result(event)" />
</mx:RemoteObject>
<mx:Scrpit>
private function insertUser_result(event:ResultEvent) {
trace(event.result);
event.result will be the id of the newly inserted record.
BTW, I would highly recommend you looking into the data
management servcie. the SQLAssembler can spare you of the CF code
for simple CRUDs like this. -
How to get the last record??
Hi, I now have to get only the last record from one table
from MS Access.
I was looking for the useful method from API, however,
I cannot get it.
How to get only the last record from one table ???
Please help, thanks.In RDBMS, row order is really not relavent by iteself. Meaning there is no such thing as 'last record' unless it's in the context of a column (e.g. timeStamp).
Of course, rows are inserted and stored in some natural order, but you cannot assume they'll come back the same way in a 'Select *' as they were entered.
So, if you mean last row as in last inserted row, I would add a timestamp field that's set at insert, or use a autonumber column. Either way, have a column that will always contain the highest number or newest timestamp, and then you can build your where clause from that.
For instance, in Access, I have an autonumbered field called 'fred', with other columns. If I want the last record, I simply use the following:
select * from atable where fred in (SELECT max(fred) AS Max FROM atable);
the 'where in' clause will filter to only those records in the subsequent select statment, which of course is only one, the max of the column fred.
bRi -
INSERT works for only last record of the Internal table ...??
I am trying to insert data from an internal table JTAB to a Databse Table
in CRM. The name of the databse table in CRM is CRMD_PARTNER
For this i first declared an internal table JTAB with same structure as that of
the databse table CRMD_PARTNER
DATA: BEGIN OF JTAB OCCURS 0,
INCLUDE STRUCTURE CRMD_PARTNER,
END OF JTAB.
Then I filled my JTAB with the required entries. For filling the JTAB i am
getting data from couple of other tables and filling the internal table JTAB
loop at ktab.
here i am filling values in JTAB and then i say APPEND JTAB.
endloop.
Till here every thing works well and my internal table JTAB has all values that
I need to insert to the CRMD_PARTNER table. Also my JTAB has vales for all primary key fields.
Now I write a condition like below.
if not jtab[] inital.
MODIFY CRMD_PARTNER from TABLE JTAB.
COMMIT WORK.
endif.
Here comes the problem...once this code is executed and once program totally executes...
i always see that only last record in JTAB is being inserted to the databse table CRMD_PARTNER.
When i check in the debugger ..i see that the loop is being executed only once
and the SY-TABIX of JTAB is being always set to the length of JTAB and
that is why only last record is being inseretd to the table CRMD_PARTNER.
Now what should i do ..to insert all records of JTAB to CRMD_PARTNER table?
is there a way i can restet the SY-TABIX and make the modify statement work for all records of JTAB?
Finally i hardcoded the values in JTAB then i dont have any problem and all records of JTAB are being
inserted into the database table.
only if i am dynamically fetching the values into JTAB(by Putting in loop..endloop statements and
pushing values to JTAB and APPENDING JTAB ) i have a problem .
in this case also my JTAB is being correctly filled with values ..but when i try to
insert theses values to CRMD_PARTNER with MODIFY stmt ..only always last record is being inserted.
i tried with INSERT instead of MODIFY and i am getting runtime error.
kindly pease help.
Regards,
Jessica Samyes a@s i am really struggling from last 3 days.
I actually want to create a sales order in CRM and want to
assign ship to party for each line item in a sales order in CRM
for that i used the standard bapi BAPI_BUSPROCESSND_CREATEMULTI and i am able
to create an order successfully, but i see that the ship toparty
that i give at header is being copied to each line item.
but this is not what is expected.
So finally i tracked in which table the ship to party gets stored in each line item
and then tried to insert the records directly into databse. and it worked
but only if i am hardcoding..but not when i am dynamically fecthing the values into JTAB for insertion,
Can yoy help ..if you have any idea why insert/modify is processing only last record?
any help will be highly appreciated.
Regards,
Jessica Sam -
SQL query to get last 10 records in the table?
Hi,
Can anyone tell me the SQL query to get last 10 records in the table?
Thanks!!
MCPPlease, define what "last" means. Sets are unordered by definition, so if you want to retrieve rows from a table in a specific order you need to specify what that order is - e.g. by maintaining a value in a column (or a combination of columns) that you can use in the ORDER BY clause of the SELECT statement.
If, for instance, you kept the time when the row was inserted in a special column (InsertedTime), you could use this in your query like this:
select top (10)
<column list>
from <table or view>
where <restriction(s)>
order by InsertedTime desc;
ML
Matija Lah, SQL Server MVP
http://milambda.blogspot.com
Maybe you are looking for
-
GR-IR difference posting in IR
Hi, I wanted to know how to post invoice for PO quantity when GR quantity is different than PO quantity. E.g PO quantity 10ea GR quantity 9ea Trying to post invoice for 10 ea so that there is a discrepancy between GR and IR, which can then be
-
AMT menu is no longer available in the BIOS.
Hi, I have a T61 machine which had the AMT sub manu in the system BIOS. I enabled AMT but no I can't access the AMT configuration (CTRL+P) as I don't get the prompt for it. and I also can't see the AMT menu in the BIOS. Any suggestions what I can do?
-
Dear All, Can one SWCV be attached to more than one product? Regards, Ashish
-
I have a work pc (laptop) and a home desktop imac. What I want to know is, if I edit photos/video on my pc laptop in photoshop elements, can I transfer them to my desktop imac and include them in photoshop organizer without issue?
-
HT5622 How change creditcards setting in Apple id
In Apple id account heads ago i set creditcards information . I want to change but howto?