Solved

Dynamically create stored procedure   using c#

Posted on 2016-10-25
9
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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 34

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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