SQL连接不返回所有字段值;订单更改时返回的不同字段值

时间:2011-12-27 22:54:50

标签: sql sql-server-2005

我正在使用以下SQL查询3个简单表并撤回结果。当我在SQL Server 2005 EM(正在使用的数据库)中运行此查询时,将返回正确的记录集结果。当我通过SQL语句或存储过程从我的网页(ASP)执行此操作时,我得到一些列的空白值。另外,我注意到当我更改在FROM子句中选择的表的顺序时,之前的一些列会返回空值。我的SQL有什么问题,因为它在调用时无法工作吗?我在本地开发了我的应用程序,它工作正常。当我将它部署到客户端的网络时,问题就开始了......客户端正在运行Win 2000 SP4作为他们的应用程序服务器,我的应用程序是在带有SQL Server 2005数据存储的ASP 3.0中开发的。

SELECT
Scorecard_Measure.Measure,
Scorecard_Measure.Target,
Scorecard_Measure.YTD,
Scorecard_Measure.Status,
Scorecard_Measure.Explanation,
Scorecard_Measure.Division,
Scorecard_Measure.ZIndex,
Scorecard_Measure.LastUpdated,
Scorecard_Measure.ID,
Scorecard_Objectives.Details,
Scorecard_Objectives.ZIndex,
Scorecard_ObjectiveCats.IdentityLetter
FROM
[Scorecard_Measure],
[Scorecard_Objectives],
[Scorecard_ObjectiveCats]
WHERE
Scorecard_Measure.ObjID=Scorecard_Objectives.ID
AND Scorecard_Objectives.ObjCatID = Scorecard_ObjectiveCats.ID
AND Scorecard_Measure.FiscalYear = '2011'
AND Scorecard_Measure.Publish='Y'
ORDER BY Scorecard_Measure.LastUpdated DESC

Scorecard_Objectives.ID是Scorecard_Measure表中的外键,Scorecard_ObjectiveCats.ID是Scorecard_Objectives表中的外键。

此外,一个奇怪的发生。我有两个相同的列名,当我引用这些列时,服务器没有像我之前所说的那样抛出一个错误,说“你的结果具有相同名称的列,使用它们来自的表引用它们” ......例如rs(“Scorecard_Objectives.ZIndex”)和rs(“Scorecard_Measure.ZIndex”) - 当我使用这些引用时,我从IIS收到错误。

赞赏任何观点。提前谢谢!

1 个答案:

答案 0 :(得分:0)

使用不同的变量访问两个ZINDEX。试试这个连接查询。

SELECT
Scorecard_Measure.Measure,
Scorecard_Measure.Target,
Scorecard_Measure.YTD,
Scorecard_Measure.Status,
Scorecard_Measure.Explanation,
Scorecard_Measure.Division,
Scorecard_Measure.ZIndex as MZIndex,
Scorecard_Measure.LastUpdated,
Scorecard_Measure.ID,
Scorecard_Objectives.Details,
Scorecard_Objectives.ZIndex as OZIndex,
Scorecard_ObjectiveCats.IdentityLetter
FROM
Scorecard_Measure INNER JOIN Scorecard_Objectives 
ON Scorecard_Measure.ObjID=Scorecard_Objectives.ID
AND Scorecard_Measure.FiscalYear = '2011'
AND Scorecard_Measure.Publish='Y'
INNER JOIN Scorecard_ObjectiveCats 
ON Scorecard_Objectives.ObjCatID = Scorecard_ObjectiveCats.ID
ORDER BY Scorecard_Measure.LastUpdated DESC