Solved

Dynamically create stored procedure   using c#

Posted on 2016-10-25
9
147 Views
Last Modified: 2016-10-31
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
Comment
Question by:Varshini S
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41858580
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
 
LVL 33

Expert Comment

by:ste5an
ID: 41858591
Dynamically? This makes no sense... what is your use-case?
0
 

Author Comment

by:Varshini S
ID: 41858656
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 18

Expert Comment

by:Anil Golamari
ID: 41858697
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
 

Author Comment

by:Varshini S
ID: 41858838
I'm not using entity frame work. Is it possible to create a stored procedure without using
Entity Framework in c#?
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41858842
Yes what ever you want to do all can be handled in a single stored procedure.
0
 

Author Comment

by:Varshini S
ID: 41859001
Okay. Is it possible to do that using c# ? Create stored procedure using c#
0
 
LVL 33

Expert Comment

by:ste5an
ID: 41861746
What happens when the new column is part of the primary key?
0
 

Author Comment

by:Varshini S
ID: 41867742
New primary key column will be added in the string[] PrimaryKeyColumns.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question