Results 1 to 2 of 2

Thread: Get new record ID when inserting data

  1. #1

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Get new record ID when inserting data

    I am writing automated test cases. In this specific case, I need to enter temporary data into a table, run the test code, and then cleanup by deleting the temporary data I inserted.

    What I do currently is storing the INSERT and DELETE statements in a XML file, and execute the INSERT and clean up using the DELETE statement. I feel this is lame.

    For instance I got a ‘PerformanceData’ table, with columns ‘ID’, ‘DateCollected’, CounterID, ‘InstanceID’ and ‘Value’
    The INSERT statement enter data into the last 4 Columns, as ‘ID’ is an id field, and it’s value is auto generated by sql server.
    So, I want to execute the INSERT statement (from my C# code) in a way that I get this auto generated ‘ID’ value and store them in a hashtable (key=ID, Value=TableName). Then afterwards, I just loop through the hashtable, and build a simple DELETE statement using the TableName and ID value is store in the collection.

    1) Can one do this?
    2) Any advice on streamlining this process further?
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  2. #2
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476

    Re: Get new record ID when inserting data

    Hi,

    i have only done that in my SqlServer Stored Procedures so far and returned the autogenerated Id.

    Like that:
    Code:
    INSERT INTO myTable(ColumnTwo,ColumnThree,ColumnFour) VALUES("Two","Three","Four")
    SET @myVariable = (SELECT @@IDENTITY AS 'ColumnId')
    Thats one option. Maybe it works if you just execute the SELECT @@IDENTITY Statement right after the Update Satement in your code.
    Worth a try!

    Stephan
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width