Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: , +
Hi all :)

Before i'm starting futher more about my problem. lets me tell a short description about my program.

1. user will choose path ID (retrieve all this data from table TPATH of database SDO) that they want to delete.

2. before the path ID would be delete, it will compare this path ID from table TTENS of database SDO and table TPATH of database SDO where this two table share the same column namely as M_C and also share the same database.

3. this comparing method was write in Stored Procedure(SP).

this SP will be called on this class name as Path.vb :

VB
Public Function getComparedPathID(ByVal DeletePathID As String) As DataSet
                Dim DHHConn As New DRConnection("", "DHH_CONN")
                Dim ds As DataSet
                Dim lsPath As String = ""
                Dim row As DataRow
                Dim intLoop As Integer = 1
        
                Try
                    ds = DHHConn.RunSPReturnDS("sp_Path_ID", New SqlParameter("@M_C", DeletePathID))
                    If ds IsNot Nothing Then 
                        If ds.Tables.Count > 0 Then
                            If ds.Tables(0).Rows.Count > 0 Then 
                                For Each row In ds.Tables(0).Rows
                                    If intLoop = 1 Then
                                        lsPath = lsPath + row(0).ToString()
                                    Else
                                        lsPath = lsPath + "," + row(0).ToString()
                                    End If
                                    intLoop = intLoop + 1
                                Next
                            End If
                        End If
                    End If
                    Return ds
                Catch ex As Exception
                    LogManager.WriteLog(ex.Message)
                    Throw New Exception(ex.Message)
                Finally
                    DHHConn.CloseConnection()
                End Try
            End Function


And, on my path.aspx.vb page, this control button delete function :

VB
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
                Dim i As Integer
                Dim result As DataSet
                Dim hypID As HyperLink
                Dim chkRemove As New CheckBox
                Dim clsPath As New Path()
                Try
                    For i = 0 To grdPath.Rows.Count - 1
                        chkRemove = DirectCast(grdPath.Rows(i).FindControl("chkRemove"), CheckBox)
                        hypID = DirectCast(grdPath.Rows(i).FindControl("hypID"), HyperLink)
                        If chkRemove.Checked Then
                            result = clsPath.getComparedPathID(chkRemove.Text)
                        End If
                    Next i
                    lblErrorMessage.Text = "Paths delete successfully."
                    BindGrid("%", "%", drpArea.SelectedValue, drpType.SelectedValue)
                Catch ex As Exception
                    lblErrorMessage.Text = ex.Message
                End Try
            End Sub


And now, my Stored Procedure code that i used :

VB
CREATE PROCEDURE [dbo].[sp_Path_ID] @M_C varchar(20)
        AS
        BEGIN
            SELECT M_C FROM [dbo].[TPATH]
            WHERE M_C  NOT IN (SELECT M_RTE_C FROM [dbo].[TTENS])
            and M_C = @M_C
        END


My problem now :


1. i'm not be able to compare this data.
2. Are my SP is correct? If not, you have any suggestion how to create it?



Thank you for your kind to help me !
Posted

1 solution

Hi,

The best way to compare 2 tables with same columns is using "except" keyword.

Please try below query:

SQL
SELECT M_C FROM [dbo].[TPATH]
except
SELECT M_RTE_C FROM [dbo].[TTENS]


This query will return data which are there in [TPATH] table but not in [TTENS] table.

Please let me know if you have any concern or query on this.

Thanks
Advay Pandya
 
Share this answer
 
Comments
Maciej Los 28-May-15 6:52am    
5ed!
Advay Pandya 28-May-15 6:56am    
Thanks :)
apple blue 29-May-15 2:05am    
Hi Advay Pandya :)

Thanks! Your solution works :)

Both this query can be used on my program. I've alter my query and also try your given query. Both works :)

But for my query, i've alter to do this query :

SELECT M_C FROM [dbo].[TPATH]
WHERE M_C IN (SELECT M_C FROM [dbo].[TTENS])
and M_C = @M_C
apple blue 29-May-15 2:05am    
Ouh yaa ! Thanks a lotttttt for helping me =)
Advay Pandya 29-May-15 2:27am    
Welcome. Your query is correct, but it may provide wrong result in the case of NULL value. Please check NULL values using your query. except keyword will also handle NULL value. Thanks..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900