我正在使用存储过程在我的数据库中运行一些查询。该值取自查询字符串,然后传递给存储过程。问题是,用户可以选择多个生成3个或更多查询字符串的选项。
e.g。 http://localhost.com/test.aspx?param=76¶m2=79
我知道如何从查询中获取值,但我是否使存储过程接受1或2或3个值有点像重载
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;
}
}
这是存储过程的副本..
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
答案 0 :(得分:3)
您可以在存储过程中使用默认参数:
CREATE PROCEDURE [dbo].[getSealRecordID]
( @PARAM int = -1,
@PARAM2 int = -1,
@PARAM3 int = -1
)
AS
...
当然,您必须在where子句中处理这些值:
...
WHERE (@PARAM = -1 OR (condition with @PARAM))
AND (@PARAM1 = -1 OR (condition with @PARAM1))
AND (@PARAM2 = -1 OR (condition with @PARAM2))
答案 1 :(得分:2)
您可以在存储过程的参数上加上默认值,然后您可以使用1,2或3个值来调用它。
CREATE PROC MyProc
(@Param1 int,
@Param2 int = null,
@Param3 int = null)
AS
-- body of proc here
答案 2 :(得分:1)
每个值都是int吗?或者每个都不同?
您的选择:
我建议使用数字1来处理客户端代码中的数据类型优先级和类型转换,如果它是相关的。否则,如果数据类型相同,则为可选参数/默认值。