Get the last inserted into the table, SQL Server Uniqueidentifier

I'm looking for the code that will make me the last ID entered in a table.  I am using SQL Server and have the ID defined as uniqueidentifiers.  I tried SCOPE_IDENTITY() and had a problem.  I also want to make sure that it is for this unique insert, not an insert that has crept into almost at the same time.

Thank you!

NCC

I'm looking for the code that will make me the last ID entered in a table.  I am using SQL Server and have the ID defined as uniqueidentifiers.  I tried SCOPE_IDENTITY() and had a problem.  I also want to make sure that it is for this unique insert, not an insert that has crept into almost at the same time.

SCOPE_IDENTITY() only works with identity columns.  If your table does not identity column, it will be of no use to you. Neither will 'result' of cfquery attribute such that it only works with ms sql identity values too.

As far as I KNOW, there is no method integrated to automatically return a newly created uniqueidentifier.  The closest you can get in MS SQL 2005, without involving triggers, use an OUTPUT clause.

SET NOCOUNT ON
INSERT INTO yourTable (ColumnA, ColumnB)
OUTPUT inserted.yourIDColumn
VALUES ('foo', 'bar')

SET NOCOUNT OFF

Post edited by: == cfSearching == -.

Tags: ColdFusion

Similar Questions

  • Insert into a table using the check boxes

    Hello

    I want to insert into a table emp when I press a button "Insert" area of employee report. the SQL query for the report is

    SELECT HTMLDB_ITEM. CheckBox(1,rowNum) "",
    x.EmpNo,
    x.Ename
    de)
    Select empno htmldb_item.hidden(2,empno),
    htmldb_item. Text(3,Ename) ename from emp) x
    order by 2

    I created the following process:

    declare
    v_test varchar (10);
    Start
    BECAUSE me in 1.HTMLDB_APPLICATION. G_F01. COUNTY
    LOOP
    v_test: = HTMLDB_APPLICATION. G_F02 (HTMLDB_APPLICATION. G_F01 (i));
    dbms_output.put_line (v_test);
    insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno)
    values(v_test,'A','B',10,01-May-81,200,1,10)
    WHERE empno = HTMLDB_APPLICATION. G_F02 (HTMLDB_APPLICATION. G_F01 (i));
    END LOOP;
    end;

    But IT is not working properly, infact, it gives me errors like:

    ORA-06550: line 9, column 47:
    PL/SQL: ORA-00933: SQL not correctly completed command
    ORA-06550: line 8, column 1:
    PL/SQL: SQL statement ignored

    7 dbms_output.put_line (v_test);
    8 insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
    9 values(v_test,'A','B',10,01-MAY-81,200,1,10)
    10 WHERE empno = HTMLDB_APPLICATION. G_F02 (HTMLDB_APPLICATION. G_F01 (i));
    11. END OF LOOP;

    Any help?
    Additional info: table Emp is the default table in the database

    This is an example showiing how such a process must be put in place:

    http://Apex.Oracle.com/pls/OTN/f?p=31517:95

    Denes Kubicek
    -------------------------------------------------------------------
    http://deneskubicek.blogspot.com/
    http://www.Opal-consulting.de/training
    http://Apex.Oracle.com/pls/OTN/f?p=31517:1
    -------------------------------------------------------------------

  • How do I get the table cells?

    It's my get code that any object in propagation.

    Int32 childCount = parent-> GetChildCount();

    for (int32 /IndexEnfant = 0; childIndex < childCount; /IndexEnfant ++)

    {

    InterfacePtr < IHierarchy > child (parent-> QueryChild (childIndex));

    ChildUIDRef = UIDRef: GetUIDRef (child);

    TextFrame table

    }

    How do I get the table cells?

    You can access the table using ITableModel and GridAddresscell.

    But to get to the ITableModel since ITextModel implies some complexity.

    Few things to understand:

    -L'interface that corresponds to the written document is ITextModel

    -The recommended method to iterate tables in a story done by ITextStoryThreadDictHier

    -Get the ITextStoryThreadDictHier on the history and the main thread as ITextStoryThreadDictHier::NextUID()

    -Now get ITextStoryThreadDict for UID and query for ITableModel. If you get ITableModel, then you have found the table.

    Once you have ITableModel you can get ITableCell as follows:

    ITableModel:QueryCellContentBoss(GridAddress); Give ITableCell

    You can refer to SnipperRunner (SnpManipulateTextModel).

    Download text template table model:

    InterfacePtr textModel (storyUIDRef, UseDefaultIID());

    InterfacePtr textStoryDictHier (textModel, UseDefaultIID());

    NextUID UID = sourceStory.GetUID (); / / Download the main thread dictionary

    Now loop the dicthier to iterate through all the threads dicts.

    InterfacePtr textStoryThreadDict (sourceStory.GetDataBase (), nextUID, UseDefaultIID());

    InterfacePtr tableModel (textStoryThreadDict, UseDefaultIID());

    If (tableModel) / / the storythreaddict represents a table

    {

    You are now in a table

    }

  • Create a button for inserting into a table

    What I can't do, because I've never needed, is to create, in a page (where I have an interactive report), a button is clicked that once performs a simple sql (for example, an insert into a table using the values of page element).

    How can I do?

    Thank you

    Here are some of the steps:

    (1) create the button.

    (2) then right click on the name of the button and select Create Dynamic Action

    (3) give it to DA a name, click Next

    (4) on the 'when' step, event should already be "Click" and the name of the button must be filled

    (5) for the Condition, you would have a condition when you want that the display of the button.  Say, if your page elements have a certain range of values, and then display this button.  No condition means that the button is always displayed.

    (6) for the 'real Action' stage, action, select "run the Code in PL/SQL.

    [There are other ways to do this, but it's simple for me.]

    (7) in the Code PL/SQL block, enter says:

    Begin

    INSERT INTO EMP (EMPNO, ENAME, HIREDATE)

    VALUES (: P6_EMPNO, 'MARK1970',: P6_HIREDATE);

    COMMIT;

    END;   -You'd probably all the variables page element

    (8) here the part I'm not sure 100%.

    For items to submit "Page, name used in YOUR QUERY - in my example page elements, these are P6_EMPNO, P6_HIREDATE (don't note any use of & or: or here.)

    Hmm.  Start by putting nothing in "Page of elements to return.   If that fails, then try the same elements of page P6_EMPNO, it P6_HIREDATE as well.

    [I'm too lazy to go running at present].

    (9) I think that's it.

    Howard

  • Insert into a table... with order by

    Oracle 10.2.0.3

    I want to insert into a table, records from another table ordered in a certain way. We want a specific order that we want the data grouped the disk.

    No idea why it does not work?
    create table test_a (
         id number primary key,
         value1 varchar2(20));
    
    insert into test_a (id, value1) values (1,'ccc');
    insert into test_a (id, value1) values (2,'bbb');
    insert into test_a (id, value1) values (3,'aaa');
         
    create table test_b (
         id number primary key,
         value1 varchar2(20));
    
    insert into test_b (id, value1) (select id, value1 from test_a order by value1)
                                                                   *
    ERROR at line 1:
    ORA-00907: missing right parenthesis     
    If I leave aside the order, it works fine...
    SQL> insert into test_b (id, value1) (select id, value1 from test_a);
    
    3 rows created.
    This does not work either:
    create table test_b as (select id, value1 from test_a order by value1)
                                                          *
    ERROR at line 1:
    ORA-00907: missing right parenthesis

    Lose the parenthesis.

    TUBBY_TUBBZ?create table test_b as select id, value1 from test_a order by value1;
    
    Table created.
    
    Elapsed: 00:00:00.02
    

    Or

    TUBBY_TUBBZ?
    insert into test_b select id, value1 from test_a order by value1;
    
    3 rows created.
    
    Elapsed: 00:00:00.01
    TUBBY_TUBBZ?
    
  • when_button_pressed, inserting into a table

    I created a button 'OK' and in the when_button_pressed, I put the code to insert into a table (X for example), the values of entries in the form (ie., main block B_MAIN). The B_MAIN block is built manually and refers to the X table above.

    The OK button is in a different block called B_ACTION.

    When I run the form and click on 'OK', nothing happens. Is no error message or another. This could be it prevents insertion of data in the table?

    Thank you
    Chiru

    Published by: Megastar_Chiru on February 17, 2009 16:23

    The B_MAIN is the database block? Check the BLOCK of DATA of DATABASE property, it must be set to NO because you entered the data manually.

    Another thing, when using the pl/sql commit it is not necessary to follow by commit_form, since he initiated commit_form built-in function.

  • Installation of the Microsoft SQL Server 2005 Express Edition (KB955706) code error 0x2B2F

    I am trying to install an update on a desktop HP for Microsoft SQL Server 2005 Express Edition (KB955706).  It has downloaded everything and still not settle.  It gives the error code is 0x2B2F.  I searched this error on the microsoft website and nothing came.  Any help would be appreciated.

    Hello nesbitzero,

    Thank you for using Microsoft Windows forum.

    You receive the 0x2b2f error code when you use the web sites, Windows Update or Microsoft Update to install SQL Server 2005 Service Pack 3 on a computer with MSXML6 Service Pack 2 is already installed.
     
    Follow this link for the reasons: http://support.microsoft.com/kb/970674

    You must first try and reset Windows Update components to eliminate the
    KB955706 update for SQL server.

    Follow the link for what has a fixit: http://support.microsoft.com/kb/971058

    After reset, try to install it once more to check if the problem persists.

    If this is the case, install the update mode safe

    Follow the steps below to start in safe mode with networking:

    1. reboot your computer and start pressing F8 on your keyboard. On a computer that is configured to start to multiple operating systems, you can press the F8 key when the Boot Menu appears.
    2. Select networking in Mode safe mode when the Windows Advanced Options menu is displayed and press ENTER

    In Safe Mode, download and install the stand-alone package for the Microsoft SQL Server 2005 Service Pack 3 on the site: https://www.microsoft.com/downloads/details.aspx?FamilyID=ae7387c3-348c-4faa-8ae5-949fdfbe59c4&displaylang=e

    Note: It is recommended that you temporarily disable the antivirus before installing the update.
     
    Follow the steps in the link below for "SQL Server 2005 Setup fails when MSXML Core Services 6.0 Service Pack 2 has been installed"
    http://support.Microsoft.com/kb/968749

    See: http://social.answers.microsoft.com/Forums/en-US/vistawu/thread/e78198b7-324f-4775-85bd-83ce5af4affc for the exact number.

    For more information, see: http://social.msdn.microsoft.com/Forums/en-US/category/sqlserver/

    Kind regards
    Aziz Nadeem - Microsoft Support

  • How to perform an addition of column values in an insert query that would insert in the 3rd column, and the values how to insert into another table.

    I have two tables (2) RESULT TAB (1)

    CREATE TABLE TAB

    (

    NUMBER OF SNO

    A NUMBER,

    B THE NUMBER.

    NUMBER OF THE SUM

    );

    CREATE AN ARRAY OF RESULT

    (

    NUMBER OF SNO

    NUMBER OF THE SUM

    )

    my doubt is:

    (1) I want to insert a table TAB, my question is how to insert a column to the SUM using the column A AND B... Here im adding two values of the column and store result in the AMOUNT column.

    SNO   A  SUM           

    1 100 150 250

    2 300 100 400

    I want to like this, it is possible with single insert query?


    (2) at the time of the insertion TAB of values that SNO, and the values of table TAB $ insert in the table of RESULTS... is it possible these two inserts at the same time?

    in fact, im using another this table.fro TAB and easy to understand I write like that, please solve this problem

    First, you post in the wrong forum as this one is only for Oracle's SQL developer tool. So you might ask your question in the general forum of SQL.

    Second, you might solve your problems with bind variable:

    Insert tab

    (sno, a, b, sum)

    values

    (: SNO,: A: B: A + B :))

    You should not use sum as column name because it is a reserved word.

    More you cannot insert into two different tables with a single SQL, but you can use PL/SQL to do this:

    Start

    insert into tab values (: SNO,: A: B: A + B :);)

    insert into result values (: SNO,: A + B :);)

    end;

    If you meet sno from a sequence, you could do something like this:

    Start

    insert into values tab (seq_sno.nextval,:,: B,: A +: B) return sno in: SNO.

    insert into result values (: SNO,: A + B :);)

    end;

    Hope that helps,

    dhalek

  • How to identify the name of the procedure that inserts into a table in the TRIGGER

    Hello
    There are several procedures in my DB that perform the INSERT operation on a main table - MYTAB say.

    I created a trigger on the table MYTAB, to follow closely, what procedure is insert data in there. Is there a function that captures the name of this object?

    For example: PROC_A, PROC_B, PROC_C insert into the table MYTAB. And my query is - the trigger on table MYTAB should document these object names where the lines fit.
    Please let me know your suggestions...

    SQL> create table t (no integer, dt timestamp, who_inserted varchar2(4000));
    
    Table created.
    
    SQL> create or replace procedure p
      2  as
      3  begin
      4    insert into t (no, dt) values (1, systimestamp);
      5  end;
      6  /
    
    Procedure created.
    
    SQL> show err
    No errors.
    
    SQL> create or replace trigger t_trig before insert on t for each row
      2  begin
      3     :new.who_inserted := dbms_utility.format_call_stack;
      4  end;
      5  /
    
    Trigger created.
    
    SQL> show err
    No errors.
    
    SQL> begin
      2    p;
      3  end;
      4  /
    
    PL/SQL procedure successfully completed.
    
    SQL> select * from t;
    
            NO DT                             WHO_INSERTED
    ---------- ------------------------------ ----------------------------------------
             1 31-OCT-14 04.58.53.668465 AM   ----- PL/SQL Call Stack -----
                                                object      line  object
                                                handle    number  name
                                              3a7ddbea0         2  KARTHICK.T_TRIG
                                              3ac979f30         4  procedure KARTHICK.P
                                              3a822deb8         2  anonymous block
    
  • Use the trigger and seq to insert into 2 tables

    I created a sequence.

    I have 2 tables

    I want a trigger that when I insert in the 1 table inserts the sequence number nextval, that is no problem, but also is there a way so that once that happens it also inserts this same value of sequence in another table.

    So the end result is a new record in the table has the seq value and a new record in table b with the same value of seq (and the rest of the fields blank)

    Thanks for any help

    Could do:

    SQL > create the sequence myseq;

    Order of creation.

    SQL > drop table t2 is serving;

    Deleted table.

    SQL > drop table t1 is serving;

    Deleted table.

    SQL > create table t1 (collar number);

    Table created.

    SQL > create table t2 (collar number);

    Table created.

    SQL > create or replace trigger mytrig

    2 before the Insert on t1

    3 for each line

    4 start

    case 5: new.col is null

    6. can

    7: new.col: = myseq.nextval;

    8 insert into t2 (col)

    9. Select myseq.currval

    10 double;

    11 end if;

    12 end;

    13.

    Trigger created.

    SQL > insert into values (null), t1 (col);

    1 line of creation.

    SQL > select * from t1;

    COL

    ----------

    1

    1 selected line.

    SQL > select * from t2;

    COL

    ----------

    1

    1 selected line.

  • Question: insertion of several lines in the MS Sql Server table using the DB adapter

    Hi all

    I managed to insert a single row in a table of MS SQL Server via the adapter DB to my process BPEL, but when I tried to insert in mutiple lines in the same table of MS SQL server, I encounter the error below.

    I use a DB SQL XA connection to connect to the server.

    Kindly help me to solve the problem. Thanks in advance.

    Error:

    " < bpelFault > < faultType > 0 < / faultType > < remoteFault xmlns =" http://schemas.Oracle.com/BPEL/extension "> < a name ="summary"part > < summary > exemption is is produced when the binding was used." Exception occurred during invocation of the JCA binding: "JCA binding run 'merge' reference operations have to: DBWriteInteractionSpec Execute Failed Exception." the merger failed. The descriptor name: [LoadCmpAggSpendStage.SapTable]. Caused by com.microsoft.sqlserver.jdbc.SQLServerException: incorrect syntax near ')'... Check the logs for the record output full DBAdapter before this exception. This exception is considered as reproducible, probably due to a communication failure. To be classified as not reproducible rather add property nonRetriableErrorCodes with the '102' value in the deployment descriptor (i.e. weblogic - RA.Xml). Auto retry a reproducible fault set composite.xml for this invoke these properties: jca.retry.interval, jca.retry.count and jca.retry.backoff. All properties are integers. ". The called JCA adapter threw an exception of resource. Please examine the error message above carefully to determine a resolution. < /Summary. (> < / piece > < part name = "detail" > < detail syntax > incorrect near ')'. < / details > < / piece > < part name = "code" > < code > 102 < / code > < / piece > < / remoteFault > < / bpelFault >

    Kind regards

    Balaji Rahmani

    It seems that in this case is called merge operation. If existing records (check primary key) are not there then it will be inserted on the other update. Check the syntax of the query that is created. It looks like she may have a supplement "). If you want to only insert then call insert operation and not merge.

  • Validate values inserted into a table against an another record in the table.

    Hi, I have this two tables on my Oracle Database 10g:
            Status                                    
    
    Id_Status | Description        
    --------------------------------       
      102     |   Ok                 
      123     |   Damaged 
      345     |   Broken
    
    Cars
    
    Car_ID|  wheel  |  hood  |  bumper  |  radiator
    -------------------------------------------------
      1   |   1     |     2  |     2    |   3
    and what I have to do is to make sure that you can only insert a record if the [wheel, hood, bumper, radiator etc...] values are values that already exist in the State of the table, but I do not know if you add FK as:
    ALTER TABLE "RCVRY"."COMPANY" 
        ADD (CONSTRAINT "CITY_ID" FOREIGN KEY("STATE") 
        REFERENCES "RCVRY"."CITIES"("STATE")
    is the right way to do it.

    If anyone can help by providing code samples or pointing to the right place in the documentation to look out it will be greatly appreciated.
    Concerning

    JMHP

    Hello

    José M. Hurtado says:
    Hi, I have this two tables on my Oracle Database 10g:

    Status                                    
    
    Id_Status | Description
    --------------------------------
    102     |   Ok
    123     |   Damaged
    345     |   Broken
    
    Cars
    
    Car_ID|  wheel  |  hood  |  bumper  |  radiator
    -------------------------------------------------
    1   |   1     |     2  |     2    |   3
    

    and what I have to do is to make sure that you can only insert a record if the [wheel, hood, bumper, radiator etc...] values are values that already exist in the State of the table, but I do not know if you add FK as:

    ALTER TABLE "RCVRY"."COMPANY"
    ADD (CONSTRAINT "CITY_ID" FOREIGN KEY("STATE")
    REFERENCES "RCVRY"."CITIES"("STATE")
    

    is the right way to do it.

    Yes, foreign keys are the right way to do it.
    Make sure that first of all, there is a constraint of PRIMARY KEY (or UNIQUE) on status.id_status.

    If anyone can help by providing code samples or pointing to the right place in the documentation to look out it will be greatly appreciated.

    The code you have posted has an imbalance ' (' between ADD and CONSTRAINT.) Lose that ' (' and you have the right syntax.)
    Double quotes are necessary only if you use non-standard names. Do not.
    The schema name (RCVRY in the above example) is optional.

    So, you could say:

    ALTER TABLE     cars
    ADD CONSTRAINT     cars_wheel_fk
    FOREIGN KEY     (wheel)
    REFERENCES     status (id_status)
    ;
    

    I hope that answers your question.
    If this is not the case, after a test script.
    Publish instructions CREATE TABLE for tables and ALTER TABLE instructions for constraints (if they are not part of the CREATE TABLE statements).
    Let's just do the constraint on wheel first. Once you know how to do this, add similar constraints for the other columns (radiator, hood, bumper,) will be easy.
    After a series of INSERT statements (and update, if you want) for the two tables, which must be executed in order.
    Say if you want as each INSERT (or UPDATE) statement of work or not. (Include a few of them).

    Published by: Frank Kulash, December 7, 2010 19:15

  • Select the statement to insert into a table by using a loop

    Hello
    create table uploadtab(
    itema varchar2(3),
    xtype varchar2(1),
    salesa number,
    margina number,
    salesb number,
    marginb number,
    salesc number,
    marginc number);
    
    insert into uploadtab
      (itema, xtype, salesa, margina, salesb, marginb, salesc, marginc)
    values
      ('abc', 'a', 100, .40, 300, .10, 450, .25);
    
    create table testinsert(itema varchar2(3),
    xtype varchar2(1),
    sales number,
    margin number);
    what I want to do is create 3 records based on this one in a loop
    so here's my desired for testinsert output
    abc  a 100  .40
    abc  a 300  .10
    abc  a 450  .25
    I don't want to use 3 insert tables if possible

    any help would be greatly appreciated
    Thanks in advance

    Published by: DM on July 7, 2010 14:22

    This gives a shot:

    INSERT INTO testinsert
    ( itema
    , xtype
    , sales
    , margin
    )
    SELECT  itema
    ,       xtype
    ,       DECODE
            ( RN
            , 1,salesa
            , 2,salesb
            , 3,salesc
            )
    ,       DECODE
            ( RN
            , 1,margina
            , 2,marginb
            , 3,marginc
            )
    FROM            uploadtab
    CROSS JOIN      (
                            SELECT ROWNUM RN
                            FROM   dual
                            CONNECT BY LEVEL <= 3
                    )
    ;
    
  • How do I get the table/matrix of an image?

    Hi, at work we will develop a program that works with images. My question is how can I get the matrix or matrix of pixels in an image?

    Thank you.

    If you have IMAQ there is a picture of table VI

    If you do not have the IMAQ stuff, there is a picture in table VI in the Vision Toolkit free AD: http://vi-lib.com/

    [edit: Oops, missed the last line of your post there are EmcV]

  • Get the table to an XML file with several bays

    Hello

    So I was faced with this program I have for the past few months but I'm almost there. I have a program that creates several berries and place one after the other in an xml file, and each table has its unique name. Now, I would create a VI that takes this XML file and when the user enters the name of the table specific they are seeking it goes into the xml file concludes the table in its entirety under this name and displays in an indicator of output to be read on the VI. A sample of my xml file and the VI that creates this xml file is attached.

    Thank you

    dlovell

    Something like this should work for you.

Maybe you are looking for