0

I'm trying to create a stored procedure to summarize counts in a view I created, but I need to use a custom parameter because I want to filter on start and end dates (two of my parameters) and those are supposed to rely on user input.

For example, I want to have something like this except I want the @Name to be a value that is set by another person, not set in the procedure itself. What would be the best way to go about this?

DECLARE @Name VARCHAR(20)
SET @Name='User1'
SELECT
    SUM(
        CASE
            WHEN Name=@Name THEN Total*-1
            ELSE Total
        END
    ) [Total Adj]
    ,Date
FROM Table
GROUP BY Date
2
  • 1
    Your question is not clear. Are you saying that the start and end dates are declared by one user and the name by another? Commented Mar 10, 2013 at 18:29
  • Oh disregard the start/end date part. I'm looking at solely names in this case. Commented Mar 10, 2013 at 18:36

3 Answers 3

2

Just create a SP:

CREATE PROC some_proc @Name VARCHAR(20)
SELECT
    SUM(
        CASE
            WHEN Name=@Name THEN Total*-1
            ELSE Total
        END
    ) [Total Adj]
    ,Date
FROM Table
GROUP BY Date

And use it:

some_proc 'User1'
Sign up to request clarification or add additional context in comments.

4 Comments

What if I have multiple parameters (say I throw in a date parameter too)? Then would I call it as some_proc 'User1' 'Date1'?
Sure: CREATE PROC some_proc 'at'Name VARCHAR(20) , 'at'Date1 DATE. to call it just add date after a comma. GL!
Where would you type some_proc 'User1' when you're implementing the procedure? I didn't know you could just type it into the query window in MS SQL Server for example and it'd run. Cool!
Alright--but where would you type some_proc 'User1' when you're implementing the procedure?
0

You can call you so in following manner:

Exec spname 'name','param1','param2'

Comments

0

I prefer a table function. Basically the same as a procedure, but can be used similar to a view. Also, as a function is returning something, I prefer to use a function to return data. A procedure does something. But, this is only my personal opinion.

create function dbo.some_func(@Name Varchar(30))
returns table
as
begin
  return(
    SELECT
      SUM(CASE WHEN Name=@Name THEN Total*-1 ELSE Total END) [Total Adj]
         ,Date
    FROM Table
    GROUP BY Date
)
end

2 Comments

Using a function instead of a stored proc introduces limitations. For example, you can call one stored procedure from another, but you can't call one from a function.
@DanBracuk: Thank you very much for this comment. I never ran into this problem yet but it is interesting to know. However, in this case (inline table functions) these limitations do not apply. It is a nice 'parametrized view'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.