反转加入命令

reverse the join command

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

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

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

这是我的两个 table:

Episode                   Facilities

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

用户更新剧集以指向不同的设施 - 假设他们希望 Episode.ID“2”引用新设施,在这种情况下,'Brisbane'

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

Episode                   Facilities

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

所以它在设施 table 中进行查找并返回 Facilities.id 并在 episode.FacilityID 列

中使用该值

我的 select 陈述很有效,希望它能帮助任何渴望回答我的问题的人!

工作select声明

 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)

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

因为我使用的是 jquery 下拉列表,它也使用了 ashx 处理程序,所以我有我需要的设施名称,我只需要 ID 名称,这样我就可以 post 返回 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,我需要为 post-back 准备好设施 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>

好了。希望这可以帮助。我确信这是一种真正横向的方式。如果你有 suggestions/improvements,请告诉我!