简体   繁体   English

SQL Server存储过程参数

[英]SQL Server stored procedure parameters

I am developing a framework, where in I am a calling stored procedure with dynamically created parameters. 我正在开发一个框架,其中是一个带有动态创建参数的调用存储过程。 I am building parameter collection at the runtime. 我在运行时建立参数集合。

The problem occurs when I am passing a parameter to stored procedure, but stored proc doesn't accept such parameter. 当我将参数传递给存储过程但存储的proc不接受此类参数时,会发生问题。

For example, my stored procedure is: 例如,我的存储过程是:

CREATE PROCEDURE GetTaskEvents
    @TaskName varchar(50)
AS
BEGIN
-- SP Logic
END

Calling stored procedure as: 调用存储过程为:

EXEC GetTaskEvents @TaskName = 'TESTTASK', @ID = 2

This throws below error: 这引发以下错误:

Msg 8144, Level 16, State 2, Procedure GetTaskEvents, Line 0
Procedure or function GetTaskEvents has too many arguments specified.

This works fine in Sybase ASE, which simply ignores any additional parameters. 在Sybase ASE中,这可以正常工作,而ASE只是忽略任何其他参数。 Could this be achieved with MSSQL server 2008? 可以使用MSSQL Server 2008来实现吗? Any help, much appreciated. 任何帮助,不胜感激。 Thanks 谢谢

SQL Server doesn't allow you to pass parameters to a procedure that you haven't defined. SQL Server不允许您将参数传递给尚未定义的过程。 I think the closest you can get to this sort of design is to use optional parameters like so: 我认为最接近这种设计的是使用可选参数,如下所示:

CREATE PROCEDURE GetTaskEvents
    @TaskName varchar(50),
    @ID int = NULL
AS
BEGIN
-- SP Logic
END;

You would need to include every possible parameter that you might use in the definition. 您需要包括在定义中可能使用的所有可能的参数 Then you'd be free to call the procedure either way: 然后,您可以随意调用以下任一过程:

EXEC GetTaskEvents @TaskName = 'TESTTASK', @ID = 2;
EXEC GetTaskEvents @TaskName = 'TESTTASK'; -- @ID gets NULL here

Why would you pass a parameter to a stored procedure that doesn't use it? 为什么将参数传递给不使用该参数的存储过程?

It sounds to me like you might be better of building dynamic SQL statements and then executing them. 在我看来,您可能最好先构建动态SQL语句然后执行它们。 What you are trying to do with the SP won't work, and even if you could change what you are doing in such a way to accommodate varying numbers of parameters, you would then essentially be using dynamically generated SQL you are defeating the purpose of having/using a SP in the first place. 您尝试使用SP进行的操作将无法正常工作,即使您可以通过改变方式来适应各种数量的参数,但是实际上使用的是动态生成的SQL,这使您无法实现SP的目的。首先具有/使用SP。 SP's have a role, but there are not the solution in all cases. SP可以发挥作用,但并非在所有情况下都提供解决方案。

I'm going on a bit of an assumption here, but I'm assuming the logic inside the procedure gets split up via task. 我在这里进行一些假设,但我假设过程中的逻辑通过任务分解。 And you cant have nullable parameters as @Yuck suggested because of the dynamics of the parameters? 而且由于参数的动态性,您不能像@Yuck那样具有可为空的参数吗?

So going by my assumption 所以按照我的假设

If TaskName = "Path1" Then Something 如果TaskName =“ Path1”,那么

If TaskName = "Path2" Then Something Else 如果TaskName =“ Path2”,则其他内容

My initial thought is, if you have separate functions with business-logic you need to create, and you can determine that you have say 5-10 different scenarios, rather write individual stored procedures as needed, instead of trying one huge one solution fits all approach. 我最初的想法是,如果需要使用业务逻辑来创建单独的功能,并且可以确定存在5-10种不同的方案,而是根据需要编写单个存储过程,而不是尝试一种适合所有解决方案的庞大解决方案方法。 Might get a bit messy to maintain. 维护时可能会有些混乱。

But if you must... 但是如果你必须...

Why not try dynamic SQL, as suggested by @EJ Brennan (Forgive me, i haven't touched SQL in a while so my syntax might be rusty) That being said i don't know if its the best approach, but could this could possibly meet your needs? 为什么不尝试动态SQL,如@EJ Brennan所建议的(请原谅,我有一段时间没有接触过SQL,所以我的语法可能会很生疏)也就是说,我不知道它是否是最好的方法,但是可以吗?可能满足您的需求?

CREATE PROCEDURE GetTaskEvents
    @TaskName varchar(50)
    @Values varchar(200)
AS
BEGIN
  DECLARE @SQL VARCHAR(MAX)

  IF @TaskName = 'Something'
  BEGIN
    @SQL = 'INSERT INTO.....' + CHAR(13)
    @SQL += @Values + CHAR(13) 
  END

  IF @TaskName = 'Something Else'
  BEGIN
    @SQL = 'DELETE SOMETHING WHERE' + CHAR(13)
    @SQL += @Values + CHAR(13) 
  END

  PRINT(@SQL)
  EXEC(@SQL)    
END

(The CHAR(13) adds a new line.. an old habbit i picked up somewhere, used to help debugging/reading dynamic procedures when running SQL profiler.) (CHAR(13)添加了一条新行。.我在某个地方拾起了旧的习惯,用于在运行SQL事件探查器时帮助调试/读取动态过程。)

CREATE PROCEDURE GetTaskEvents
@TaskName varchar(50),
@Id INT
AS
BEGIN
-- SP Logic
END

Procedure Calling 程序调用

DECLARE @return_value nvarchar(50)

EXEC  @return_value = GetTaskEvents
        @TaskName = 'TaskName',
        @Id =2  

SELECT  'Return Value' = @return_value

您正在解析错误的参数组合。 @TaskName =传递@TaskName =@ID而不是@TaskName = .SP仅需要一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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