Is it possible to DECLARE variable names and set the values in TSQL dynamically at the start of a stored procedure so that the "setting/value" pairs may be used throughout the stored procedure?
Something along these lines...
It would be a great benefit to store my "settings" in a centralized location such as a table. If this is not possible please post any ideas to achieve the same goal.
Thanks!
Something along these lines...
Code:
-- settings table
CREATE TABLE tbl_Settings (
SETTING_ID int IDENTITY(1,1) NOT NULL,
SETTING nvarchar(max) NOT NULL,
VALUE nvarchar(max) NOT NULL
)
-- some data
INSERT INTO tbl_Settings
SELECT 'MaxRows', '1000'
--stored procedure
CREATE PROCEDURE MyProc
AS
BEGIN
DECLARE @SETTINGID
DECLARE @SETTING nvarchar(max)
DECLARE @VALUE nvarchar(max)
SELECT *
INTO #tempSETTINGS
FROM tbl_SETTINGS
WHILE (SELECT COUNT(*) FROM #tempSETTINGS) > 0
BEGIN
SELECT TOP 1
@SETTINGID = SETTING_ID
,@SETTING = SETTING
,@VALUE = VALUE
FROM #tempSETTINGS
-- is something like this possible?
DECLARE 'var' + @SETTING AS nvarchar(max)
SET 'var' + @SETTING = @VALUE
DELETE #tempSETTINGS
WHERE SETTING_ID = @SETTINGID
END
END
--do something with variable
IF @varMaxRows >= 1000
SELECT 'It Works!'
Thanks!
Comment