【问题标题】:reverse the join command反转加入命令
【发布时间】:2016-03-29 06:40:54
【问题描述】:

我正在接手一个用 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

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

我的 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)

【问题讨论】:

  • 我仍然不清楚你在问什么。我读这个的方式,听起来你想更新外键(Episode.FacilityID)。这是笔误吗?
  • 为什么不直接使用 DropDownList?如果根据用户输入不存在,您是否要创建新设施?
  • Dustin - 我希望它通过 FacName 列搜索 Facility 表,获取匹配的 ID,并更新 Episode 表 FacilityID 列。我实际上有一个基于用户输入的 jquery 下拉列表。以前的版本使用了下拉菜单,但它会提取数千个结果 - 用户无法排序。
  • sqlfiddle.com/#!3/fdb1c/16 对于任何感兴趣的人,我已将查询放在这里。它的工作除了更新 FacilityID 列中的每条记录
  • 我仍然对您想要什么感到困惑 - 在您的第一个示例中,您在 select 语句 Episode.FacilityID = Facilities.ID 上有一个连接,而您的更新连接语句显示 Episode.FacilityID = Facilities。姓名。

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


【解决方案1】:

好的,这就是我最终要做的:

因为我使用的 jquery 下拉列表也使用了 ashx 处理程序,所以我有我需要的设施名称,我只需要 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>

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

【讨论】:

    猜你喜欢
    • 2020-03-11
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多