Update the query with increment

Hi, How I can update the column r2 with the new number. r2 column which needs to be updated from 300000 with increment 1.
drop table #cric
create table #cric (r1 int, r2 int, rt char(4),rp char(6))
insert into #cric values (111111,0,'GR','ATYOP')
insert into #cric values (222222,0,'GE','ATYOP')
insert into #cric values (100112,0,'TX','ATYOR')
insert into #cric values (100113,0,'TX','ATYOP')
insert into #cric values (100114,0,'TS','ATYOS')
insert into #cric values (100117,0,'TS','ATYOP')
insert into #cric values (100118,0,'TS','ATYOP')
--Output
Select * from #cric
r1    r2    rt    rp
111111    300000    GR      ATYOP
222222    300001    GE      ATYOP
100112    300002    TX      ATYOR
100113    300003    TX      ATYOP
100114    300004    TS      ATYOS
100117    300005    TS      ATYOP
100118    300006    TS      ATYOP
Thanks.

This is easily achieved with the row_number() function:
create table #cric (r1 int, r2 int, rt char(4),rp char(6))
insert into #cric values (111111,0,'GR','ATYOP')
insert into #cric values (222222,0,'GE','ATYOP')
insert into #cric values (100112,0,'TX','ATYOR')
insert into #cric values (100113,0,'TX','ATYOP')
insert into #cric values (100114,0,'TS','ATYOS')
insert into #cric values (100117,0,'TS','ATYOP')
insert into #cric values (100118,0,'TS','ATYOP')
go
WITH numbering AS (
   SELECT r2, rowno = row_number() OVER(ORDER BY r1)
   FROM   #cric
UPDATE numbering
SET    r2 = 30000 + rowno
go
SELECT * FROM #cric
go
drop table #cric
Except that this will not give you the result you are asking for. But there is no apparent indication why you want exactly that order. Hm, maybe
   ORDER BY substring(rt, 1, 1), substring(rt, 2, 1) DESC
But that is a little far-fetched.
If you really mean that the rows are inserted, there is no way to safely do this, unless you have a column that records the insertion order. You could try "ORDER BY (SELECT NULL)" but that really means "any order, I don't care which".
Erland Sommarskog, SQL Server MVP, [email protected]

Similar Messages

  • UPDATING the query in materialized view

    Hi,
    i have a little doubt in Materialized view. i created a materialized view and log with the following query,
    create table test_mv (a number ,b number )
    alter table test_mv add constraint t_pk primary key ( a );
    insert into test_mv values (1,2);
    insert into test_mv values(2,2);
    insert into test_mv values(3,2);
    insert into test_mv values(4,2);
    insert into test_mv values(5,2);
    commit;
    CREATE MATERIALIZED VIEW LOG ON test_mv
    WITH SEQUENCE, ROWID
    *(a,b)*
    INCLUDING NEW VALUES;
    CREATE MATERIALIZED VIEW product_sales_mv
    ENABLE QUERY REWRITE
    AS SELECT  b  from test_mv;
    Now i want to update the query in the MV as 'Select a from test_mv' . for that i tried with
    *'ALTER MATERIALIZED VIEW product_sales_mv AS SELECT a from test_mv;'*
    But it throwing error,
    Error starting at line 5 in command:
    alter  MATERIALIZED VIEW product_sales_mv   AS SELECT  b  from test_mv
    Error report:
    SQL Error: ORA-00933: SQL command not properly ended
    +00933. 00000 - "SQL command not properly ended"+
    *Cause:+   
    *Action:+
    i guess i am doing wrong. kindly help me here. i want to update it without drop and recreate.
    thanks,
    Jeevanand.Ke

    Hi Jeeva,
    No. you cannot add or drop columns to the materialized view using the ALTER Statement.
    To Change the Structure of the view , drop and re-create the materialized view.
    To Alter a materialized view log, You can use the ALTER MATERIALIZED VIEW LOG. By this,
    You can add new columns to a materialized view log.
    ALTER MATERIALIZED VIEW LOG ON sggi_mrps.emp ADD(deptno);Thanks,
    Shankar
    Edited by: Shankar Viji on Aug 24, 2012 2:11 AM

  • Is it possible to update a query with another query?

    I'm trying to update a query with another query (see attached
    code). Here's my setup: I've got a table in an Access database in
    which I enter a string into a form and update. This string
    corresponds to a single record in another table of the same
    datasource. The first table has only one record to provide the
    second, which has many and will have more. Basically what I'm
    wondering is: Is this a valid thing to do in coldfusion? If not
    please help with an alterate method. I'm still a novice at
    coldfusion.
    The overall effect I'm going for is to display the one record
    as a featured truck profile on the web site:
    www.truckerstoystore.net.
    I currently get an error when I try to display the page with the
    current query setup.
    Check this page to see the error:
    www.truckerstoystore.net/currentTOW2.cfm
    Help on this issue is very much appreciated.
    ------------------------------------------------------------------------------------------ -----------------------------------------------------------------------

    I think this is what you are after
    <!--- this query will get all the records from the DB
    --->
    <cfquery name="cTOW" datasource="tow">
    SELECT *
    FROM currentTOW
    <!--- Do you need to find a particular record in the
    database --->
    <!--- If so, then you need a 'where' clause in here
    --->
    </cfquery>
    <!-- Loop the cTOW query for each record returned -->
    <cfloop query="cTOW">
    <!--- For the record returned from the cTOW query you now
    need to update the table --->
    <!-- Update the table -->
    <cfquery name="currentTOW" datasource="tow">
    UPDATE Your tblName
    SET
    Dataname = cTOW.DataValue
    </cfquery>
    </cfloop>
    thats it
    PS: I think your original query needs modifying. To return
    the exact records that you want to update from the original table.
    ie: Primary and foreign key relationship

  • Polling the master detail table and to update the LAST_UPDATED with SYSDATE

    Hi
    The requirement is polling the master detail table where read_flag is null and has to update the LAST_UPDATED with SYSDATE in both tables.
    Refered the MasterDetail and PollingPureSQLSysdateLogicalDelete samples of SOASuite.
    Used the delete polling strategy in polling process and modified the generated TopLink discriptor as follows.
    set the TopLink -> Custom SQL tab -> Delete tab with the following query
    for master table (RECEIVER_DEPT) :
    update RECEIVER_DEPT set READ_FLAG= 'S' , LAST_UPDATED=sysdate where DEPTNO=#DEPTNO
    set the TopLink -> Custom SQL tab -> Delete tab with the following query
    for Detail table (RECEIVER_EMP):
    update RECEIVER_EMP set LAST_UPDATED=sysdate where EMPNO=#EMPNO
    After deploying the bpel process data is updated in master(RECEIVER_DEPT) table with LAST_UPDATED as sysdate and read_flag as S
    however data is deleted in detail(RECEIVER_EMP) table rather than updated records.

    Xtanto,
    I suggest using JSP / Struts. UIX will be replaced by ADF Faces in JDeveloper 10.1.3 and thus I wouldn't suggest new developments to be started with UIX unless time doesn't allow to wait for ADF Faces. In this case develop UIX in an MVC1 model, using the UIX events for navigation because this model seems more likely to be mgratable, according to the UIX Statement of direction on Otn.
    Back to your question. You can create a search form in JSP that forwards the request to a StrutsData Action to set the scope of teh result set. The read only table can have a link or a button to call the detail page, passing the RoewKey as a string.
    Have a look at the Oracle by Example (OBE) tutorials that contain similar exaqmples.
    Frank

  • Updating the Database with datetime stamp

    Hi all,
    I have a scenario where in once sender JDBC channel polls the database , i need to update the table with current datetime stamp for a particular DATE field.
    what will be the update syntax command that i need to add in the sender channel ?
    Update Emp Set DATE =  ? where EmmNo = 100;
    Regards
    Vinay P.

    Hi Guys ,
    The following query is working :
    Update Emp Set DOJ = GetDate() where EmpNo = 1;  O/p = Sep 1 201
    Regarding adding Time as well , which function do i need to use
    Also say for example , i need the Date time stamp format to be updated in the following format : dd.mm.yyyy hh:mm:ss , what will be the syntax
    Regards
    Vinay P.

  • HT1338 i have apps to be updated but when i try to update them a message pops out saying "You have updates for other accounts, please sigh in with the other id". Can some one help me how can i still update the same with the new apple id ive created.

    i have apps to be updated but when i try to update them a message pops out saying "You have updates for other accounts, please sigh in with the other id". Can some one help me how can i still update the same with the new apple id ive created. As i dont have the access to the earlier id anymore.

    You cannot. The apps are assigned to that Apple ID and there is nothing you can do to change that. You could choose to download them again with the new Apple ID, any paid apps will need to be purchased again.
    Hope that helps.

  • I updated the IPAD with the latest update and now when I turned the IPAD on, I get the itunes logo and then nothing else.  How can I fix this to let me use my IPAD?

    I updated the IPAD with the latest update and now when I turned the IPAD on, I get the itunes logo and then nothing else.  How can I fix this to let me use my IPAD?

    YOU ARE IN RECOVERY MODE
    1. Turn off iPad
    2. Turn on computer and launch iTunes (make sure you have the latest version of iTune)
    3. Plug USB cable into computer's USB port
    4. Hold Home button down and plug the other end of cable into docking port.
    DO NOT RELEASE BUTTON until you see picture of iTunes and plug
    5. Release Home button.
    ON COMPUTER
    6. iTunes has detected iPad in recovery mode. You must restore this iPad before it can be used with iTunes.
    7. Select "Restore iPad"...
    Note:
    1. Data will be lost if you do not have backup
    2. You must follow step 1 to step 4 VERY CLOSELY.
    3. Repeat the process if necessary.

  • Im using iphone 4 uk unlock , it has ios 4.2.1 and it does not have software update option.Now i want to update the phone with latest ios is it possible and tel me how ?

    Im using iphone 4 uk unlock , it has ios 4.2.1 and it does not have software update option.Now i want to update the phone with latest ios is it possible and tel me how ?

    The update to iOS 5.0 & higher is an erase/restore deal, so make sure you follow the directions here to update your phone:
    http://support.apple.com/kb/ht4972
    iOS 7.0.4 will be installed.

  • How to update the Query of an existing WEBI document's dataprovider, through the RESTful Web service SDK.

    Hi,
    I am trying to update the Query of an existing WEBI document's dataprovider, through the RESTful Web service SDK.
    For this, first i will get the Dataprovider information,
    Example:
    URI: http://localhost:6405/biprws/raylight/v1/documents/11111/dataproviders/DP0
    Expected result;
    <dataprovider>
         <id>DP0</id>
         <name>Query 1</name>
         <dataSourceId>1234</dataSourceId>
         <updated>2014-04-18T11:55:21.000-08:00</updated>
         <duration>1</duration>
         <isPartial>false</isPartial>
         <rowCount>113</rowCount>
         <flowCount>11</flowCount>
         <dictionary>
              <expression qualification="Dimension" dataType="String">
                   <id>DP0.DO1</id>
                    <name>EmpID</name>
                   <description>Employee ID.</description>
                    <dataSourceObjectId>DS0.DO1</dataSourceObjectId>
              </expression>
              <expression qualification="Dimension" dataType="String">
                   <id>DP0.DO2</id>
                   <name>EmpName</name>
                   <description>Employee Name.</description>
                   <dataSourceObjectId>DS0.DO2</dataSourceObjectId>
              </expression>
         </dictionary>
         <query>SELECT Employee.EmpID, Employee.EmpName FROM Employee</query>
    </dataprovider>
    Then Changing the above dataprovider's Query to some thing like below,
    <query>SELECT Employee.EmpID, Employee.EmpName FROM Employee where Upper(Employee.EmpName)='RAJ'</query>
    Please let me know the RESTful Call required to do this.
    Thanks in advance.
    Thanks,
    Mahendra.

    FYI, the output of this call returns something like:
    <?xml version="1.0" encoding="UTF-8"?> 
    <queryplan>
        <union>
            <fullOuterJoin>
                <statement index="1">SELECT 'FY' || to_char(SALES.invoice_date,'yy'), count( distinct SALES.inv_id) FROM SALES GROUP BY 'FY' || to_char(SALES.invoice_date,'yy')</statement>
                <statement index="2">SELECT 'FY' || to_char(SALES.invoice_date,'yy'), sum(INVOICE_LINE.nb_guests) FROM SALES, INVOICE_LINE, SERVICE_LINE, SERVICE WHERE ( SALES.INV_ID=INVOICE_LINE.INV_ID ) AND ( INVOICE_LINE.SERVICE_ID=SERVICE.SERVICE_ID ) AND ( SERVICE.SL_ID=SERVICE_LINE.SL_ID ) AND ( SERVICE_LINE.service_line = 'Accommodation' ) GROUP BY 'FY' || to_char(SALES.invoice_date,'yy')</statement>
            </fullOuterJoin>
            <fullOuterJoin>
                <statement index="3">SELECT 'FY' || to_char(SALES.invoice_date,'yy'), count( distinct SALES.inv_id) FROM SALES GROUP BY 'FY' || to_char(SALES.invoice_date,'yy')</statement>
                <statement index="4">SELECT 'FY' || to_char(SALES.invoice_date,'yy'), sum(INVOICE_LINE.days * INVOICE_LINE.nb_guests * SERVICE.price) FROM SALES, INVOICE_LINE, SERVICE WHERE ( SALES.INV_ID=INVOICE_LINE.INV_ID ) AND ( INVOICE_LINE.SERVICE_ID=SERVICE.SERVICE_ID ) GROUP BY 'FY' || to_char(SALES.invoice_date,'yy')</statement>
            </fullOuterJoin>
        </union>
    </queryplan>

  • HT4972 Hello, I just updated the iphone with 5.1 ios...but now i'm getting "No Service" message for AirTel...Please help

    Hello, I just updated the iphone with 5.1 ios...but now i'm getting "No Service" message for AirTel...Please help

    Hi, Yes, I bought new sim (64k) from Airtel gallery...and now i'm on version 5.1.1...Sometimes it works but if i turn on the 3g then the network went down and showing me a "NO SERVICE" error...i have to wait till the network come...it's a headche..I think i have to go again in gallery and want to ask for new sim with 128k...Any suggestions on this plz?

  • Hello, I just updated the iphone with 5.1 ios...but now i'm getting "No Service" message for AirTel...Please help

    Hello, I just updated the iphone with 5.1 ios...but now i'm getting "No Service" message for AirTel...Please help

    Where did you originally purchase your iPhone? How was your
    iPhone unlocked to work with AirTel? What does it say  when
    you look at Settings=>General=>Carrier?

  • Update the game with tuning!

    The people, who have finished the game are not frequently playing the game. To bring them back, update the game with tuning. You can make people to comeback to the game everyday looking for the perfect tune for their faviourate cars!. It would increase the players on multiplayer getting their cars sideways together and just having fun. It's gonna add the competion of the drifting love.

    It's not a simulation, therefore there won't be tuning.

  • REUSE_ALV_GRID_DISPLAY - updating the database with new values

    Hi,
    I am using the function module 'REUSE_ALV_GRID_DISPLAY' to display records. I have managed to open a field for input/edit mode. Once this data has been changed , where will this data be? I have checked the internal table - no joy. I need to use this new/changed data to update the database with the new values.
    Thanks,
    Leanne

    Hi,
    The data is stored in table tab. After the changes the data does not reflect in table tab. Where can I get the data so that I can update my database?
    I have coded the process as follows:
        when '&SUSPEND'.
          loop at tab
            where box = 'X'.
              update zzzz
                set status       = 'SP'
          endloop.
    Regards,
    Leanne

  • Rewrite the query with out joins and group by

    Hi,
    This was an interview question.
    Table Names: bookshelf_checkout
    bookshelf
    And the join condition between these two tables is title
    We need to rewrite below query without using join condition and group by clause ?
    SELECT b.title,max(bc.returned_date - bc.checkout_date) "Most Days Out"
               FROM bookshelf_checkout bc,bookshelf b
               WHERE bc.title(+)=b.title
               GROUP BY b.title;When I was in college, I read that most of the SELECT statements can be replaced by basic SQL operations (SET OPERATORS). Now I am trying to rewrite the query with SET operators but not able to get the exact result.
    Kindly help me on this.
    Thanks,
    Suri

    Something like this?
      1  WITH books AS (
      2  SELECT 'title 1' title FROM dual UNION ALL
      3  SELECT 'title 2' FROM dual UNION ALL
      4  SELECT 'title 3' FROM dual ),
      5  bookshelf AS (
      6  SELECT 'title 1' title, DATE '2012-05-01' checkout_date, DATE '2012-05-15' returned_date FROM dual UNION ALL
      7  SELECT 'title 1' title, DATE '2012-05-16' checkout_date, DATE '2012-05-20' returned_date FROM dual UNION ALL
      8  SELECT 'title 2' title, DATE '2012-04-01' checkout_date, DATE '2012-05-15' returned_date FROM dual )
      9  SELECT bs.title, MAX(bs.returned_date - bs.checkout_date) OVER (PARTITION BY title) FROM bookshelf bs
    10  UNION
    11  (SELECT b.title, NULL FROM books b
    12  MINUS
    13* SELECT bs.title, NULL FROM bookshelf bs)
    SQL> /
    TITLE   MAX(BS.RETURNED_DATE-BS.CHECKOUT_DATE)OVER(PARTITIONBYTITLE)
    title 1                                                           14
    title 2                                                           44
    title 3Lukasz

  • HT1386 Why when I select not to update the calendar with any entry older than 1 day it ignors that and goes back as far as my calendar has entries

    Why when I select not to update the calendar with any entry older than 1 day it ignors that and goes back as far as my calendar has entries

    FCPx answer: At least the answer that worked for me...
    I had the same problem, as I believe many others had.
    In my case the problem was with one of OSX Mavericks' new features: APP NAP.
    It looks like this is ON by DEFAULT for ALL Applications and that FCPx, or Compressor for that matter, will not work if this is enabled on them.
    I just disabled App Napp on FCPx and now it works, again, like a charm.
    This is how you disable App Nap: http://reviews.cnet.com/8301-13727_7-57612009-263/how-to-disable-app-nap-in-os-x -mavericks/
    I hope this helps,
    Mariano

Maybe you are looking for

  • Iphoto message: library created with unreleased version of iphoto

    Hi I'm brand new to the forum--know others have had a similar issue-- I get the message that says library was created with an unreleased version of iphoto, and I should quit and open with version 2 or 4. when this message appears the only choice is t

  • Exporting data to Excel 2007 from SAP report

    Dear all, Earlier we used to have Excel 2003 and while downloading data from MC.9 Report - Export - Transfer to XXL, we could able to download the data. Now we have installed Excel 2007 and trying to download the data from MC.9 report, its opening ex

  • How do I block a website so my kids cannot get on it? I am trying to block facebook.

    I just want to be able to block Facebook and certain sites so my child cannot have access to them.

  • Mapping in to SAP

    I have a scenario like this THis is for pharma company. product is having four operations. last one is compression like converting the powder in to tablets. so during this.. tablets weight is checked periodically for example for every 30 min for 10 t

  • Workflow from form

    I am trying to initiate a workflow from a form. I have seen several posts in the forums and confusing. I have a form called example user form. In that form I have placed a button. I want as soon as I press the button; test workflow should initiate. W