如何知道从多个表中检索到的行以表格名称显示到网格视图

时间:2019-05-09 14:58:06

标签: sql tsql c#-4.0 asp.net-4.0

我有查询来获取重复记录,并将所有记录合并以成功绑定到网格视图。现在我只想知道在删除时所选记录正在从哪个表中生成。多个表行是否可能。

str += "(SELECT ";
                str += "s.Id, s.EmailAddress As ColumnValue,";
                str += "(SELECT Name FROM tbl_User_master ";
                str += "WHERE Id = s.user_id) AS CreatedBy,";
                str += "s.SubscriptionDateTime As CreatedDate,d.Count ";
                str += "FROM ";
                str += "(SELECT EmailAddress, COUNT(*) as Count ";
                str += "FROM tbl_Subscribers ";
                str += "WHERE user_id = '" + int.Parse(CommonLogic.GetSessionValue("user_id").ToString()) + "' ";
                str += "GROUP BY EmailAddress ";
                str += "HAVING COUNT(*) > 1) AS d ";
                str += "INNER JOIN ";
                str += "tbl_Subscribers s ON s.EmailAddress = d.EmailAddress)";
                str += " UNION ALL ";
                str += "(SELECT ";
                str += "f.Id,f.EmailAddress As ColumnValue,";
                str += "(SELECT Name FROM tbl_User_master WHERE Id = f.user_id) AS CreatedBy,";
                str += "f.CreatedDate As CreatedDate,d.Count ";
                str += "FROM ";
                str += "(SELECT EmailAddress, COUNT(*) AS Count ";
                str += "FROM tbl_From_master ";
                str += "WHERE user_id = '" + int.Parse(CommonLogic.GetSessionValue("user_id").ToString()) + "' ";
                str += "GROUP BY EmailAddress ";
                str += "HAVING COUNT(*) > 1) AS d ";
                str += "INNER JOIN ";
                str += "tbl_From_master f ON f.EmailAddress = d.EmailAddress)";
                str += " UNION ALL ";
                str += "(SELECT ";
                str += "c.Id,c.CategoryName As ColumnValue,(SELECT Name FROM tbl_User_master WHERE Id = c.user_id) As CreatedBy,NULL As CreatedDate,d.Count ";
                str += "FROM (";
                str += "SELECT CategoryName, COUNT(*) as Count ";
                str += "FROM tbl_Categories WHERE user_id='" + int.Parse(CommonLogic.GetSessionValue("user_id").ToString()) + "' ";
                str += "GROUP BY CategoryName ";
                str += "HAVING COUNT(*) > 1";
                str += ") AS d ";
                str += "INNER JOIN tbl_Categories c ON c.CategoryName = d.CategoryName)";
                str += " UNION ALL ";
                str += "(SELECT ";
                str += "t.Id,t.CategoryName As ColumnValue,(SELECT Name FROM tbl_User_master WHERE Id = t.user_id) As CreatedBy,NULL As CreatedDate,d.Count ";
                str += "FROM (";
                str += "SELECT CategoryName, COUNT(*) as Count ";
                str += "FROM tbl_Template_Categories WHERE user_id='" + int.Parse(CommonLogic.GetSessionValue("user_id").ToString()) + "' ";
                str += "GROUP BY CategoryName ";
                str += "HAVING COUNT(*) > 1";
                str += ") AS d ";
                str += "INNER JOIN tbl_Template_Categories t ON t.CategoryName = d.CategoryName)";
                str += " UNION ALL ";
                str += "(SELECT ";
                str += "t.Id,t.TemplateName As ColumnValue,(SELECT Name FROM tbl_User_master WHERE Id = t.user_id) As CreatedBy,t.Created_date As CreatedDate,d.Count ";
                str += "FROM (";
                str += "SELECT TemplateName, COUNT(*) as Count ";
                str += "FROM tbl_Template_master WHERE user_id='" + int.Parse(CommonLogic.GetSessionValue("user_id").ToString()) + "' ";
                str += "GROUP BY TemplateName ";
                str += "HAVING COUNT(*) > 1";
                str += ") AS d";
                str += " INNER JOIN tbl_Template_master t ON t.TemplateName = d.TemplateName)";

网格视图如何将具有唯一ID的每一行绑定在一起,但是如何从哪个表中知道给定ID。

0 个答案:

没有答案