[英]SQL Server function with count doesn't work as intended
I am working on a project for class & course management.我正在研究 class 和课程管理的项目。 I want to create a function that automatically creates a course code based on the course name and then adds a number based on whether there already exist a course name with the same first 4 characters.我想创建一个 function 根据课程名称自动创建课程代码,然后根据是否已经存在具有相同前 4 个字符的课程名称添加一个数字。
Here is what my function looks like:这是我的 function 的样子:
CREATE FUNCTION [dbo].[fxGenerateCourseCode]
(@course_name VARCHAR(50))
RETURNS VARCHAR(8)
AS
BEGIN
DECLARE @course_code VARCHAR(8), @count_course INT
SET @count_course = (SELECT COUNT(1) FROM dbo.COURSE_TB
WHERE SUBSTRING(course_name, 1, 4) = SUBSTRING(@course_name, 1, 4)) + 1
SELECT @course_code = UPPER(SUBSTRING(@course_name, 1, 4)) + ' - ' + '00' + CAST(@count_course AS VARCHAR)
RETURN @course_code
END
The problem is, whenever I execute this function, it is not returning the right count values and it doesn't include the 00
either.问题是,每当我执行这个 function 时,它都没有返回正确的计数值,它也不包括00
。
For example, executing this:例如,执行这个:
SELECT dbo.fxGenerateCourseCode('french')
returns FREN-0
rather than FREN-001
(which is what I expect).返回FREN-0
而不是FREN-001
(这是我所期望的)。
However, when I execute the code manually, it gives me the result that I expect:但是,当我手动执行代码时,它给了我预期的结果:
DECLARE @course_code VARCHAR(8), @count_course INT, @course_name VARCHAR(50)
SET @course_name = 'french'
SET @count_course = (SELECT COUNT(1) + 1 FROM dbo.PARAMETRES_COURS
WHERE SUBSTRING(nom_cours, 1, 4) = SUBSTRING(@course_name, 1, 4))
SELECT course_code = UPPER(SUBSTRING(@course_name, 1, 4)) + ' - ' + '00' + CAST(@count_course AS VARCHAR)
Result: FREN-001
.结果: FREN-001
。
I have tried changing the way I write the function with not much result.我尝试改变编写 function 的方式,但效果不大。 Can someone help me know where the problem here is coming from?有人可以帮我知道这里的问题来自哪里吗? Am I doing something wrong in the function?我在 function 中做错了吗?
Thank you谢谢
I suspect that you are putting the value into a column with a length of 6. That said, the problem would appear to be the spaces around the hyphen.我怀疑您将值放入长度为 6 的列中。也就是说,问题似乎出在连字符周围的空格上。 Also, always include a length for varchar
, because the defaut length varies by context and it might not do what you want:此外,始终包含varchar
的长度,因为默认长度因上下文而异,并且可能无法满足您的要求:
You can simplify your code and make it more accurate:您可以简化代码并使其更准确:
declare @course_code varchar(8), @count_course int;
select @count_course = count(1) + 1
from dbo.COURSE_TB
where left(course_name, 4) = left(@course_name, 4);
select @course_code = concat(left(upper(@course_name, 4)), '-', '00', @count_course);
Note that this still doesn't do what you want, which is padding the number.请注意,这仍然不能满足您的要求,即填充数字。 This version only works for 9 courses.此版本仅适用于 9 门课程。 You seem to want:你似乎想要:
select @course_code = concat(left(upper(@course_name, 4)), '-',
right(concat('00', @count_course), 3)
);
Note the use of the concat()
function, so you don't have to worry about lengths of strings when doing a conversion.注意concat()
function 的使用,因此在进行转换时不必担心字符串的长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.