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
Solved

Dynamically create stored procedure   using c#

Posted on 2016-10-25
9
124 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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