Solved

Bulk Inserts with Stored Procedures

Posted on 2010-09-15
4
637 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now