简体   繁体   中英

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. 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? 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. 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.

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

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
  • IF the data in RedFlagData.LETTERCREATETIME is Null then it should be matched to a filter date of '07/05/81' FWR

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:

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:

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? If not, you may need to rethink your approach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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