Results 1 to 3 of 3

Thread: [RESOLVED] Save SP Result to Parameter

  1. #1

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Resolved [RESOLVED] Save SP Result to Parameter

    hi,

    how do i save result to parameter when using SP?



    i have something like this

    Code:
    SELECT SUM(price) 
    FROM employee 
    WHERE employee_id = @EmployeeID
    the problem is that it sometimes return NULL, i want to save the result to parameter and check if this parameter is null if so i will return 0


    something like that:

    Code:
    DECLARE @SumPrice decimal
    
    SET @SumPrice = SELECT SUM(price) 
    FROM employee 
    WHERE employee_id = @EmployeeID IS NULL
    i think i can do it like so...

    Code:
    IF SELECT SUM(price) 
    FROM employee 
    WHERE employee_id = @EmployeeID IS NULL
    BEGIN
    
    SELECT 0
    END
    ELSE
    BEGIN
    SELECT SUM(price) 
    FROM employee 
    WHERE employee_id = @EmployeeID IS NULL
    END
    but then i executing the query twice... (no good).... what is the best solution ?

    thanks!
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Save SP Result to Parameter

    What about this:

    code Code:
    1. SELECT ISNULL(SUM(price),0) As Summed
    2. FROM employee
    3. WHERE employee_id = @EmployeeID
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Save SP Result to Parameter

    very nice, i never knew you can use isnull in such way.

    Thanks!
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width