简体   繁体   English

将参数传递给存储过程

[英]Passing Parameters to a stored Procedure

I am working on a project that accepts 3 different parameters, the date is required and the First and Last name are optional. 我正在一个接受3个不同参数的项目中,日期是必需的,名字和姓氏是可选的。 We setup the query below, but even if I change the parameters on the report (SSRS) it still looks at @LetterCreated as being '1/1/1950', any ideas on how I can get this to just accept the parameters? 我们在下面设置查询,但是即使我更改报表(SSRS)上的参数,它仍然将@LetterCreated视为“ 1/1/1950”,关于如何获取该参数以仅接受参数有任何想法吗? We set the date this way because we want the report to show with all of the reports when it is initially opened. 我们以这种方式设置日期,因为我们希望报表在初次打开时与所有报表一起显示。

Alter Proc
AS 
BEGIN
SET NOCOUNT ON;

    DECLARE @LetterCreated DATETIME,
    @FirstName VARCHAR(20),
    @LastName VARCHAR(20) 

    SELECT @LetterCreated = '1/1/1950'
    SELECT @FirstName = ''
    SELECT @LastName = ''

    SELECT  
        LETTERCREATETIME,
        Firstname,
        LastName,
    From RedFlagAddress

    WHERE 
        CASE WHEN @LetterCreated='1/1/1950' 
             THEN '1/1/1950' 
             ELSE ISNULL(LETTERCREATETIME, '07/05/81')
        END = @LetterCreated 
    AND (LastName LIKE @LASTNAME + '%' AND FirstName LIKE @FirstNAME + '%')
    END

Any suggestions would be greatly appreciated. 任何建议将不胜感激。

You are setting the @lettercreated date in the procedure. 您正在过程中设置@lettercreated日期。 Variables defined within the procedure are not visible outside it. 在过程中定义的变量在过程外部不可见。

You should declare the parameters as parameters, and set the default in the declaration 您应该将参数声明为参数,并在声明中设置默认值

ALTER PROC yourproc
(
@LetterCreated DATETIME = '1950-1-1',
@FirstName VARCHAR(20) = '',
@LastName VARCHAR(20) = ''
)
as
begin
    select  
        LETTERCREATETIME,
        Firstname,
        LastName,
    From 
        RedFlagAddress
    where 
    (
         ISNULL(LETTERCREATETIME, '07/05/81') = @LetterCreated
         or
         @LetterCreated = '1950-1-1'
    )
    AND LastName LIKE @LASTNAME + '%' 
    AND FirstName LIKE @FirstNAME + '%'
end

Similar to podiluska's answer, you should set the parameters as part of the SP, as podiluska indicates, but then you should set the default values in SSRS, in the Parameters there. 与podiluska的答案类似,应按照podiluska的指示将参数设置为SP的一部分,但随后应在SSRS中的“参数”中设置默认值。

This will let the users see the defaults and use those for subscriptions, or override them as needed. 这将使用户可以看到默认值并将其用于订阅,或者根据需要覆盖它们。

Alter Proc Proc_Name
(
@LetterCreated DATETIME,
@FirstName VARCHAR(20) = null,
@LastName VARCHAR(20) = null
)
AS 
BEGIN
SET NOCOUNT ON;

This will pass null values as Default values to you Proc now you code should be able to handle null values if no values are provided for FirstName and LastName 这会将空值作为默认值传递给您,现在,如果没有为FirstName和LastName提供值,则您的代码应该能够处理空值

I'm guessing here but what you may want is 我猜这里,但是您可能想要的是

  • IF parameter @LetterCreated is null then it should not be used as a filter at all 如果参数@LetterCreated为null,则根本不应将其用作过滤器
  • IF the data in RedFlagData.LETTERCREATETIME is Null then it should be matched to a filter date of '07/05/81' FWR 如果RedFlagData.LETTERCREATETIME的数据为Null,则应将其与过滤日期'07/05/81' 07/05/81 '07/05/81'匹配

Assuming that you have the 'allow nulls' check on the RDL/RDLC set for the @LetterCreated parameter , the where needs to be changed, the optional filter can be set like so: 假设您已为@LetterCreated parameter设置了RDL/RDLC的“允许空值”检查where需要更改的位置,可以如下设置可选过滤器:

ISNULL(@LetterCreated, LETTERCREATETIME) = ISNULL(LETTERCREATETIME, '07/05/81')

If you get rid of the magic date, then you can guarantee no date filter applied even if LETTERCREATETIME is null, by the filter: 如果您摆脱了魔术日期,那么即使LETTERCREATETIME为空,也可以保证没有应用任何日期过滤器:

ISNULL(@LetterCreated, LETTERCREATETIME) = ISNULL(LETTERCREATETIME) 
OR 
(@LetterCreated IS NULL AND LETTERCREATETIME IS NULL)

Thus: 从而:

ALTER PROC XYZ
(
  @LetterCreated DATETIME,
  @FirstName VARCHAR(20) = NULL,
  @LastName VARCHAR(20) = NULL
)
as
begin
    select  
        LETTERCREATETIME,
        Firstname,
        LastName,
    From 
        RedFlagAddress
    where 
    (
       ISNULL(@LetterCreated, LETTERCREATETIME) = LETTERCREATETIME
       OR
       (@LetterCreated IS NULL AND LETTERCREATETIME IS NULL)
    )
    AND LastName LIKE ISNULL(@LastName, '') + '%'
    AND FirstName LIKE ISNULL(@FirstName, '') + '%'
end

One caveat : The performance of this query will be terrible, given the amount of functional manipulation in the where clause, and the LIKEs and ORs - Hopefully RedFlagAddress is a relatively small table? 一个警告:考虑到where子句中的功能操作量以及LIKEsORs ,该查询的性能将很糟糕-希望RedFlagAddress是一个相对较小的表? If not, you may need to rethink your approach 如果没有,您可能需要重新考虑您的方法

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

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