SQL Server stored procedure parameters

前端 未结 5 1530
[愿得一人]
[愿得一人] 2020-12-08 07:09

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 pr

相关标签:
5条回答
  • 2020-12-08 07:39

    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?

    So going by my assumption

    If TaskName = "Path1" Then Something

    If TaskName = "Path2" Then Something Else

    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. Might get a bit messy to maintain.

    But if you must...

    Why not try dynamic SQL, as suggested by @E.J 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?

    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.)

    0 讨论(0)
  • 2020-12-08 07:41

    SQL Server doesn't allow you to pass parameters to a procedure that you haven't defined. 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
    
    0 讨论(0)
  • 2020-12-08 07:53
    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
    
    0 讨论(0)
  • 2020-12-08 07:59

    You are parsing wrong parameter combination.here you passing @TaskName = and @ID instead of @TaskName = .SP need only one parameter.

    0 讨论(0)
  • 2020-12-08 08:00

    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. 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's have a role, but there are not the solution in all cases.

    0 讨论(0)
提交回复
热议问题