存储过程SQL Server参数

时间:2012-08-28 08:31:31

标签: sql sql-server-2005 stored-procedures

我想为以下SQL Server表编写存储过程:

No    Date          Days       Percentage
 1    17-Aug-12      2          -0.34%
 2    3-Aug-12       15         -29.39%
 3    27-Jul-12      22          12.02%
 4    20-Jul-12      29         -0.64%
 5    13-Jul-12      36         -3.45%
 6    6-Jul-12       43         -99.93%
 7    29-Jun-12      50          41.26%

这里我想动态地将参数传递给百分比列或日期列。

请帮助wite存储过程仅选择范围内的百分比,例如'12% - 15%'

3 个答案:

答案 0 :(得分:1)

简单地检查百分比......

CREATE PROCEDURE [GetPercentages]
( @Low INT, @High INT )
AS
BEGIN
    SELECT *
    FROM [MyTable]
    WHERE [Percentage] BETWEEN @Low AND @High
END

对于更复杂的检查(动态指定表和/或字段),您可以考虑构建一个针对sp_executesql运行的TSQL字符串。

答案 1 :(得分:0)

您可以将minvalue和maxvalue等参数作为两个不同的参数传递,然后将它们用于您的SP。

答案 2 :(得分:0)

尝试类似

的内容
IF EXISTS (SELECT name 
           FROM sys.procedures 
           WHERE name = N'myspSelectDateRange') 
DROP PROCEDURE myspSelectDateRange;
GO
CREATE PROCEDURE myspSelectDateRange @DateA DATETIME, @DateB DATETIME 
AS
SELECT [SeqNo], [MyDate], [Days], [Percentage] 
FROM MyTable 
WHERE [MyDate] 
BETWEEN @DateA AND @DateB;
GO

我希望这会有所帮助。