我有一个带有几个子查询的SQL查询。子查询基本上将来自其他表的数据拉到一起,以便组合要从单个源访问的记录。设置是在访问时拉动添加的最新记录。但是,如果我要向该组添加新记录,并尝试稍后查找它,则主查询无法识别它已添加。此代码用于访问查询,该查询运行下面的代码以使用最新的员工记录填充表。
查询在
下面SELECT AC.REG_NR
,AC.DIS_NR
,AC.GEMSID
,AC.TMS_ID
,AC.EMP_NA
,AC.EMP_SEX_TYP_CD
,AC.EMP_EOC_GRP_TYP_CD
,AC.DIV_NR
,AC.CTR_NR
,AC.JOB_CLS_CD_DSC_TE
,AC.JOB_GRP_CD
,AC.Job_Function
,AC.Job_Group
,AC.Meeting_Readiness_Rating
,AC.Manager_Readiness_Rating
,CD.Employee_ID
,CD.Meeting_Readiness_Rating AS Expr1
,CD.Manager_Readiness_Rating AS Expr2
,CD.Meeting_End_Date
,CD.EmployeeFeedback
,CD.DevelopmentForEmployee1
,CD.DevelopmentForEmployee2
,CD.DevelopmentForEmployee3
,CD.DevelopmentForEmployee4
,CD.DevelopmentForEmployee5
,CD.Justification
,CD.Changed
,CD.Notes
FROM dbo.AC_Source AS AC
LEFT OUTER JOIN (
SELECT Employee_ID
,Meeting_Readiness_Rating
,Manager_Readiness_Rating
,Meeting_End_Date
,EmployeeFeedback
,DevelopmentForEmployee1
,DevelopmentForEmployee2
,DevelopmentForEmployee3
,DevelopmentForEmployee4
,DevelopmentForEmployee5
,Justification
,Changed
,Notes
,RowNum
FROM (
SELECT Employee_ID
,Meeting_Readiness_Rating
,Manager_Readiness_Rating
,Meeting_End_Date
,EmployeeFeedback
,DevelopmentForEmployee1
,DevelopmentForEmployee2
,DevelopmentForEmployee3
,DevelopmentForEmployee4
,DevelopmentForEmployee5
,Justification
,Changed
,Notes
,ROW_NUMBER() OVER (
PARTITION BY Employee_ID ORDER BY Meeting_End_Date DESC
) AS RowNum
FROM (
SELECT EmployeeID AS Employee_ID
,MeetingReadinessLevel AS Meeting_Readiness_Rating
,ManagerReadinessLevel AS Manager_Readiness_Rating
,logdate AS Meeting_End_Date
,EmployeeFeedback
,DevelopmentForEmployee1
,DevelopmentForEmployee2
,DevelopmentForEmployee3
,DevelopmentForEmployee4
,DevelopmentForEmployee5
,Justification
,Changed
,Notes
FROM dbo.AC_CDData_1
UNION ALL
SELECT Employee_ID
,Meeting_Readiness_Rating
,Manager_Readiness_Rating
,Meeting_End_Date
,'' AS EmployeeFeedback
,'' AS DevelopmentForEmployee1
,'' AS DevelopmentForEmployee2
,'' AS DevelopmentForEmployee3
,'' AS DevelopmentForEmployee4
,'' AS DevelopmentForEmployee5
,'' AS Justification
,'' AS Changed
,'' AS Notes
FROM dbo.TMS_Data_Latest_Career_Meeting_Rating
WHERE (Plan_Year = '2013')
) AS A
) AS B
WHERE RowNum = 1
) AS CD
ON AC.TMS_ID = CD.Employee_ID
添加数据时,会将其添加到AC_CDDATA_1表中,该表位于其中一个内部子查询中。它将无法识别它,因为最外层的select语句没有匹配的ID用于添加的新语句。我已经尝试了不同的子查询,它们都使用我添加的新ID。但是当您执行整个查询时,它无法识别新的ID。我尝试了一个RIGHT OUTER JOIN,但之后只包含来自INNER查询的员工,然后我尝试了一个FULL OUTER JOIN来拉取SQL Server中的所有记录,但是当我尝试在访问中运行下载时,我得到了一个无效的NULL使用错误。我的想法已经用完了,是在JOIN中还是其中一个子查询,还是我需要添加一些东西?
答案 0 :(得分:0)
为什么不将数据转储到临时表中,然后用更简单的语句更新它?性能会更好,代码更容易阅读。
答案 1 :(得分:0)
相当令人印象深刻的sql语句。但我相信如果你遵循数据库设计的规则,表格中不应该有重复的数据。
如果不知道你的桌子和关系设计,几乎不可能弄清楚你正在做什么是可能的。
一个简单的例子,说明为什么你不应该以这种方式设计你的查询是一个主要的细节关系。查询可能会尝试在主服务器之前添加详细信息。这将每次都产生错误。
要解决或开始一种新方法来实现你的目标,我会创建一个带有按钮的表单,并使用VBA代码一次运行一个单独的查询。