反转join命令

时间:2016-03-29 06:40:54

标签: asp.net sql-server vb.net webforms

我正在接管一个用asp.net VB编写的项目,并使用mssql。

我有一个文本框,其中包含设施(FacName)的文本值

我想要做的是当用户点击提交时,程序将获取在文本框中输入的文本,查找设施表,查找设施名称,将设施ID与其匹配,并更新剧集设施ID栏。

以下是我的两张表:

Episode                   Facilities

ID__________FacilityID   |   ID__________FacName
1___________10           |   10__________Beenleigh
2___________14           |   14__________Sunnybank
3___________15           |   15__________Brisbane

用户更新剧集指向不同的设施 - 让我们说他们希望Episode.ID'2'引用新的设施,在这种情况下,“布里斯班”

他们在文本框中输入布里斯班并点击提交,并且剧集表中的结果变为:

Episode                   Facilities

ID__________FacilityID   |   ID__________FacName
1___________10           |   10__________Beenleigh
2___________15           |   14__________Sunnybank
3___________15           |   15__________Brisbane

所以它在设施表中进行查找并返回Facilities.id并在episode.FacilityID列中使用该值

我已经让我的选择声明运作良好,希望能帮助任何想要回答我问题的人!

工作选择语句

 SELECT Episode.ID, Episode.NOCSID, Episode.Disease, Episode.PHUID,     
        Episode.Status, Episode.Date, Episode.PersonID, Episode.ChangeType, 
        Episode.ChangeBy, Episode.ChangeDate, Episode.DoctorID, 
        Episode.FacilityID, Episode.OnsetDate, Episode.CloseDate, 
        Episode.Duplicate, Episode.OutbreakID, Episode.ClinicalLead, 
        Episode.NextActionDate, Facilities.FacName, Facilities.ID AS Expr1 
        FROM Episode 
        LEFT OUTER JOIN Facilities 
        ON Episode.FacilityID = Facilities.ID WHERE (Episode.ID = @ID)

无效UPDATE 声明:

UPDATE Episode
SET NOCSID = @NOCSID,
    Disease = @Disease,
    PHUID = @PHUID,
    Status = @Status,
    PersonID = @PersonID,
    ChangeType = 'Updated',
    ChangeBy = @ChangeBy,
    ChangeDate = GETDATE(),
    DoctorID = @DoctorID,
    OnsetDate = @OnsetDate,
    CloseDate = @CloseDate,
    Duplicate = @Duplicate,
    OutbreakID = @OutbreakID,
    ClinicalLead = @ClinicalLead,
    NextActionDate = @NextActionDate,
    FacilityID = @FacilityID
FROM Episode
INNER JOIN Facilities
  ON Episode.FacilityID = Facilities.FacName
WHERE (Episode.ID = @original_ID)
AND (Episode.NOCSID = @original_NOCSID
OR Episode.NOCSID IS NULL
AND @original_NOCSID IS NULL)
AND (Episode.Disease = @original_Disease)
AND (Episode.PHUID = @original_PHUID)
AND (Episode.Status = @original_Status)
AND (Episode.PersonID = @original_PersonID
OR Episode.PersonID IS NULL
AND @original_PersonID IS NULL)
AND (Episode.DoctorID = @original_DoctorID
OR Episode.DoctorID IS NULL
AND @original_DoctorID IS NULL)
AND (Episode.FacilityID = @original_FacilityID
OR Episode.FacilityID IS NULL
AND @original_FacilityID IS NULL)
AND (Episode.OnsetDate = @original_OnsetDate
OR Episode.OnsetDate IS NULL
AND @original_OnsetDate IS NULL)
AND (Episode.CloseDate = @original_CloseDate
OR Episode.CloseDate IS NULL
AND @original_CloseDate IS NULL)
AND (Episode.Duplicate = @original_Duplicate
OR Episode.Duplicate IS NULL
AND @original_Duplicate IS NULL)
AND (Episode.OutbreakID = @original_OutbreakID
OR Episode.OutbreakID IS NULL
AND @original_OutbreakID IS NULL)
AND (Episode.ClinicalLead = @original_ClinicalLead
OR Episode.ClinicalLead IS NULL
AND @original_ClinicalLead IS NULL)
AND (Episode.NextActionDate = @original_NextActionDate
OR Episode.NextActionDate IS NULL
AND @original_NextActionDate IS NULL)

1 个答案:

答案 0 :(得分:0)

好的,这就是我最终做的事情:

因为我使用了一个jquery下拉列表,它也使用了ashx处理程序,我有我需要的Facility名称,我只需要ID名称,所以我可以在提交时将ID发回数据库。所以我修改了sql命令以引入ID以及设施名称,如下所示:

  Public Class Search_VB : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    Dim prefixText As String = context.Request.QueryString("q")
    Dim conn As SqlConnection = New SqlConnection
    conn.ConnectionString = ConfigurationManager _
            .ConnectionStrings("CDI").ConnectionString
    Dim cmd As SqlCommand = New SqlCommand
    cmd.CommandText = ("select ID, FacName from Facilities where FacName like '%' + @SearchText + '%'")


    cmd.Parameters.AddWithValue("@SearchText", prefixText)
    cmd.Connection = conn
    Dim sb As StringBuilder = New StringBuilder

    conn.Open()
    Dim sdr As SqlDataReader = cmd.ExecuteReader
    While sdr.Read
        sb.Append(sdr("FacName") & (" ID: "))
        sb.Append(sdr("ID")).Append(Environment.NewLine)


    End While
    conn.Close()
    context.Response.Write(sb.ToString)

End Sub

the drop down list showing facility id's 所以现在我有了我的设施ID,我需要抓住设备ID准备好回传。

所以我在页面的脚本标记中更新了以下内容:

<script type="text/javascript">
    //the code below gets the jquery drop down working, and uses the ashx handler to feed it results to populate the list
    $(document).ready(function () {
        $('[id$=txt_Search]').autocomplete('/Facility_Search.ashx');


        //monitor for when the user clicks the update button.
        $("[id$=Button1]").click(function () {

            //fetch the selected value from the text box
            FacNameTextValue = $('[id$=txt_Search]').val()
            //pull off the prefix (the facility name, leaving only the characters after the : character, this should
            //only leave the facility ID
            FacNameTextValue = FacNameTextValue.substring(FacNameTextValue.indexOf(":") + 2);
            //Filter everything out except numbers
            FacNameTextValue = FacNameTextValue.replace(/\D/g, '');
            // Now if the string is empty, we need to alert the user that no Facility match was found, and they need to create a new one.


            //Now populate the text box with the filtered facility ID
            document.getElementsByName('DetailsViewEpisodeEdit$FacilityIDTextBox')[0].value = FacNameTextValue;
            //alert is for debug purposes bellow, comment out when the code is working :)
            //alert(":" + FacNameTextValue + ":");

            //now check the string, and if we are left with nothing, then alert the user that the record will be left blank
            if (FacNameTextValue.length < 1) {
                alert("No matching facilility was found, facility field will be left blank.")
            }







        });

    });
</script>

所以你有它。希望这可以帮助。我确信这是一种非常横向的方式。如果您有任何建议/改进,请告诉我们!