views:

116

answers:

3

i am using a stored procedure to run some queries in my database. The value is taken from a query string then passed to the stored procedure. the thing is, the user may select more than 1 option that generates 3 or more query string.

e.g. http://localhost.com/test.aspx?param=76&param2=79

i know how to take the values from the query but i do i make the stored procedure accepts either 1 or 2 or 3 values kinda like overloading

e.g.

   setValue (int val)
    {
       this.value = val;
    }

    setValue (double val)
    {
       this.value = (int) val
    }

    setValue (string val)
    {
       try
       {
          this.value = Integer.parseInt(val)
       }
       catch (Exception e)
       {
         System.out.println(e.getMessage());
         return 0;
       }


    }

this is a copy of the stored procedure..

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[getSealRecordID] (@TRANSFER_ID int)
    AS
-- Declare variables


SELECT DISTINCT "FGFSTRANSFERS"."CONSIDERATION", "FGFSTRANSFERS"."AMOUNT_TRANSFER", "FGFSTRANSFERS"."DATE", 
"FGFSTRANSFERS"."TRANSFER_ID", 
CURR = CASE "FGFSTransferDetails"."CURR"
    WHEN 'USD' THEN 'United States Dollars'
    WHEN 'JMD' THEN 'Jamaican Dollars'
    WHEN 'CAD' THEN 'Canadian Dollars'
    WHEN 'GBP' THEN 'POUNDS'
    WHEN 'EUR' THEN 'EUROS'
    END
,"FGFSCUSTOMERS"."CMF_NAME_1", "FGFSCUSTOMERS"."CMF_NAME_2" , "FGFSTransferDetails"."AMOUNT", 
Cons_Curr = CASE "FGFSTransferDetails"."CURR" -- As Cons_Curr, 
    WHEN 'USD' THEN 'United States Dollars'
    WHEN 'JMD' THEN 'Jamaican Dollars'
    WHEN 'CAD' THEN 'Canadian Dollars'
    WHEN 'GBP' THEN 'POUNDS'
    WHEN 'EUR' THEN 'EUROS'
    END

,"FGFSTransferDetails"."DESCRIPTION", "FGFSTransferDetails"."IMID"
 FROM   ("FGFSInvestment"."dbo"."FGFSTransferDetails" "FGFSTransferDetails" INNER JOIN "FGFSInvestment"."dbo"."FGFSTRANSFERS" "FGFSTRANSFERS" ON "FGFSTransferDetails"."TRANSFER_ID"="FGFSTRANSFERS"."TRANSFER_ID") INNER JOIN "FGFSInvestment"."dbo"."FGFSCUSTOMERS" "FGFSCUSTOMERS" ON "FGFSTRANSFERS"."CUSTODIAN"="FGFSCUSTOMERS"."CMF_ACCOUNT"
 WHERE  "FGFSTRANSFERS"."TRANSFER_ID"= @TRANSFER_ID  AND "FGFSTransferDetails"."TRANSFER_ID"=@TRANSFER_ID
+3  A: 

You can use default parameters in your stored procedure:

CREATE PROCEDURE [dbo].[getSealRecordID] 
(   @PARAM int = -1,
    @PARAM2 int = -1,
    @PARAM3 int = -1
)
    AS
...

of course you'll have to handle those values in your where clause:

...
WHERE (@PARAM = -1 OR (condition with @PARAM))
  AND (@PARAM1 = -1 OR (condition with @PARAM1))
  AND (@PARAM2 = -1 OR (condition with @PARAM2))
fretje
hey thanks man, tried this and it worked!!! You just saved my life, thanks. God bless you
ferronrsmith
you're welcome :)
fretje
+2  A: 

you can put defaults on the parameters for your stored proc, then you can call it with 1, 2, or 3 values.

CREATE PROC MyProc
    (@Param1 int,
     @Param2 int = null,
     @Param3 int = null)
AS
-- body of proc here
Scott Ivey
thanks you too man
ferronrsmith
+1  A: 

Will each value be int? Or will each be different?

Your options:

  1. Deal with overloading in the client and call one stored proc
  2. Use optional parameters (eg one int, one float, one varchar OR default values)
  3. Use different stored procs

I'd suggest number 1 to deal with datatype precedence and type conversion in the client code, if it's relevent. Otherwise optional parameters/default values if same datatype.

gbn
it was just an example, i guess i throw u off sorry
ferronrsmith