I'm trying to create a stored procedure using T-SQL to insert variables into a table. Using the method that I know of, you declare the variables in the creation of the stored procedure, but I believe it's possible to declare the variables as part of the exec statement to make the insert dynamic. I've googled and googled and googled and cannot find anything that supports this.
Here is my example code as it stands with static variables:
create procedure spInsert as
declare @insertValueOne varchar(5) = 'Test1'
declare @insertValueTwo varchar(5) = 'Test2'
declare @insertValueThree varchar(5) = 'Test3'
declare @insertValueFour varchar(5) = 'Test4'
begin
insert into testTable
(ValueOne, ValueTwo, ValueThree, ValueFour)
values
(@insertValueOne, @insertValueTwo, @insertValueThree, @insertValueFour)
end
exec spInsert
What I'm trying to achieve is a situation where I can can use an execution script like this for example:
exec spInsert('Test1', 'Test2', 'Test3', 'Test4')
Instead of creating static variables within the procedure. Allowing the user to execute the stored procedure without amending the contents of it.
Does this make sense? Any ideas?