使用count作为存储过程中的参数?

时间:2015-03-06 17:34:22

标签: sql stored-procedures count duplicates

我想尝试创建一个基于特定数字搜索重复行的存储过程(例如,如果我想看到2个相同行的实例,请使用"有计数(*) →2)

 select invoice.first_name, invoice.last_name, invoice.date_ordered,
 sum(total_cost) AS Total_Spent
 from invoice
 group by invoice.last_name
 having count(*)>2;

我想创建的是人们在调用程序时指定计数的选项,这样他们就不必进入程序,编辑查询,保存等等。

有没有办法把它变成参数?

2 个答案:

答案 0 :(得分:0)

试试这样:

set nocount on;
create proc dbo.test
   @nas int
as 
begin
select invoice.first_name, invoice.last_name, invoice.date_ordered,
 sum(total_cost) AS Total_Spent
 from invoice
 group by invoice.last_name
 having count(*)>@nas;
end

exec dbo.test @nas=2

答案 1 :(得分:0)

首先,你的意思是:

  

此人指定计数的选项

如果您想要指定返回的相同记录的数量,那么您可以:

ALTER PROCEDURE YourProcedureName (@Count INT) AS
BEGIN
 select invoice.first_name, invoice.last_name, invoice.date_ordered,
 sum(total_cost) AS Total_Spent
 from invoice
 group by invoice.last_name
 having count(*)>@Count;
END

这将创建一个输入参数,以便当用户运行该过程时,他们可以指定相同行的数量将大于@Count参数。

如果您希望用户能够依靠特定的列,那就变得有点棘手了。