Need to retrieve number of rows affected by SQL query

Hi,
I am executing some update queries in SQL in a xMII transaction. Iam using a SQL Query template for the same and I need to find out how many records were affected by the last executed statement. I have tried doing SELECT @@ROWCOUNT in a second SQL template that executes after the UPDATE SQL Query Template. However, the results are intermittent, sometimes it returns 1 row affected as expected but sometimes returns 0 rows. Can anyone help on this.
Thanks

Anamika,
1) As per sql best practices use of trigger is not recommend unless you don't have any other option because usually triggers consume more resources and IO since it involves internal monitoring.
2) @@ROWCOUNT should go in same SQL session where you are performing update to identify rows affected else as you said earlier your results will not be accurate
3) The other alternative is to create a Stored Procedure but it again boils down to your authorization issue. This would be easier and better if you have authorization to create SP's. But nevertheless give it a try.
4) Lets forget about SP say that you have an Update Statement like
Update Table1 set column2 = 'abc' where Column2 = 'xyz'
In this case it is evident that you know what you are passing into the Where clause and to what table hence its obvious that you also know how many rows will be affected by using query,
Select count(*) from Table1 where Column2 = 'abc'
The @@ROWCOUNT is powerful within t-sql with SP, cursors etc..
Hope this helps!!
Regards,
Adarsh

Similar Messages

  • Need to limit number of rows returned by a query

    Hi. Is there a way to limit the rowsets returned to me in a query? I need to get the first 100, then the next hundred, etc until there are no more rows left to retrieve. Someone told me that I should use a cursor to do this. Does anyone have a specific example? Can this be done with a combination of SQL/JDBC?
    Thanks in advance...bbetta
    null

    if you are talking about limiting the number of rows to be returned to the calling program to a managable number of rowa ...
    for example the result set size would be 5000 rows and you want to get them in batches of 50 or 100 ..
    see the attached link :
    JDBC Code Templates
    http://technet.oracle.com/sample_code/tech/java/sqlj_jdbc/htdocs/templates.htm#BatchSize
    if you want the server side transaction to only return the first 50 or 100 rows even though the result set has more ... then
    another approach is required -- possibly as suggested above ...

  • Get Number of rows from a sql query.

    I am reading data from a sql query in a BLS transaction and I would like to know the number of rows returned.
    Is there an easy way to do this without looping through the table?
    Thanks Jasper

    Hi Jasper,
    You can use the XPATH count function similar to  this:
    GetTagList.Results{count(/Rowsets/Rowset/Row)}
    Kind Regards,
    Diana Hoppe

  • How to get thr number of rows of an SQL query in JSTL

    hi guys,
    can anyone please tell me how do i perform a function which finds out the particular rownumber of an sql query in JSTL.....
    the same code in jsp is:
    <% plateRSet.getRowNum() %>
    how do i do this given plateRset is the var for a given iteration
    thanks
    rahul

    make your result set scrollable...Not such a good idea. With Oracle, the JDBC driver will read and cache all the ResultSet in memory (with other DBMSs/drivers the behavior will probably be different, but you will still have some unnecessary overhead).
    You can do the caching yourself if you think it's worth it. If the piece of code you posted is why you need this for, then use a List or Vector and you won't need to know the size upfront.
    Alin,
    The jTDS Project.

  • Get the number of rows affected by update statement

    Hi
    I'm working on an application that uses OCI as interface against an Oracle database.
    I'm trying to find out how i can get the number of rows affected by a query executed with OCIStmtExecute. it is not a select query.
    Best regards,
    Benny Tordrup

    If I run a bulk UPDATE query using OCIBindArrayOfStruct, is there a way I can tell which+ rows successfully updated?
    I have a file of records of which 75% of PKs are already in the table and will be updated, the other 25% aren't and should be inserted later. I want to attempt an UPDATE statement using OCIBindArrayOfStruct to execute in bulk and then check which entries in my bulk array successfully updated. If an array entry isn't successfully updated then I will assume it should be inserted and will store it for a Direct Path load later.
    Many thanks for any advice you can give as I've been trawling through the docs trying to find a solution for ages now.

  • Fetching row affected by a query

    Hi =)
    I've my execQuery method here that returns a ResultSet of results.
    I would like to know the rows returned, but I cannot find a count, lenght or size method in the ResultSet class.
    �Hoy can i count the number of Rows affected by a query?
    public ResultSet execQuery(String consulta)
             * Variables locales
            ResultSet coleccion = null;
            Statement stmt;
             * Creamos statement y ejecutamos consulta
            try {
                 stmt = conn.createStatement();
                 coleccion = stmt.executeQuery(consulta);
            catch(SQLException ex)
                    this.setError(ex.toString());
                    return null;
            // Devolvemos filas afectadas
            return coleccion;
       }Thx!

    simple answer:
       stmt = conn.createStatement(ResultSet.TYPE_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       coleccion = stmt.executeQuery(consulta);
       coleccion.last();
       int rowCount = coleccion.getRow();note:
    1. Returning a result set is not a good idea.
    2. Why you need to know rows number here. i think its useless
    Message was edited by:
    j_shadinata

  • ExecuteNonQuery doesnt return number of rows affected when used with PL/SQL

    ExecuteNonQuery doesn't return number of rows affected when used with PL/SQL but returns -1. The documentation says ExecuteNonQuery method of OracleCommand returns the number of raws affected but when used with PL/SQL, it returns -1 even the update statement successfully executes and affects multiple records with no exeptions thrown.
    However, if ExecuteNonQuery is used with update sql statement in the asp.net code it returns number of rows affected correctly. Does anybody know why ExecuteNonQuery behaves like this?
    I am using ODP.NET that comes with Oracle Client 11g. The version is 2.111.6.20 of ODP.NET, Oracle database server is 10g.
    Marat

    You can get the rowcount in the stored procedure via SQL%ROWCOUNT and then pass it out as a parameter.
    For example:
    SQL> create or replace procedure updatesal(v_deptno in number, v_rowsaff out number) as
      2  begin
      3  update emp set sal = sal * 5 where deptno = v_deptno;
      4  v_rowsaff := SQL%ROWCOUNT;
      5  end;
      6  /
    Procedure created.Cheers

  • Number of rows returned in a query

    Is there a way to get the number of rows returned from a query without itterating through the itterator?
    Thanks,
    Yon

    No.
    Because rows are not all returned until you start interating.
    Some times I have seen people do a count query before the normal query, this helps out if you need a count to set things up.

  • How to get number of rows return in SELECT query

    i'm very new in java, i have a question:
    - How to get number of rows return in SELECT query?
    (i use SQL Server 2000 Driver for JDBC and everything are done, i only want to know problems above)
    Thanks.

    make the result set scroll insensitve, do rs.last(), get the row num, and call rs.beforeFirst(), then you can process the result set like you currently do.
             String sql = "select * from testing";
             PreparedStatement ps =
              con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
             ResultSet rs = ps.executeQuery();
             rs.last();
             System.out.println("Row count = " + rs.getRow());
             rs.beforeFirst();~Tim
    NOTE: Ugly, but does the trick.

  • How to restrict number of rows returned in a query

    Hi frnds,
    I'd like to restrict number of rows returned by my query to some 10 rows. how to do that.When I try doing with the rownum<10 its giving results for a particular dept and that too some 6 rows only...btw I'm grouping my table and includes joins from many a table and am ordering the table results by a column.. How to do this..

    776317 wrote:
    Hi frnds,
    I'd like to restrict number of rows returned by my query to some 10 rows. how to do that.When I try doing with the rownum<10 its giving results for a particular dept and that too some 6 rows only...btw I'm grouping my table and includes joins from many a table and am ordering the table results by a column.. How to do this..
    TELL ME HOW MANY ROWS YOU HAVE IN TABLE?
    Because you have only *6 rows* in you column, if you less than 10 rows then it displays only containied/exist rows. nothing much
    select ename,empno from emp where rownum < 10;Thanks

  • How to get row number in the fetch row itself in Sql Query ?

    Hi,
    i am fetching some rows from a sql query . Is there any way to get row number as well in each row while all rows are fetched ?
    like this :
    RowNum data1 data2
    1 abc ere
    2 bnh ioi

    Hello
    Ofcourse you can get the rownum inside a query, just keep in mind that the rownum is the number of order in which the records were fetched from the table, so if you do an order by, the rownum will not be sequential, unless you query the information in a subquery first.
    SELECT rown, col1, col1
    FROM table
    Or
    SELECT rownum, col1, col2
    FROM (SELECT col_1, col_2 FROM table ORDER BY col1)
    Regards
    Johan

  • How can I get the number of rows in my sql result, without a loop?

    Hello,
    I've a problem, I would like to get the number of rows in my sql result without make a loop like :
    while (rs.next()){
    int number = rs.getRow();
    Is there any method to do this, on the first element?
    Thx, STeF

    If you want to count how many rows are in result set, I dont think you can, but you could always run a count statement for that sql
    say your sql is
    select field1, field2, field3
    from table1, table2
    where field4=field5 ......
    then you can count the rows this statement returns by
    select count(*) from
    (say your sql is
    select field1, field2, field3
    from table1, table2
    where field4=field5 ......)
    This way you will get the count for just that sql

  • SQL Query - The number of columns specified in "SQL Query" does not match t

    I am creating new UDM for tablespace alert, below is my query,however its failing with error
    SQL Query - The number of columns specified in "SQL Query" does not match the value specified in "SQL Query Output"
    I selected Metric type is number
    SQL Query Format : Two columns
    Query:
    SELECT d.tablespace_name,round(((a.bytes - NVL(f.bytes,0))*100/a.maxbytes),2)
    used_pct FROM sys.dba_tablespaces d,(select tablespace_name, sum(bytes) bytes, sum(greatest(maxbytes,bytes)) maxbytes from sys.dba_data_files group by tablespace_name) a,(select tablespace_name, sum(bytes) bytes from sys.dba_free_space group by tablespace_name) f
    WHERE d.tablespace_name = a.tablespace_name(+) AND d.tablespace_name = f.tablespace_name(+)
    AND NOT (d.extent_management = 'LOCAL' AND d.contents = 'TEMPORARY');
    Any clues why i am getting error.

    SQL> SELECT d.tablespace_name,round(((a.bytes - NVL(f.bytes,0))*100/a.maxbytes),2) used_pct
    2 FROM sys.dba_tablespaces d,(select tablespace_name, sum(bytes) bytes, sum(greatest(maxbytes,bytes)) maxbytes from sys.dba_data_files group by tablespace_name) a,(select tablespace_name, sum(bytes) bytes from sys.dba_free_space group by tablespace_name) f
    3 WHERE d.tablespace_name = a.tablespace_name(+) AND d.tablespace_name = f.tablespace_name(+)
    4 AND NOT (d.extent_management = 'LOCAL' AND d.contents = 'TEMPORARY');
    TABLESPACE_NAME USED_PCT
    MGMT_TABLESPACE .82
    SYSAUX 1.52
    UNDOTBS1 .32
    RMAN .02
    CORRUPT_TS 10.63
    USERS 0
    SYSTEM 2.26
    MGMT_ECM_DEPOT_TS .04
    MGMT_AD4J_TS 0

  • How does Index fragmentation and statistics affect the sql query performance

    Hi,
    How does Index fragmentation and statistics affect the sql query performance
    Thanks
    Shashikala
    Shashikala

    How does Index fragmentation and statistics affect the sql query performance
    Very simple answer, outdated statistics will lead optimizer to create bad plans which in turn will require more resources and this will impact performance. If index is fragmented ( mainly clustered index,holds true for Non clustred as well) time spent in finding
    the value will be more as query would have to search fragmented index to look for data, additional spaces will increase search time.
    Please mark this reply as the answer or vote as helpful, as appropriate, to make it useful for other readers
    My TechNet Wiki Articles

  • How to retrieve number of rows of a table by jdbc?

    can anyone show me a simple snippet of a java code about how to retrieve the number of ROWS in a table?
    or even only showing the jdbc command that does this job would be enough.
    thanks in advance :)
    cheers

    1. query for the count, then execute the query
    "select count(*) from test" then
    "select * from test"
    2. create a scrollable ResultSet then
    rs.last();
    int count = rs.getRow();
    rs.beforeFirst();
    Jamie
    p.s. this topic has been discussed MANY times. Do a search on this forum and you will get lots of other discussions on this topic.

Maybe you are looking for

  • Error while Creating Key Flex Feild

    Hi Experts, I am learning OAF; i want to create KEY FLEXFIELD in OAF page. Steps: 1. Created VO(GLCodeCombinationsVO) -- SQL Query:select code_combination_id from gl_code_combinations_kfv 2. Created AM(GLCodeCombinationAM) and attached VO to AM. 3. C

  • OUAF / MDM 2.0.1 / XAI Inbound Service - SyncRequestInbound

    I am trying to "import" an Device from my system to MDM using D1-SyncRequestInbound, but some errors have occurred. Can someone post an example ? This is my xml: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:d1="ht

  • Blackberry manager 4.1

    I have an issue with the BB Manager 4.1. When I try to add new user and enter the server name, it gives error "unable to find path to the server". Lotus Notes is configured locally on this machine and the servers are accessible from Lotus Notes. Is t

  • Can't find Time Capsule

    Ever since Apple techs helped me re-establish connections to my wireless home network, my Time Machine hasn't worked.  It worked before. Under the new configuration the Time Capsule is wired into the modem and I connect to the internet wirelessly thr

  • Facebook notification is stuck

    I have a facebook notification on my home screen "2f"  I have tried everything to clear this and have not been successful.  I have downloaded the newest version.  Checked my messages and notifications from my phone.  What else can I do?  I have had t