如何让六段SQL查询整合在一起?
因为每段SQL查询到的资料是下一段查询语句的条件,再加上必须跑回圈去执行?边查资料边插入资料。
1. mySQL = "Select MAX(ProjectNo) AS MaxProjectNo From tblProject Where ProjectNo Like '" & strPrefix & strSerialYearMonth & "%'" '(取得ProjectNo (Prefix + YYYYMM + SSS 共10码, eg: S201109001))
2. mySQL1 = "SELECT ROW_NUMBER() OVER (ORDER BY C.CateMainID) AS RowNo,A.ProjectNo FROM tblProjectDevpHistory AS A "
mySQL1 += "INNER JOIN tblProject AS B ON A.ProjectNo = B.ProjectNo "
mySQL1 += "INNER JOIN tblBasCategory AS C ON B.CateMainID = C.CateMainID "
mySQL1 += "INNER JOIN tblProject AS B ON A.ProjectNo = B.ProjectNo "
mySQL1 += "WHERE A.DHDate >= '2020/05/01' And A.DHDate <= '2020/05/31' And A.ProjectNo < 'S2020' "
mySQL1 += "GROUP BY A.ProjectNo,C.CateMainID" '(取得ProjectNo List 2020/05/01 ~ 2020/05/31) - 共10多笔
3. mySQL2 += "SELECT A.CateMainID,A.CateSubID,A.ProjectName,A.KickOffDate,A.PlanDate,A.DepartmentNo,A.CreatorNo "
mySQL2 += "FROM tblProject AS A "
mySQL2 += "INNER JOIN tblBasCategory AS B ON A.CateMainID = B.CateMainID "
mySQL2 += "INNER JOIN tblProjectItem AS C ON A.ProjectNo = C.ProjectNo "
mySQL2 += "INNER JOIN tblProjectDevpHistory AS D ON A.ProjectNo = D.ProjectNo AND C.ItemNo = D.ItemNo AND D.DHDate >= '2020/05/01' AND D.DHDate <= '2020/05/31' "
mySQL2 += "WHERE A.ProjectNo = '" & myDR("ProjectNo").ToString & "' AND A.ProjectNo < 'S2020' "
mySQL2 += "GROUP BY A.CateMainID,A.CateSubID,A.ProjectName,A.KickOffDate,A.PlanDate,A.DepartmentNo,A.CreatorNo" '(依据2取得的ProjectNo查询Project基本资料)
4. mySQL = "Insert Into tblProject (ProjectNo, CateMainID, CateSubID, ProjectName, KickOffDate, PlanDate, DepartmentNo, CreatorNo) "
mySQL += "Values ('" & strNewProjectNo & "','" & myDR("CateMainID").ToString & "','" & myDR("CateSubID").ToString & "','" & myDR("ProjectName").ToString & "','" & myDR("KickOffDate").ToString & "','" & myDR("PlanDate").ToString & "','"
& myDR("DepartmentNo").ToString & "','" & myDR("CreatorNo").ToString & "') "
5. mySQL3 += "SELECT A.ItemNo,A.ItemName,A.DepartmentNo,A.PropertyDesc,A.KickOffDate,A.PlanDate,A.EngineerNo,A.AdvisorNo,A.CreatorNo "
mySQL3 += "FROM tblProjectItem AS A"
mySQL3 += "INNER JOIN tblProjectDevpHistory AS B ON A.ProjectNo = B.ProjectNo AND A.ItemNo = B.ItemNo AND B.DHDate >= '2020/05/01' AND B.DHDate <= '2020/05/31'"
mySQL3 += "WHERE A.ProjectNo= '" & myDR2("ProjectNo").ToString & "' AND A.ProjectNo < 'S2020'"
mySQL3 += "GROUP BY A.ItemNo,A.ItemName,A.DepartmentNo,A.PropertyDesc,A.KickOffDate,A.PlanDate,A.EngineerNo,A.AdvisorNo,A.CreatorNo" '(依据2取得的ProjectNo查询Item基本资料) - 笔数不定
6. mySQL = "Insert Into tblProject (ProjectNo, ItemID, ItemName, DepartmentNo, PropertyDesc, KickOffDate, PlanDate, EngineerNo, AdvisorNo, CreatorNo) "
mySQL += "Values ('" & strNewProjectNo & "','" & myDR("ItemID").ToString & "','" & myDR("ItemName").ToString & "','" & myDR("DepartmentNo").ToString & "','" & myDR("PropertyDesc").ToString & "','" & myDR("KickOffDate").ToString & "','"
& myDR("PlanDate").ToString & "','" & myDR("EngineerNo").ToString & "','" & myDR("AdvisorNo").ToString & "','" & myDR("CreatorNo").ToString & "') " (1+3+4+5+6须依据2跑回圈执行)
还是有人能将六段SQL整合成一段SQL语法执行?
写到3层While myDR.Read ... End While回圈,总觉得不太对,上来问问。