Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I did some research on this but didn't get the exact solution that i am looking for.
Basically what i am trying to do is, insert a dynamic value to a dynamic column of the dynamic table. I came across 2 ways for doing that.

case 1:
Static Declaration of the number of variables needed for the insertion, So here "No. of Columns defined for the insertion" will be pre-defined.
This works well , no issues.

case2:
But again i even want to define this "No. of Columns for the insertion" DYNAMICALLY.
This causes the problem, since i cannot pass the block level variables from runtime.

I do not want to go with case 1, i prefer case 2 since i can write only one stored procedure instead of 'N' numbers.

Below is the code for both cases:

CASE 1:
SQL
CREATE PROCEDURE [dbo].[DYN_SP_INSERT]
  
  @Tabname NVARCHAR(511),
  @COL1NAME NVARCHAR(MAX),
  @COL1VALUE NVARCHAR(MAX)
  
AS
BEGIN
  SET NOCOUNT ON;

  DECLARE @sql NVARCHAR(MAX);
  DECLARE @params NVARCHAR(max);

BEGIN
  SET @sql = 'INSERT INTO ' + @Tabname + ' 
  ('+QUOTENAME(@COL1NAME)+') 
  VALUES 
  (@C1V)';
  set @params ='@C1V NVARCHAR(MAX)'

  EXEC sp_executesql @sql,@params,@C1V=@COL1VALUE;
  
  END
END


CASE 2:
SQL
CREATE PROCEDURE [dbo].[DYN_SP_INSERT]
  
  @NOC int, --[FYI-no. of columns to be inserted based on runtime value-]
  @Tabname NVARCHAR(511)
  
AS
BEGIN
  SET NOCOUNT ON;

  DECLARE @sql NVARCHAR(MAX);
  DECLARE @params NVARCHAR(max);

if(@NOC=1)--this is for to define the "No. of Columns for Insertion @runtime"
BEGIN
  DECLARE @COL1NAME NVARCHAR(MAX);--block level variables
  DECLARE @COL1VALUE NVARCHAR(MAX);

  SET @sql = 'INSERT INTO ' + @Tabname + ' 
  ('+QUOTENAME(@COL1NAME)+') 
  VALUES 
  (@C1V)';
  set @params ='@C1V NVARCHAR(MAX)'

  EXEC sp_executesql @sql,@params,@C1V=@COL1VALUE;
  
  END
if(@NOC=2)
BEGIN
--HERE WILL BE GOING TO INSERT 2 VALUES FOR 2 DYNAMIC COLUMNS..
END
if(@NOC=3)
BEGIN
--HERE WILL BE GOING TO INSERT 3 VALUES FOR 3 DYNAMIC COLUMNS..SO ON..
END
END


EXECUTION OF BOTH CASE 1 & 2:
CASE 1:
SQL
EXEC DYN_SP_INSERT 'DYN_TEST_TABLE','PROJCODE','ASTR998'

->works fine no issues.

CASE 2:
SQL
EXEC DYN_SP_INSERT '1','DYN_TEST_TABLE','PROJCODE','ASTR998'


-> THIS throws an error.
"Procedure or function DYN_SP_INSERT has too many arguments specified."
Since only 2 variables declared in the main.it is not conisdering block level variables.

So how do i pass values to block level variables.[@COL1NAME ,@COL1VALUE ]..?
And
Is there any performance issues with this kind of stored procedure.?

Thanks in advance.
Posted
Updated 19-Feb-14 5:55am
v6

Hi,

Please refer here

http://social.msdn.microsoft.com/Forums/en-US/2175febd-a6b1-4acd-a431-a71beeea1329/variable-number-of-arguments-in-a-stored-procedure?forum=transactsql[^]

I believe this is what you are looking for.

Please let me know if that helps you.
 
Share this answer
 
Comments
Prathap S V 19-Feb-14 12:45pm    
Thanks for the link, i did go through it, but still it didn't solve my problem. Most of the answers are like its not possible to pass 'Values' to 'Block level variables' from the runtime 'DIRECTLY'.
But it lead me to give a try by passing an XML or Delimited value and later setting it to the block level variables. I will be going to try that, but meanwhile
If you come to know any other way of doing, please let me know.
Yes, it is!

Have a look at SP's declaration:
SQL
CREATE PROCEDURE [dbo].[DYN_SP_INSERT]
  
  @NOC int, --[FYI-no. of columns to be inserted based on runtime value-]
  @Tabname NVARCHAR(255), -- destination table
  @Cols NVARCHAR(MAX), -- the names of coulmns
  @Vals NVARCHAR(MAX)  -- values to be inserted
AS
BEGIN
  SET NOCOUNT ON;

  -- here comes the body of SP

END


How to call above SP?
SQL
EXEC DYN_SP_INSERT 2, 'DYN_TEST_TABLE', 'Col1,Col2,Col3,Col4', 'PROJCODE,ASTR998,RUMBA,CHACHA'


The basic idea is to use Common Table Expression[^] to split column names and values from third and fourth input parameters. For further information, please see: this set of answers[^]

Try! When you get stuck, please come back here and ask detailed question.
 
Share this answer
 
Try this
SQL
EXEC DYN_SP_INSERT @NOC=1,@col1=DYN_TEST_TABLE,@col2=PROJCODE,@col3=ASTR998
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900