简体   繁体   English

此查询有什么问题,如何使它更有效?

[英]What is wrong with this query, and how can I make it more efficient?

I have a ProjectNum column that contains a string data type . 我有一个包含string data typeProjectNum column 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 . 通常这些数字是X1234...但是如果没有分配任何数字,则必须自动生成一个数字,并且根据分配给项目的优先级,取决于它是以C or F开头。 So an autogenerated number must begin with C or F and be followed by six digits and also auto-increment. 因此,自动生成的数字必须以C or F开头,后跟六位数字,并且还要自动递增。 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." 这使我可以获取最后一个自动递增的数字,然后可以通过添加1自动生成代码中的数字。问题是,当我发送@priorityDefID of 4 (目前数据库中没有以。开头的数字) “ C”),我收到错误消息"This expression is typed incorrectly, or it is too complex to be evaluated.例如,一个数字表达式可能包含太多复杂的元素。请尝试通过将表达式的某些部分分配给变量来简化该表达式“。

Not quite sure why this comes up with I pass 4, but a 1, 2 or 3 work fine and return the correct value. 不太确定为什么我会通过4,但是1、2或3可以正常工作并返回正确的值。 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? 我在考虑,而不是编写MAX ,只是抓住所有以C或F开头的字符,然后抓住正确的6位数字,即降序并抓住前1位?

That query has numerous issues and I'm not really clear about what all is going on so I don't know where to start. 该查询有很多问题,我不清楚发生了什么,所以我不知道从哪里开始。 However I can warn you about a danger of using CInt() with 6 digits. 但是,我可以警告您使用带有6位数字的CInt()的危险。 CInt("999999") throws an overflow error because the maximum integer value is 32,767. CInt("999999")抛出溢出错误,因为最大整数值为32,767。 You would be safer to use CLng because the maximum long integer value is 2,147,483,647 ... so a long integer will accommodate all possible 6 digit values. 使用CLng会更安全,因为最大长整数值为2,147,483,647 ...,因此长整数将容纳所有可能的6位数字值。

Although that issue may not be a source of trouble with the present ProjectNum values, it's something which may bite you in the future as you store more ProjectNum values. 尽管该问题可能不是使用当前ProjectNum值引起麻烦的根源,但随着您存储更多ProjectNum值,将来可能会困扰您。

Regarding "[the error message] comes up with I pass 4, but a 1, 2 or 3 work fine and return the correct value" , you also said "at the moment there are none in the database that begin with 'C'" . 关于“ [错误消息]随我通过4而出现,但是1、2或3可以正常工作并返回正确的值” ,您还说过“目前数据库中没有以'C'开头的内容” That means the query returns no rows in that situation. 这意味着在这种情况下查询不返回任何行。 I suspect that would cause a problem with RIGHT(ProjectNum, 6) because Right(Null, 6) triggers an invalid use of Null error. 我怀疑这会导致RIGHT(ProjectNum, 6)出现问题,因为Right(Null, 6)会触发对Null错误的无效使用

Check whether the error goes away after you add a row with a ProjectNum which begins with "C" . 添加以“ C”开头的ProjectNum的行后,检查错误是否消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM