Solved

Bulk Inserts with Stored Procedures

Posted on 2010-09-15
4
649 Views
Last Modified: 2012-05-10
Hello

I want a .net App to send data in bulk to a stored procedure, which will then use this data to delete all data in an existing table and perform a bulk insert.

Question: What type of variable do I use in my stored procedure?  Examples of code in .net that call a stored procedure with a 'bulk input' type of parameter? Example of a stored procedur that gets a 'bulk input' type of parameter and performs a bulk input

thanks
0
Comment
Question by:adimit
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
Vipul Patel earned 500 total points
ID: 33685176
I would prefer to use XML data type for bulk operations.
Please refer below example;

.NET code:


        public void BulkOperation()
        {
            string cns = @"Data Source=VIPSLAPTOP\SQLEXPRESS;Initial Catalog=ExpertsExchange;Integrated Security=SSPI;";
            string records = "<Operations>" +
                                    "<Delete>" +
                                        "<Row>" +
                                            "<ID>1</ID>" +
                                        "</Row>" +
                                        "<Row>" +
                                            "<ID>2</ID>" +
                                        "</Row>" +
                                    "</Delete>" +
                                    "<Insert>" +
                                        "<Row>" +
                                            "<ID>5</ID>" +
                                            "<Data>Five</Data>" +
                                        "</Row>" +
                                        "<Row>" +
                                            "<ID>6</ID>" +
                                            "<Data>Six</Data>" +
                                        "</Row>" +
                                    "</Insert>" +
                                    "<Update>" +
                                        "<Row>" +
                                            "<ID>3</ID>" +
                                            "<Data>vrluckyin</Data>" +
                                        "</Row>" +
                                    "</Update>" +
                                "</Operations>";
            SqlConnection cn = new SqlConnection(cns);
            cn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            cmd.CommandText = "BulkOperations";
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            SqlParameter parameter = new SqlParameter("@Records", System.Data.SqlDbType.Xml);
            parameter.Value = records;
            cmd.Parameters.Add(parameter);
            cmd.ExecuteNonQuery();
            cn.Close();
        }

NOTE: You can also use LINQ, EntityFramework, MDAC for data access code.
Stored Procedure:

/*
DECLARE @Records XML
SET @Records =
'
	<Operations>
		<Delete>
			<Row>
				<ID>1</ID>
			</Row>
			<Row>
				<ID>2</ID>
			</Row>
		</Delete>
		<Insert>
			<Row>
				<ID>5</ID>
				<Data>Five</Data>
			</Row>
			<Row>
				<ID>6</ID>
				<Data>Six</Data>
			</Row>
		</Insert>
		<Update>
			<Row>
				<ID>3</ID>
				<Data>vrluckyin</Data>
			</Row>
		</Update>
	</Operations>
'
EXEC BulkOperations @Records
*/
ALTER PROCEDURE [dbo].[BulkOperations]
@Records XML
AS
BEGIN
	;WITH DeleteRecords AS
	(
		SELECT 
			col.value('ID[1]','INT') AS ID
		FROM  @Records.nodes('//Delete/Row') tbl(col)
	)
	DELETE FROM Q_26475408
	WHERE ID IN (SELECT ID FROM DeleteRecords)
	
	;WITH InsertRecords AS
	(
		SELECT 
			col.value('ID[1]','INT') AS ID
			,col.value('Data[1]','VARCHAR(50)') AS Data
		FROM  @Records.nodes('//Insert/Row') tbl(col)
	)
	INSERT INTO Q_26475408
	(
		ID
		,Data
	)
	SELECT ID,Data FROM InsertRecords
	
	;WITH UpdateRecords AS
	(
		SELECT 
				col.value('ID[1]','INT') AS ID
				,col.value('Data[1]','VARCHAR(50)') AS Data
		FROM  @Records.nodes('//Update/Row') tbl(col)
	)
	UPDATE a
	SET a.Data = b.Data
	FROM Q_26475408 a
	INNER JOIN UpdateRecords b ON a.ID = b.ID 
	 
END

Open in new window

0
 
LVL 5

Assisted Solution

by:Vipul Patel
Vipul Patel earned 500 total points
ID: 33685199
Sorry ;) , I forgot to attach code for SQL table Q_26475408
CREATE TABLE [dbo].[Q_26475408](
	[ID] [int] NULL,
	[Data] [varchar](50) NULL
)

Open in new window

0
 

Author Comment

by:adimit
ID: 33695902
Every thing works fine within a stored procedure.

But I get an arithmentic overflow when inserting the following value into a datetime column.

<DTime>15/09/2000 10:00:00</DTime>

I get a similiar conversion error if I try the following:

<DTime>#15/09/2000 10:00:00#</DTime>

How do i store a date and time stamp.

see sample SP below:
ALTER PROCEDURE [dbo].[usp_InsertBatchLogFromPLC]
@Records XML
AS
BEGIN
      
      ;WITH InsertRecords AS
      (
            SELECT
                  col.value('UserName[1]','VARCHAR(50)') AS UserName
                  ,col.value('Phase[1]','VARCHAR(20)') AS Phase
                  ,col.value('DTime[1]','datetime') AS DTime
                  ,col.value('PT01A[1]','numeric(3,2)') AS PT01A
                  ,col.value('RTD01A[1]','numeric(3,2)') AS RTD01A
                  ,col.value('LOAD1[1]','numeric(3,2)') AS LOAD1
                  ,col.value('LOAD2[1]','numeric(3,2)') AS LOAD2
                  ,col.value('LOAD3[1]','numeric(3,2)') AS LOAD3
                  ,col.value('LOAD4[1]','numeric(3,2)') AS LOAD4
                  ,col.value('LOAD5[1]','numeric(3,2)') AS LOAD5
                  ,col.value('LOAD6[1]','numeric(3,2)') AS LOAD6
                  ,col.value('FoMin[1]','numeric(3,2)') AS FoMin
                  ,col.value('FoMax[1]','numeric(3,2)') AS FoMax
                  ,col.value('PhaseTime[1]','numeric(3,2)') AS PhaseTime
            FROM  @Records.nodes('//Logs/Row') tbl(col)
      )
      
      INSERT INTO Batch1Log
      (
            UserName,Phase,DTime,PT01A,RTD01A,LOAD1,LOAD2,LOAD3,LOAD4,LOAD5,LOAD6,
            FoMin,FoMax,PhaseTime
      )
      SELECT UserName,Phase,DTime,PT01A,RTD01A,LOAD1,LOAD2,LOAD3,LOAD4,LOAD5,LOAD6,
            FoMin,FoMax,PhaseTime FROM InsertRecords
      
END

GO

DECLARE @Records1 XML
SET @Records1 =
'
      <Operations>
            <Logs>
            <Row>
                        <UserName>Ant</UserName>
                        <Phase>PURGE</Phase>
                        <DTime>15/09/2000 10:00:00</DTime>
                        <PT01A>11.23</PT01A>
                        <RTD01A>100.55</RTD01A>
                        <LOAD1>12.56</LOAD1>
                        <LOAD2>156.98</LOAD2>
                        <LOAD3>900.23</LOAD3>
                        <LOAD4>45.34</LOAD4>
                        <LOAD5>90.14</LOAD5>
                        <LOAD6>345.67</LOAD6>
                        <FoMin>90</FoMin>
                        <FoMax>54</FoMax>
                        <PhaseTime>1.8</PhaseTime>
            </Row>
            <Row>
                        <UserName>Ant</UserName>
                        <Phase>PURGE</Phase>
                        <DTime>#15/09/2000 10:01:00#</DTime>
                        <PT01A>11.23</PT01A>
                        <RTD01A>100.55</RTD01A>
                        <LOAD1>12.56</LOAD1>
                        <LOAD2>156.98</LOAD2>
                        <LOAD3>900.23</LOAD3>
                        <LOAD4>45.34</LOAD4>
                        <LOAD5>90.14</LOAD5>
                        <LOAD6>345.67</LOAD6>
                        <FoMin>90</FoMin>
                        <FoMax>54</FoMax>
                        <PhaseTime>2.8</PhaseTime>
            </Row>
      </Logs>
            
      </Operations>
'


EXEC usp_InsertBatchLogFromPLC @Records1
0
 
LVL 5

Assisted Solution

by:Vipul Patel
Vipul Patel earned 500 total points
ID: 33698564
The reason is that you are using DD/MM/YYYY date format.

NOTE: 103 indicates DD/MM/YYYY date format.

Your query should be look like;


 
;WITH InsertRecords AS
      (
            SELECT 
                  col.value('UserName[1]','VARCHAR(50)') AS UserName
                  ,col.value('Phase[1]','VARCHAR(20)') AS Phase
                  ,CONVERT(DATETIME,col.value('DTime[1]','VARCHAR(50)'),103) AS DTime
                  ,col.value('PT01A[1]','numeric(3,2)') AS PT01A
                  ,col.value('RTD01A[1]','numeric(3,2)') AS RTD01A
                  ,col.value('LOAD1[1]','numeric(3,2)') AS LOAD1
                  ,col.value('LOAD2[1]','numeric(3,2)') AS LOAD2
                  ,col.value('LOAD3[1]','numeric(3,2)') AS LOAD3
                  ,col.value('LOAD4[1]','numeric(3,2)') AS LOAD4
                  ,col.value('LOAD5[1]','numeric(3,2)') AS LOAD5
                  ,col.value('LOAD6[1]','numeric(3,2)') AS LOAD6
                  ,col.value('FoMin[1]','numeric(3,2)') AS FoMin
                  ,col.value('FoMax[1]','numeric(3,2)') AS FoMax
                  ,col.value('PhaseTime[1]','numeric(3,2)') AS PhaseTime
            FROM  @Records.nodes('//Logs/Row') tbl(col)
      )

Open in new window

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
get row value in vb.net 4 17
VB.Net Tranfer data between to VB.Net apps 8 38
Groupbox Control ? 2 17
Access Schema 6 23
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this article I will describe the Copy Database Wizard 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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

792 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