在查询中使用 MAX(CASE WHEN) 时出现奇怪的异常:DB2.iSeries.iDB2DCFunctionErrorException: 'An unexpected exception occurred
Having weird exception using MAX(CASE WHEN) in query: DB2.iSeries.iDB2DCFunctionErrorException: 'An unexpected exception occurred
我有以下查询将行中的一些数据放入列中:
string qry3 =
"SELECT TABLENAME1.SLONO AS ORDER, TABLENAME1.SLLNNO AS LINE " +
", MAX(CASE WHEN TABLENAME2.CZVRNM in ('SLEEVEDEPTH', 'LENGTH') THEN CAST(RTRIM(TABLENAME2.CZREFD) AS FLOAT(53)) END) AS SLEEVE " +
", MAX(CASE WHEN TABLENAME2.CZVRNM in ('INSTALLATION') THEN CAST(RTRIM(TABLENAME2.CZREFD) AS FLOAT(53)) END) AS DAMPER_AI " +
"FROM LOCATION.LOCATION2.TABLENAME1 TABLENAME1 LEFT JOIN LOCATION.LOCATION2.TABLENAME2 TABLENAME2 " +
" ON (TABLENAME1.SLONO = TABLENAME2.SPONO AND TABLENAME1.SLLNNO = TABLENAME2.SPLNNO) " +
"GROUP BY TABLENAME1.SLONO, TABLENAME1.SLLNNO ";
但是当我在下面的代码中尝试 运行 时:
try
{
conAS400.Open();
iDB2Command command = conAS400.CreateCommand();
command.CommandText = qry3;
command.CommandTimeout = 0;
iDB2DataReader reader = command.ExecuteReader();
dtAS400 = new DataTable();
dtAS400.Load(reader); //<-- The exception occurs here. I can't even catch it
conAS400.Close();
retrieved = true;
}
catch (Exception ex)
{
MessageBox.Show("Could not retrive the information. Exception: " + ex.Message);
retrieved = false;
}
finally
{
conAS400.Close();
}
我得到这个异常:
"IBM.Data.DB2.iSeries.iDB2DCFunctionErrorException: 'An unexpected
exception occurred. Type: System.AccessViolationException, Message:
Attempted to read or write protected memory. This is often an
indication that other memory is corrupt..'"
我还有其他查询可以正常工作,但这个查询不行。我什至在 SQL 上尝试了如下查询(感谢 Gordon Linoff 提供了这段代码):
SELECT
A.IDA, A.SomeInfo,
MAX(CASE WHEN VarName in ('Depth', 'Depth2') THEN VarValue END) AS Depth,
MAX(CASE WHEN VarName in ('Length') THEN VarValue END) AS Length
FROM
A
LEFT JOIN
B ON A.IDA = B.IDA
GROUP BY
A.IDA, A.SomeInfo
并且有效。我能做什么?
Drivers:
客户端访问 ODBC Driver(32 位)12.00.00.00
iSeries 访问 ODBC Driver12.00.00.00
编辑:这是 sql 查询而不是字符串。
SELECT TABLENAME1.SLONO AS ORDER, TABLENAME1.SLLNNO AS LINE,
MAX(CASE WHEN TABLENAME2.CZVRNM in ('SLEEVEDEPTH', 'LENGTH') THEN CAST(RTRIM(TABLENAME2.CZREFD) AS FLOAT(53)) END) AS SLEEVE,
MAX(CASE WHEN TABLENAME2.CZVRNM in ('INSTALLATION') THEN CAST(RTRIM(TABLENAME2.CZREFD) AS FLOAT(53)) END) AS DAMPER_AI
FROM LOCATION.LOCATION2.TABLENAME1 TABLENAME1 LEFT JOIN LOCATION.LOCATION2.TABLENAME2 TABLENAME2
ON (TABLENAME1.SLONO = TABLENAME2.SPONO AND TABLENAME1.SLLNNO = TABLENAME2.SPLNNO)
GROUP BY TABLENAME1.SLONO, TABLENAME1.SLLNNO
好的,所以我终于找到了问题所在。出于某种原因,我无法 CAST
漂浮在 MAX
中。如果我从查询中删除两个 CAST
,则异常消失并且一切正常。我不确定为什么会发生这种情况,如果有人能解释这一点,那将非常有帮助,因为我需要将这些值转换为浮动值。此外,如果我 SELECT
更多列,则有必要将所述列添加到 GROUP BY
语句中。无论如何,这是修改后的代码:
编辑:找到投射它们的方法。事实证明我得到了一些空值,一些空白字段,并且我试图将一个数字分配给一个字符串。这是现在的代码:
SELECT TABLENAME1.SLONO AS ORDER, TABLENAME1.SLLNNO AS LINE, TABLENAME1.PROMISEDT,
CAST(MAX(CASE WHEN RTRIM(TABLENAME2.CZREFD) IS NULL OR RTRIM(TABLENAME2.CZREFD) = '' THEN '0.000' WHEN TABLENAME2.CZVRNM = 'SLEEVEDEPTH' OR TABLENAME2.CZVRNM = 'LENGTH' THEN RTRIM(TABLENAME2.CZREFD) END) AS FLOAT(53)) AS SLEEVE,
MAX(CASE WHEN TABLENAME2.CZVRNM in ('INSTALLATION') THEN RTRIM(TABLENAME2.CZREFD) END) AS DAMPER_AI
FROM LOCATION.LOCATION2.TABLENAME1 TABLENAME1 LEFT JOIN LOCATION.LOCATION2.TABLENAME2 TABLENAME2
ON (TABLENAME1.SLONO = TABLENAME2.SPONO AND TABLENAME1.SLLNNO = TABLENAME2.SPLNNO)
GROUP BY TABLENAME1.SLONO, TABLENAME1.SLLNNO, TABLENAME1.PROMISEDT --remember to add every column from the SELECT here
我有以下查询将行中的一些数据放入列中:
string qry3 =
"SELECT TABLENAME1.SLONO AS ORDER, TABLENAME1.SLLNNO AS LINE " +
", MAX(CASE WHEN TABLENAME2.CZVRNM in ('SLEEVEDEPTH', 'LENGTH') THEN CAST(RTRIM(TABLENAME2.CZREFD) AS FLOAT(53)) END) AS SLEEVE " +
", MAX(CASE WHEN TABLENAME2.CZVRNM in ('INSTALLATION') THEN CAST(RTRIM(TABLENAME2.CZREFD) AS FLOAT(53)) END) AS DAMPER_AI " +
"FROM LOCATION.LOCATION2.TABLENAME1 TABLENAME1 LEFT JOIN LOCATION.LOCATION2.TABLENAME2 TABLENAME2 " +
" ON (TABLENAME1.SLONO = TABLENAME2.SPONO AND TABLENAME1.SLLNNO = TABLENAME2.SPLNNO) " +
"GROUP BY TABLENAME1.SLONO, TABLENAME1.SLLNNO ";
但是当我在下面的代码中尝试 运行 时:
try
{
conAS400.Open();
iDB2Command command = conAS400.CreateCommand();
command.CommandText = qry3;
command.CommandTimeout = 0;
iDB2DataReader reader = command.ExecuteReader();
dtAS400 = new DataTable();
dtAS400.Load(reader); //<-- The exception occurs here. I can't even catch it
conAS400.Close();
retrieved = true;
}
catch (Exception ex)
{
MessageBox.Show("Could not retrive the information. Exception: " + ex.Message);
retrieved = false;
}
finally
{
conAS400.Close();
}
我得到这个异常:
"IBM.Data.DB2.iSeries.iDB2DCFunctionErrorException: 'An unexpected exception occurred. Type: System.AccessViolationException, Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt..'"
我还有其他查询可以正常工作,但这个查询不行。我什至在 SQL 上尝试了如下查询(感谢 Gordon Linoff 提供了这段代码):
SELECT
A.IDA, A.SomeInfo,
MAX(CASE WHEN VarName in ('Depth', 'Depth2') THEN VarValue END) AS Depth,
MAX(CASE WHEN VarName in ('Length') THEN VarValue END) AS Length
FROM
A
LEFT JOIN
B ON A.IDA = B.IDA
GROUP BY
A.IDA, A.SomeInfo
并且有效。我能做什么?
Drivers:
客户端访问 ODBC Driver(32 位)12.00.00.00
iSeries 访问 ODBC Driver12.00.00.00
编辑:这是 sql 查询而不是字符串。
SELECT TABLENAME1.SLONO AS ORDER, TABLENAME1.SLLNNO AS LINE,
MAX(CASE WHEN TABLENAME2.CZVRNM in ('SLEEVEDEPTH', 'LENGTH') THEN CAST(RTRIM(TABLENAME2.CZREFD) AS FLOAT(53)) END) AS SLEEVE,
MAX(CASE WHEN TABLENAME2.CZVRNM in ('INSTALLATION') THEN CAST(RTRIM(TABLENAME2.CZREFD) AS FLOAT(53)) END) AS DAMPER_AI
FROM LOCATION.LOCATION2.TABLENAME1 TABLENAME1 LEFT JOIN LOCATION.LOCATION2.TABLENAME2 TABLENAME2
ON (TABLENAME1.SLONO = TABLENAME2.SPONO AND TABLENAME1.SLLNNO = TABLENAME2.SPLNNO)
GROUP BY TABLENAME1.SLONO, TABLENAME1.SLLNNO
好的,所以我终于找到了问题所在。出于某种原因,我无法 CAST
漂浮在 MAX
中。如果我从查询中删除两个 CAST
,则异常消失并且一切正常。我不确定为什么会发生这种情况,如果有人能解释这一点,那将非常有帮助,因为我需要将这些值转换为浮动值。此外,如果我 SELECT
更多列,则有必要将所述列添加到 GROUP BY
语句中。无论如何,这是修改后的代码:
编辑:找到投射它们的方法。事实证明我得到了一些空值,一些空白字段,并且我试图将一个数字分配给一个字符串。这是现在的代码:
SELECT TABLENAME1.SLONO AS ORDER, TABLENAME1.SLLNNO AS LINE, TABLENAME1.PROMISEDT,
CAST(MAX(CASE WHEN RTRIM(TABLENAME2.CZREFD) IS NULL OR RTRIM(TABLENAME2.CZREFD) = '' THEN '0.000' WHEN TABLENAME2.CZVRNM = 'SLEEVEDEPTH' OR TABLENAME2.CZVRNM = 'LENGTH' THEN RTRIM(TABLENAME2.CZREFD) END) AS FLOAT(53)) AS SLEEVE,
MAX(CASE WHEN TABLENAME2.CZVRNM in ('INSTALLATION') THEN RTRIM(TABLENAME2.CZREFD) END) AS DAMPER_AI
FROM LOCATION.LOCATION2.TABLENAME1 TABLENAME1 LEFT JOIN LOCATION.LOCATION2.TABLENAME2 TABLENAME2
ON (TABLENAME1.SLONO = TABLENAME2.SPONO AND TABLENAME1.SLLNNO = TABLENAME2.SPLNNO)
GROUP BY TABLENAME1.SLONO, TABLENAME1.SLLNNO, TABLENAME1.PROMISEDT --remember to add every column from the SELECT here