如何在SQL中的Select语句中修复子查询

时间:2019-09-04 12:45:47

标签: sql vb.net

Public ClientEmpIDx As String = ""

Public Sub PromotionLog_table()
    Dim cmd As New SqlCommand
    Dim da7 As New SqlDataAdapter
    Dim sSql7 As String = String.Empty
    Dim sSql8 As String = String.Empty
    cmd.Connection = Con_view
    cmd.CommandType = CommandType.Text
    sSql7 = "Select PromotionLog.EmpID as [Emp ID], Employee.ClientEmpID as [EMP No],"
    sSql7 = sSql7 & "RTRIM(Employee.Initials) + + LTRIM(Employee.LastName) As [Name], "

    sSql7 = " PromotionLog.OldGradeID, (Select RTRIM(Grade.GradeName) "
    sSql7 = sSql7 & " FROM PromotionLog join Grade on (PromotionLog.OldGradeID= Grade.GradeID) "
    sSql7 = sSql7 & " WHERE  Grade.GradeID = PromotionLog.OldGradeID) "

    sSql7 = sSql7 & "PromotionLog.NewGradeID as [New Grade ID],"
    sSql7 = sSql7 & " RTRIM(Designation.DesName) as [New Desig], "
    sSql7 = sSql7 & "PromotionLog.PromotedDate as [Promoted Date], "
    sSql7 = sSql7 & "PromotionLog.SystemPromotedDate as [System Promoted Date], "
    sSql7 = sSql7 & "RTRIM(PromotionLog.Reason) as [Remarks] "
    sSql7 = sSql7 & "FROM Employee join PromotionLog on (Employee.EmpID= PromotionLog.EmpID) "
    sSql7 = sSql7 & " join Grade on (PromotionLog.NewGradeID= Grade.GradeID) "
    sSql7 = sSql7 & " join Designation On (Grade.DesignationID= Designation.DesignationID) "
    sSql7 = sSql7 & " WHERE  Employee.ClientEmpID   Like '%" & ClientEmpIDx & "%'"
    cmd.CommandText = sSql7
    da7.SelectCommand = cmd
    da7.Fill(dt17)
End Sub

1 个答案:

答案 0 :(得分:0)

Select PromotionLog.EmpID as [Emp ID], 
Employee.ClientEmpID as [EMP No],
RTRIM(Employee.Initials) +  LTRIM(Employee.LastName) As [Name], 
PromotionLog.OldGradeID, 
RTRIM(G.GradeName)
,
 PromotionLog.NewGradeID as [New Grade ID],
         RTRIM(Designation.DesName) as [New Desig], 
        PromotionLog.PromotedDate as [Promoted Date], 
        PromotionLog.SystemPromotedDate as [System Promoted Date], 
        RTRIM(PromotionLog.Reason) as [Remarks] 
 FROM Employee j
Join PromotionLog on (Employee.EmpID= PromotionLog.EmpID) 

join Grade on (PromotionLog.NewGradeID= Grade.GradeID)
LEFT JOIN Grade G ON G.GradeID = PROMOTIONLOG.OLDGradeID 
join Designation On (Grade.DesignationID= Designation.DesignationID) 
WHERE  Employee.ClientEmpID   Like '%" & ClientEmpIDx & "%'