sqlce IF EXISTS.... workaround

indyrob

Member
Joined
Sep 16, 2009
Messages
7
Programming Experience
Beginner
I'm new to sqlce and have come to realize that it doesn't support the IF EXISTS statements...

Is there a better workaround for this?

I originally wanted this query to run:


VB.NET:
IF EXISTS (SELECT * FROM PARTS WHERE PARTNO = @PARTNO)
UPDATE PARTS SET QTYOH = QTYOH + @QTYOH WHERE PARTNO = @PARTNO
ELSE 
INSERT INTO PARTS (PARTNO, QTYOH)
VALUES (@PARTNO, @QTYOH)

I got it to work with sqlce as follows:

VB.NET:
'SETUP THE CONNECTION
        Dim CONSTRING As String = "DATA SOURCE = C:\TECHTOOLS.SDF"
        Dim CON As New SqlCeConnection(CONSTRING)

        'ASIGN THE EXECUTENONQUERY RESULT TO A VARIABLE
        Dim RESULT As Integer

        'DEFINE SQL STATEMENTS
        Dim SQLUPDATE As String = "UPDATE PARTS SET QTYOH = QTYOH + @QTYOH WHERE PARTNO = @PARTNO"
        Dim SQLINSERT As String = "INSERT INTO PARTS (PARTNO, QTYOH)" & _
                                    "VALUES (@PARTNO, @QTYOH)"




        Dim CMD As New SqlCeCommand(SQLUPDATE, CON)
        Dim CMDNEXT As New SqlCeCommand(SQLINSERT, CON)



        CMD.Parameters.Add("@PARTNO", TextBox1.Text)
        CMD.Parameters.Add("@QTYOH", TextBox2.Text)

        CMDNEXT.Parameters.Add("@PARTNO", TextBox1.Text)
        CMDNEXT.Parameters.Add("@QTYOH", TextBox2.Text)



        CON.Open()
        RESULT = CMD.ExecuteNonQuery()

        If RESULT = 0 Then

            CMDNEXT.ExecuteNonQuery()

        End If

        CON.Close()

Question: Are there any other simpler tricks to substitute IF EXISTS in sqlce? Thanks!

Rob
 
Back
Top