0

我们有一个将 3 个参数 fname、lname、hiredate 传递到另一个页面的表单,用于检查个人是否是公司的员工。

下面的代码基于这 3 个参数进行验证。

Dim sqlstr As String = "SELECT fname,lname,hiredate (SELECT COUNT(*) FROM Comp WHERE comp.emp_ID = e.emp_ID) AS [exists], b.* FROM dbo.EMP e LEFT OUTER JOIN comp b ON e.emp_id=b.emp_id WHERE e.fname Like '%" & Request.QueryString("fname") & "%' And e.lname Like '%" & Request.QueryString("lname") & "%' And e.hiredate = '" & Request.QueryString("hiredate") & "'"

如果检查确认此人确实是员工,则下一个代码会检查他/她是否有资格获得福利:

If myDS.Tables(0).Rows.Count > 0 Then

    Dim listOfBenefits = New List(Of String) ( { _
        "CT07B", "CT081", "CT083", "SG09A", "SG10", "SC11A", "SG23", "SG23A", "SG27" _
    })

    If Not listOfBenefits.Contains(txtBenefits.Text) Then
        blMsg.Text = "This employee is not eligible to receive any of the listed benefits yet."
    End If

End If

到目前为止,这非常有效。

现在,我们正在尝试添加另一项检查,以验证特定员工是否已注册任何福利。

如果员工未注册任何福利,则无需检查他或她的资格。

另一方面,如果员工已经注册了一些福利,那么我们会检查他/她是否有资格获得上面列出的任何福利。

到目前为止,我的支票不起作用,因为确定员工是否注册了任何福利的支票被忽略了。

我究竟做错了什么?

这是更新的代码,其中包括检查福利注册。

 Dim sqlstr As String = "SELECT fname,lname,hiredate, (SELECT COUNT(*) FROM Comp WHERE comp.emp_ID = e.emp_ID) AS [exists], b.* FROM dbo.EMP e LEFT OUTER JOIN comp b ON e.emp_id=b.emp_id WHERE e.fname Like '%" & Request.QueryString("fname") & "%' And e.lname Like '%" & Request.QueryString("lname") & "%' And e.hiredate = '" & Request.QueryString("hiredate") & "'"

If myDS.Tables(0).Rows.Count > 0 Then

    ' Ok, this individual is an employee, let's check to see if s/he has registered for any benefits.
    benefitSQL = "SELECT fname, lname, (SELECT COUNT(*) FROM benefits WHERE benefits.emp_ID = e.emp_ID) AS [exists], b.* FROM dbo.EMP e LEFT OUTER JOIN benefits b ON e.emp_id=b.emp_id "

    If CInt(exists.Value) > 0 Then
        ' Then employee has some benefits. Now, check whether one of those benefits is on the list below. '

        Dim listOfBenefits = New List(Of String) ( { _
            "CT07B", "CT081", "CT083", "SG09A", "SG10", "SC11A", "SG23", "SG23A", "SG27" _
        } )

        If Not listOfBenefits.Contains(txtBenefits.Text) Then
            lblMsg.Text = "This employee is not eligible to receive any of the listed benefits yet."
        End If
    Else

        lblMsg.Text = "This employee has not registered for any benefits yet."   
    End If
 End If

SQL,格式化:

SELECT
    fname,
    lname,
    hiredate,
    (
        SELECT
            COUNT(*)
        FROM
            Comp
        WHERE
            comp.emp_ID = e.emp_ID
    ) AS [exists],
    b.*
FROM
    dbo.EMP e
    LEFT OUTER JOIN comp b ON e.emp_id = b.emp_id
WHERE
    e.fname LIKE %@fname And e.lname LIKE %@lname%
    AND
    e.hiredate = @hiredate
4

1 回答 1

0

您正在选择数据,但您希望受数据约束:

SELECT
    e.fname,
    e.lname,
    e.hiredate
FROM
    dbo.EMP e
WHERE
    e.fname LIKE %@fname And e.lname LIKE %@lname%
    AND
    e.hiredate = @hiredate
    AND
    EXISTS (
        SELECT
            1
        FROM
            Comp
        WHERE
            comp.emp_ID = e.emp_ID
    )

您还可以内部连接表:

SELECT
    e.fname,
    e.lname,
    e.hiredate,
    c.*
FROM
    dbo.EMP e
    INNER JOIN Comp c ON e.emp_ID = c.emp_ID
WHERE
    e.fname LIKE %@fname And e.lname LIKE %@lname%
    AND
    e.hiredate = @hiredate
于 2012-09-28T21:25:15.960 回答