我的问题是计数问题,如果我通过比较emp_id尝试另一种方法,则不允许我说emp_id> 10,因为它的类型为char。我已经尝试了很多方法来检查行是否大于或等于10,但是到目前为止,还没有找到对我有用的任何东西。我将从存储过程中摘录一个片段。
begin
if exists (select emp_id from employee where pub_id = @pubId group by emp_id having count(emp_id) > 10)
throw 500001, 'Only 10 employees can be assigned to a publisher',1
end
答案 0 :(得分:2)
您是否尝试过使用计数并再次检查?我发现您的病情很重要,这不是我在说的。
begin
if (select count(*) from employee where pub_id = @pubId) > 10
throw 500001, 'Only 10 employees can be assigned to a publisher',1
end
您可能必须将该select语句存储到变量中,然后对照返回的值进行检查。
答案 1 :(得分:0)
您可以按照以下逻辑尝试。请记住,这只是一个想法,您可以限制单个emp_id的插入行最多10条记录。
INSERT INTO employee(emp_id,Col1,Col2,Col3)
SELECT * FROM
(
SELECT @emp_id,@Col1,@Col2,@Col3
-- Values are parameter value passed to the stored procedure
) A
WHERE 10 > (
SELECT COUNT(*)
FROM employee
WHERE pub_id = @pubId
AND emp_id = @emp_id
-- If I guess correct, You need 10 same emp_id per pub_id
-- If requirement is different, you can adjust your logic here
-- like you can remove the filter AND emp_id = @emp_id
-- This will consider any 10 records for the pub_id
)