CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2000
    Posts
    58

    SQL SERVER - easy

    Hi,
    This is probably an easy question to answer.

    How would I code a procedure in SQL Server to do the followibg. Accept a variable(var1) and a recordset(rst) sent from a VB6 app, and then:

    1)delete * from table1 where field1 = var1
    then
    2)add rst to table1

    The important thing is that if two people run the procedure at the same time, l have to make sure that it runs the delete and add straight after each other and not run the delete for person 1 and then the delete for person 2 and then the add for person 1 and the add for person 2 (otherwise l will get duplicates). I guess this will require record locking.

    Also how would l call this procedure from vb6.

    THANKS HEAPS.
    UNI.


  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: SQL SERVER - easy

    1.set the connection object (cn)

    sSQL = "delete from Table where Field = " & var ' if var is a number
    sSQL = "delete from Table where Field = '" & var & "'" ' if var is a string

    cn.Execute sSQL

    2.Add recordset to table

    rs - is the recordset which you want tot enter to the table
    prepare recordset for the table where you will enter records (RS1)
    sSQL = "select * from Table"
    RS1.Open sSQL,cn,adopendynamic,adLockOptimistic
    With rs
    .MoveFirs
    Do while not .EOF
    .AddNew
    RS1!Field1 = !FieldA
    RS1!Field2 = !FieldB
    'FIeld1,2 from RS1, FieldA,B from rs
    .Update
    .MoveNext
    Loop
    End With








    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: SQL SERVER - easy

    This is a job for transactions.

    You need to add a "BEGIN TRANSACTION " and "COMMIT TRANSACTION " pair aorund the code that does all the deleteing and adding of records.
    This done, SQL server will take care of th elocking for you.

    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

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

Featured