简体   繁体   English

SSMS-存储过程中的参数数量不同

[英]SSMS - Varying number of parameters in a stored procedure

I have an issue regarding the number of parameters I put in a stored procedure. 我在存储过程中放入的参数数量存在问题。 I'm automating a report with quarterly data (parameter format = 2012Q1, 2012Q2, and so on). 我正在使用季度数据自动执行报告(参数格式= 2012Q1、2012Q2,依此类推)。
If the user's first parameter starts with Q1, Q2, or Q3, there will only be 3 years of data so if the user's first parameter is 2012Q1, 2012Q2, or 2012Q3, the parameters will end at 2014Q4....the number of parameters will be different since it requires 12 parameters from 2012Q1-2012Q4, 11 parameters from 2012Q2-2014Q4, and 10 parameters from 2012Q3-2014Q4. 如果用户的第一个参数以Q1,Q2或Q3开头,则只有3年的数据,因此,如果用户的第一个参数是2012Q1、2012Q2或2012Q3,则参数将以2014Q4结尾。...参数的数量会有所不同,因为它需要使用2012Q1-2012Q4的12个参数,2012Q2-2014Q4的11个参数和2012Q3-2014Q4的10个参数。

Is it possible to change the number of parameters declared based on what the user's first parameter is like??? 是否可以根据用户的第一个参数的状态更改声明的参数数量??? Is there even a good way to tackle this? 有没有解决这个问题的好方法?

No this is not possible, however you can have the parameters have a default value of NULL, now it is not required to pass a value in. 否,这是不可能的,但是您可以使参数的默认值为NULL,现在不需要传入值。

example

create procedure prTest 
@i int = null, @i2 int = null
as
select @i,@i2
go

exec prTest 1  --just pass 1 parameter, 2nd is default
go
exec prTest   -- leave all as default
go
exec prTest @i2 = 5  --to pass in a param if it is not 1st, use names
go
exec prTest null,5  --same as above

See also: Optional and named arguments in C# a comparison with SQL Server stored procs 另请参见: C#中的可选和命名参数与SQL Server存储过程的比较

Instead of doing this why don't you just use start and end date or can they choose quarters from multiple years ad hoc? 除了这样做,您为什么不只使用开始日期和结束日期,或者他们可以临时从多年中选择季度?

I think you can simplify things immensely by just having two parameters. 我认为您只需两个参数即可极大地简化事情。 The first one will specify the start of your search period and the second one will specify the end. 第一个指定搜索周期的开始,第二个指定结束时间。 Whether these are dates, year-months, or names of periods is something you can figure out, 无论是日期,年份,月份或期间名称,您都可以确定,

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

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