列名称无效的Menu_mkey

时间:2015-12-03 06:48:40

标签: c# asp.net sql-server-2005

在调试代码时,我收到错误

  

无效的列名称Menu_mkey

以下是

的代码
string strsqlflag = "select count(*) from WMS_User_Rights where User_Id='" + Hid_Selected_user.Value + "'  and delete_flag='N' ";


        if (Dt.Rows[e.Row.RowIndex]["child_menu_mkey"].ToString() == "0")
        {
            strsqlflag += " and  MENU_MKEY = '" + Dt.Rows[e.Row.RowIndex]["MKEY"] + "'";
        }
        else
        {
            strsqlflag += " and  MENU_MKEY = '" + Dt.Rows[e.Row.RowIndex]["child_menu_mkey"] + "'";
        }

生成以下查询

select count(*) from WMS_User_Rights where User_Id='1'  and delete_flag='N'  and  MENU_MKEY = '1'

现在,我在这里也想要Menu_mkey,但问题是它来自另一个来自表WMS_menu_rights的表

如何实现这一目标?

旁注: - 我正在使用SQL-server 2005

2 个答案:

答案 0 :(得分:1)

使用SQL Joins在查询中链接这些表。

例如:

a = 1:n;
ver = a(sum(A) < k+1 );

查询string strsqlflag = "select count(*) from WMS_User_Rights Inner join WMS_Menu_Rights on WMS_User_Rights.somefield=WMS_Menu_Rights.somefield where WMS_User_Rights.User_Id='" + Hid_Selected_user.Value + "' and WMS_User_Rights.delete_flag='N' "; if (Dt.Rows[e.Row.RowIndex]["child_menu_mkey"].ToString() == "0") { strsqlflag += " and WMS_Menu_Rights.MENU_MKEY = '" + Dt.Rows[e.Row.RowIndex]["MKEY"] + "'"; } else { strsqlflag += " and WMS_Menu_Rights.MENU_MKEY = '" + Dt.Rows[e.Row.RowIndex]["child_menu_mkey"] + "'"; } 中的somefield代表两个表中常见的任何字段。

答案 1 :(得分:0)

您必须为此撰写INNER JOIN查询:

select count(*) from WMS_User_Rights INNER JOIN WMS_menu_rights on WMS_menu_rights.someField = WMS_User_Rights.someField where User_Id='1'  and delete_flag='N' and MENU_MKEY = '1'