1

I have a ProjectNum column that contains a string data type. Normally the numbers are X1234... but if there is no number assigned, then one must be autogenerated and depending on the priority assigned to the project depends whether it begins with C or F. So an autogenerated number must begin with C or F and be followed by six digits and also auto-increment. So here is my query...

SELECT MAX(CINT(RIGHT(ProjectNum, 6))) AS LastDigits
FROM project_master_query
WHERE ((ProjectNum LIKE (IIF([@priorityDefID] = 4, "C*", "F*"))));

This allows me to grab the last auto-incremented number and then I can autogenerate a number in code by adding 1. The issue is, when I send in @priorityDefID of 4 (and at the moment there are none in the database that begin with "C"), I receive the error "This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables."

Not quite sure why this comes up with I pass 4, but a 1, 2 or 3 work fine and return the correct value. I was thinking of instead of writing MAX, just grabbing all of them that begin with C or F and then grab the right 6 digits, order by descending and grabbing the top 1?

4

1 回答 1

1

该查询有很多问题,我不太清楚发生了什么,所以我不知道从哪里开始。但是,我可以警告您使用CInt()6 位数字的危险。 CInt("999999")引发溢出错误,因为最大整数值为 32,767。您使用起来会更安全,CLng因为最大长整数值为 2,147,483,647 ... 所以长整数将容纳所有可能的 6 位值。

尽管该问题可能不是当前值的麻烦来源ProjectNum,但随着您存储更多值,它可能会在将来咬住您ProjectNum

关于“[错误消息]我通过了 4,但是 1、2 或 3 工作正常并返回正确的值”,您还说“目前数据库中没有以 'C' 开头的” . 这意味着查询在这种情况下不返回任何行。我怀疑这会导致问题, RIGHT(ProjectNum, 6)因为Right(Null, 6)触发无效使用 Null错误。

在添加以"C"ProjectNum开头的行后,检查错误是否消失。

于 2013-05-17T13:42:01.977 回答