public static string GerarSelect(SqlTable sqlTable) { string str = ""; if (!sqlTable.IsTableNo) { str = str + string.Format("-- *****************************************************\n-- *** Criado em ........ {0}\n-- *** Criado por ....... CSGen\n-- *** Descricao ........ Procedure criada para {1} registros na tabela {2}\n-- *** Alterado em ......\n-- *** Alterado por .....\n-- *** Motivo alteracao .\n-- *****************************************************\n\n", string.Format("{0:dd/MM/yyyy HH:mm}", DateTime.Now), "selecionar", sqlTable.Nome); str = str + string.Format("IF EXISTS (SELECT * FROM sysobjects \nWHERE name = N'{0}'\nAND type = 'P')\nDROP PROCEDURE {0} \n#go#\n\n", string.Format("usp_{1}{0}", sqlTable.ProcNome, Program.sulfixSelect)) + string.Format("create proc usp_{1}{0}\n", sqlTable.ProcNome, Program.sulfixSelect) + GerarParametros(sqlTable.Colunas) + "set nocount on\n"; string str2 = ""; foreach (SqlColumn column in sqlTable.Colunas) { str2 = str2 + "" + column.Name + ", "; } if (str2 != string.Empty) { str2 = str2.Remove(str2.Length - 2, 1); } string str3 = str; str = str3 + "select " + str2 + " from " + sqlTable.Nome + " where \n"; foreach (SqlColumn column2 in sqlTable.Colunas) { if (column2.SqlDataType != "text") { string str11 = str; str = str11 + "(" + column2.Name + " = @" + column2.Name + " or @" + column2.Name + " is null) and \n"; } } return(str.Substring(0, str.Length - 6)); } List <SqlTable> list = new List <SqlTable>(); foreach (SqlReference reference in sqlTable.References) { list.Add(SqlTable.GetTable(reference.PkTable)); } for (int i = 0; i < list.Count; i++) { string str4; string str5; char ch; str = str + string.Format("-- *****************************************************\n-- *** Criado em ........ {0}\n-- *** Criado por ....... CSGen\n-- *** Descricao ........ Procedure criada para {1} registros na tabela {2}\n-- *** Alterado em ......\n-- *** Alterado por .....\n-- *** Motivo alteracao .\n-- *****************************************************\n\n", string.Format("{0:dd/MM/yyyy HH:mm}", DateTime.Now), "selecionar ", sqlTable.Nome + " & " + list[i].Nome); if (list.Count > 1) { if (i == 0) { ch = list[1].Colunas[0].Name[0]; str4 = SqlTable.GetBusinessClassName(sqlTable.References[0].PkTable) + "By" + ch.ToString().ToUpper() + list[1].Colunas[0].Name.Substring(1); str5 = string.Format("IF EXISTS (SELECT * FROM sysobjects WHERE name = N'{0}' AND type = 'P') DROP PROCEDURE {0} \n#go#\n\n", string.Format("usp_{1}{0}", str4, Program.sulfixSelect)); string str6 = str + str5 + string.Format("create proc usp_{1}{0}", str4 + "\n", Program.sulfixSelect); str = str6 + "@" + list[1].Colunas[0].Name + " " + list[1].Colunas[0].SqlDataType; } else { ch = list[0].Colunas[0].Name[0]; str4 = SqlTable.GetBusinessClassName(sqlTable.References[1].PkTable) + "By" + ch.ToString().ToUpper() + list[0].Colunas[0].Name.Substring(1); str5 = string.Format("IF EXISTS (SELECT * FROM sysobjects WHERE name = N'{0}' AND type = 'P') DROP PROCEDURE {0} \n#go#\n\n", string.Format("usp_{1}{0}", str4, Program.sulfixSelect)); string str7 = str + str5 + string.Format("create proc usp_{1}{0}", str4 + "\n", Program.sulfixSelect); str = str7 + "@" + list[0].Colunas[0].Name + " " + list[0].Colunas[0].SqlDataType; } } else if (list.Count == 1) { ch = list[0].Colunas[0].Name[0]; str4 = SqlTable.GetBusinessClassName(sqlTable.References[0].PkTable) + "By" + ch.ToString().ToUpper() + list[0].Colunas[0].Name.Substring(1); str5 = string.Format("IF EXISTS (SELECT * FROM sysobjects WHERE name = N'{0}' AND type = 'P') DROP PROCEDURE {0} \n#go#\n\n", string.Format("usp_{1}{0}", str4, Program.sulfixSelect)); string str7 = str + str5 + string.Format("create proc usp_{1}{0}", str4 + "\n", Program.sulfixSelect); str = str7 + "@" + list[0].Colunas[0].Name + " " + list[0].Colunas[0].SqlDataType; } str = str + "\nas\nset nocount on\nselect "; foreach (SqlColumn column3 in list[i].Colunas) { str = str + column3.Name + ", "; } str = str.Remove(str.Length - 2, 2); if (list.Count > 1) { switch (i) { case 0: { string str8 = str; str = str8 + " from " + list[i].Nome + " \nwhere " + list[0].Colunas[0].Name + " in \n(\n\tselect " + sqlTable.References[0].PkColumnName + " from " + sqlTable.Nome + " \n\twhere " + sqlTable.References[1].PkColumnName + " = @" + list[1].Colunas[0].Name + "\n)"; break; } case 1: { string str9 = str; str = str9 + " from " + list[i].Nome + " \nwhere " + list[1].Colunas[0].Name + " in \n(\n\tselect " + sqlTable.References[1].PkColumnName + " from " + sqlTable.Nome + " \n\twhere " + sqlTable.References[0].PkColumnName + " = @" + list[0].Colunas[0].Name + "\n)"; break; } } } else if (list.Count == 1) { string str9 = str; str = str9 + " from " + list[0].Nome + " \nwhere " + list[0].Colunas[0].Name + " in \n(\n\tselect " + sqlTable.References[0].PkColumnName + " from " + sqlTable.Nome + " \n\twhere " + sqlTable.References[0].PkColumnName + " = @" + list[0].Colunas[0].Name + "\n)"; break; } str = str + "\n\n#go#\n\n\n"; } return(str); }