Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

Dynamically create stored procedure using c#

I have the following  input parameters  in c#
string TableName , string[] PrimaryKeyColumns. Based on this parameter i need to create a stored procedure Dynamically using c#.  Stored procedure using MERGE command to update or insert record in target table.

Here SOURCE table is a temp table (#TableName )

How to achieve this in C# ?

Below is the sample stored procedure. This stored procedure needs to be created Dynamically using c#

CREATE PROC  usp_+TableName
AS
BEGIN

    MERGE MYDB.dbo.TableName
    USING  #TableName ON SOURCE.PrimaryKeyColumn1=TARGET.PrimaryKeyColumn1 AND SOURCE.PrimaryKeyColumn2=TARGET.PrimaryKeyColumn2

    WHEN MATCHED THEN

        UPDATE Set Target.column3=Source.column3,Target.column4=Source.column4

        -- UPDATE
    WHEN NOT MATCHED THEN

        INSERT(column1,column2,column3,column4)
        VALUES(SOURCE.column1,SOURCE.column2,SOURCE.column3,SOURCE.column4);

   
END
0
Varshini S
Asked:
Varshini S
  • 4
  • 2
  • 2
  • +1
1 Solution
 
Pawan KumarDatabase ExpertCommented:
You can dynamically handle the table name and other parameters in the sp directly. No need to do things dynamically in c#. E.g.

CREATE PROC  usp_Proc
(
	@TableName VARCHAR(1000)
)
AS
BEGIN

	DECLARE @Cmd AS VARCHAR(MAX)= ''

    SET @Cmd = 'MERGE MYDB.dbo.' + @TableName + '
    USING #TableName ON SOURCE.PrimaryKeyColumn1=TARGET.PrimaryKeyColumn1 AND SOURCE.PrimaryKeyColumn2=TARGET.PrimaryKeyColumn2

    WHEN MATCHED THEN

        UPDATE Set Target.column3=Source.column3,Target.column4=Source.column4

        WHEN NOT MATCHED THEN

        INSERT(column1,column2,column3,column4)
        VALUES(SOURCE.column1,SOURCE.column2,SOURCE.column3,SOURCE.column4);'


	EXEC(@Cmd)
    
END

Open in new window

0
 
ste5anSenior DeveloperCommented:
Dynamically? This makes no sense... what is your use-case?
0
 
Varshini SAuthor Commented:
Use-case

I have two databases DB1  and DB2.  When i pass the table name and Primarykey columns to my C# application that will insert or update the records in  DB2 table using MERGE command.
Both DB1 and DB2 table schemas are same.   When new columns added in table the c# application drop the existing stored procedure and create  the new stored procedure with new column mappings.

Another function in my c# application will execute that stored procedure.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Anil GolamariCommented:
Are you using Entity Framework with code first approach to create these tables and store proc's in the first place? If that is the case then you should be able to drop and create new tables if there is any change in the model by using
Database.SetInitilizer(new DropCreateDatabaseIfModelchanges<dbcontext>); 

Open in new window

in global.asax.cs file inside application start.

Build Storeproc using Entity framework.
0
 
Varshini SAuthor Commented:
I'm not using entity frame work. Is it possible to create a stored procedure without using
Entity Framework in c#?
0
 
Pawan KumarDatabase ExpertCommented:
Yes what ever you want to do all can be handled in a single stored procedure.
0
 
Varshini SAuthor Commented:
Okay. Is it possible to do that using c# ? Create stored procedure using c#
0
 
ste5anSenior DeveloperCommented:
What happens when the new column is part of the primary key?
0
 
Varshini SAuthor Commented:
New primary key column will be added in the string[] PrimaryKeyColumns.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now