It is not clear why the stored process has a dependency on your global set of two parties in your example. I see two main possibilities: either SP has a global dependency at the time of creation (for example, code generation - case 1), or SP has a global runtime dependence (i.e. you have to choose between parameterization - case 2 - or self-configuration - Case3).
If it depends on the execution time, regardless of whether it is received from any place outside the SP and passed as a parameter or inside the SP, this is the main design decision. The choice of when to transfer data as a parameter and when to pull from tables is not quite a science, it all depends on all cases of use in real life in the system.
Case 1 - Code Generation:
DECLARE @SomeVariable int SET @SomeVariable = 'VALUE' FROM someTable --do stuff with @SomeVariable GO DECLARE @sp as varchar(MAX) SET @sp = ' CREATE PROCEDURE myProcedure -- I would actually name this myProcedure_ + CONVERT(varchar, @SomeVariable), since each proc generated might function differently ( @MyParameter ) AS SET NOCOUNT ON DECLARE @SomeVariable AS int -- This is going to be an initialized local copy of the global at time of SP creation SET @SomeVariable = ' + CONVERT(varchar, @SomeVariable) + ' --Do something --Do something using @SomeVariable SET NOCOUNT OFF RETURN 0 ' EXEC(@sp) -- create the procedure dynamically Executing the producedure normally as EXEC myProcedure or EXEC myProcedure_1, etc.
Case 2 - Parameterization:
DECLARE @SomeVariable int SET @SomeVariable = 'VALUE' FROM someTable --do stuff with @SomeVariable GO CREATE PROCEDURE myProcedure ( @MyParameter ,@SomeVariable int ) AS SET NOCOUNT ON --Do something --Do something using @SomeVariable SET NOCOUNT OFF RETURN 0 GO
Now when myProcedure is myProcedure , it should always be passed the @SomeVariable parameter. This is recommended if you regularly call the same SP with a different parameterization.
Case 3 - Configuration:
DECLARE @SomeVariable int SET @SomeVariable = 'VALUE' FROM someTable --do stuff with @SomeVariable GO CREATE PROCEDURE myProcedure ( @MyParameter ) AS SET NOCOUNT ON --Do something DECLARE @SomeVariable int SET @SomeVariable = 'VALUE' FROM someTable SET NOCOUNT OFF RETURN 0 GO
Now, whenever you execute EXEC myProcedure, you need to make sure that the table has been configured. This scenario is recommended for slowly changing configuration cases. In this case, you can wrap the initialization of @SomeVariable in a scalar-valued UDF, so that at any time when the same configuration is used in different SPs, they will all call through the same UDF, which frees you from changes to the configuration table settings (you donβt give your users SELECT permission for your tables, anyway?), and if the UDF should start to change depending on the user or the like, now you have a breakpoint that provides consistency, permissions and interface calling conventions:
DECLARE @SomeVariable int SET @SomeVariable = dbo.udf_Global(username, session, etc.) --do stuff with @SomeVariable GO CREATE PROCEDURE myProcedure ( @MyParameter ) AS SET NOCOUNT ON --Do something DECLARE @SomeVariable int SET @SomeVariable = dbo.udf_Global(username, session, etc.) SET NOCOUNT OFF RETURN 0 GO