Inserting Data In Batch Mode In A SQL Table Using .NET
Jun 22, 2004
Hi,
I have an ASP.NET Web Service that accepts a DataSet object passed to it. This DataSet will contain a large number of records in it's table. What I want to do (if possible) is insert all records in a SQL table in a batch mode (one go). Is this doable?
Is there a clever way of reteiving the identity of an INSERT query in SQLServer?
The example below is a the first insert (into the "parent" table with an identity column) of a heirarchical multi-table update.
sqlCommand.CommandText = "INSERT INTO Orders VALUES ( Date, Customer ) ; SELECT @@IDENTITY AS ID" ; sqlCommand.ExecuteNonQuery() ;
The ExecuteNonQuery() method returns the number of rows inserted but I need the ID for the INSERT into the "child" table.
My options based on my current understanding appear to be: a) use command parameters and set ID as an output param b) use a stored procedure, use command parameters and set ID as an output param c) issue the SELECT @@IDENTITY as a subsequent seperate command using ExecuteScalar()
Is there a more elegant way to get the ID using SQLServer's "batch" query mechanism and ADO.NET v1.1? Thanks,
I am trying to insert data into two different tables. I will insert into Table 2 based on an id I get from the Select Statement from Table1. Insert Table1(Title,Description,Link,Whatever)Values(@title,@description,@link,@Whatever)Select WhateverID from Table1 Where Description = @DescriptionInsert into Table2(CategoryID,WhateverID)Values(@CategoryID,@WhateverID) This statement is not working. What should I do? Should I use a stored procedure?? I am writing in C#. Can someone please help!!
How to launch MSOLAP cube processing in batch mode? ================================================== =
When one wants to run SQL Statement or Stored procedure in batch mode, OSQL.exe can be used.
Now we have created OLAP cube and want to process(populate) cube. From MSOLAP Analysis manage we can right click a cube name then click “Process”. But how to do this in batch mode? I.e. any equivalent thing of OSQL.exe to help process cube?
I believe a matured tool should work good in both interactive and batch mode. Unfortunately I did not find how to launch cube processing from MS OLAP documents. Looks like we have to write one using DSO supplied by Microsoft. Do you know if there is any tool for us not to write one? Pls help.
I am trying to insert bulk data into main table from staging table in sql server 2012. If any error comes, this total activity is rollbacked. I don't want that to happen. I want to know the records where ever the problem persists, and the rest has to be inserted.
Hi, I have a table in which I will insert several redundant data. Don't ask why, is Integration services, it only reads data and inserts it in a SQL table. THis way, I have a SQL table with several lines repeating them selves. What I want to do is create a procedure that reads the distinct data and inserts it in another table, but my problem is that I am not able to select data line by line on the original table to save it in local variables and insert it on the another table, I just can select the last line. I've tried a while cycle but no succeed. Here is my code: create proc insertLocalizationASdeclare @idAp int, @macAp varchar(20), @floorAp varchar(2), @building varchar(30), @department varchar(30)select @idAp = idAp from OLTPLocalization where idAp not in (select idAp from dimLocalization)select @macAp=macAp,@floorAp=floorAp,@building=building,@department=department from OLTPLocalizationif (@idAp <> null)beginInsert into dimLocalization VALUES(@idAp,@macAp,@floorAp,@building,@department)endGO This only inserts the last line in the "oltpLocalization" table. O the other hand, like this:create proc aaaaasdeclare @idAp as int, @macAp as varchar(50), @floorAp as int, @building as varchar(50), @department as varchar(50)while exists (select distinct(idAp) from OLTPLocalization)begin select @idAp =idAp from OLTPLocalization where idAp not in (select idAp from dimLocalization) select @macAp = macAp from OLTPLocalization where idAp = @idAp select @building = building from OLTPLocalization where idAp = @idAp select @department = department from OLTPLocalization where idAP = @idApif (@idAp <> null)begin insert into dimLocalization values(@idAp,@macAp,@floorAp,@building,@department)endendgo this retrieves every distinct idAp in each increment on the while statement. The interess of the while is really selecting each different line in the OLTPLocalization table. I did not find any foreach or for each statement, is there any way to select distinct line by line in a sql table and save each column result in variables, to then insert them in another table? I've also thought about web service, that reads the distinct data from the oltpLocalization into a dataset, and then inserts this data into the dimLocalization table. Is there anything I can do?Any guess?Really needing a hand here!Thanks a lot!
Hi, I have two tables in a data base and i'm inserting the data from one into the other...no probs. What i was wondering is, in table1 i have an column of ID numbers. In the table2 i have a matching set of ID numbers. There are 5 PersonID numbers in table one and 10 in table two, the same 5 numbers as in table1 but each ID has a duplicate with different data in the two rows.
SAMPLE:
INSERT INTO Table1 (PersonID, level1, Level2, Level3, Level4) SELECT PersonID,level1, Level2, Level3, Level4 FROM Table2
When i insert the data into table1 it leaves the first 5 rows of data as null and then populates the table with all the data from table two. Is there anyway of preventing these first 5 columns from remaining empty....
Hi, I'm fairly new to SQL Server 2005. i have a table that creates customer id's along with other data (let's call it Customer) I would like to take the same customer_id data and import it into a different table (HQ_Customer) the new table also has different column names.
Is there a script that can be used for this problem?
Well, I think this should be an easy question, but here goes: I'm taking data from one table and inserting it into another. According to the SQL Server Mobile Book Online, the syntax goes like this: INSERT INTO Table1 (col1, col2) SELECT (col1, col2) from Table2
So while I can do this with my tables: INSERT INTO sensor_stream (sensor_stream_id) SELECT (sensor_stream_id) FROM sensor_stream_temp
If I add any more columns, I get an error. Like this: INSERT INTO sensor_stream (sensor_stream_id, sensor_stream_type_id) SELECT (sensor_stream_id, sensor_stream_type_id) FROM sensor_stream_temp The error is "There was an error parsing th equery. [ Token line number =1, Token line offset = 98, Token in error = ',' ]"
Anyone have any ideas about why I cannot do more than one column at a time? TIA, -Dana
I am try to transfer some tables data from one database server into another database server. I create a package in SSIS, and I use a variable to pass each table name. In Data flow, I use a OLEDB Source, but I cannot set the Data access mode to Table name or view name variable. Ever time, I will get this following error info "===================================
Error at Data Flow Task [OLE DB Source [31]]: A destination table name has not been provided.
(Microsoft Visual Studio)
===================================
Exception from HRESULT: 0xC0202042 (Microsoft.SqlServer.DTSPipelineWrap)
------------------------------ Program Location:
at Microsoft.SqlServer.Dts.Pipeline.Wrapper.CManagedComponentWrapperClass.ReinitializeMetaData() at Microsoft.DataTransformationServices.DataFlowUI.DataFlowComponentUI.ReinitializeMetadata() at Microsoft.DataTransformationServices.DataFlowUI.DataFlowAdapterUI.connectionPage_SaveConnectionAttributes(Object sender, ConnectionAttributesEventArgs args)".
Some one can tell me what is the reason, or give me some examples.
I can do this in an old (~2003) way, but I'm trying to figure out a new (2005) way. What I've got is an e-commerce project in which users select various products from a catalog and add them to a shopping cart. Then they go to a checkout page which displays the current contents of the shopping cart, which are contained in a manually-constructed data table (system.data.datatable). On the checkout page a GridView displays the contents of the data table. At that point they can check out via button click, which launches the following (somewhat simplified) ADO.NET code:1 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 2 'Some variables 3 Dim intCounter As Integer 'Used to count the loop 4 Dim prmQuantity As New System.Data.SqlClient.SqlParameter() 'A parameter 5 Dim prmProduct As New System.Data.SqlClient.SqlParameter() 'A parameter 6 Dim prmPrice As New System.Data.SqlClient.SqlParameter() 'A parameter 7 Dim InsertCommand As New System.Data.SqlClient.SqlCommand 'The SQL insert command 8 Dim dbConnection As New System.Data.SqlClient.SqlConnection 'The connection to the DB 9 10 'Set the parameters for the SQL statement 11 prmQuantity.ParameterName = "@Quantity" 12 prmQuantity.SqlDbType = Data.SqlDbType.Int 13 prmQuantity.Size = 18 14 prmQuantity.Direction = Data.ParameterDirection.Input 15 16 prmProduct.ParameterName = "@Product" 17 prmProduct.SqlDbType = Data.SqlDbType.VarChar 18 prmProduct.Size = 50 19 prmProduct.Direction = Data.ParameterDirection.Input 20 21 prmPrice.ParameterName = "@Price" 22 prmPrice.SqlDbType = Data.SqlDbType.Int 23 prmPrice.Size = 18 24 prmPrice.Direction = Data.ParameterDirection.Input 25 26 'Create the connection to the database using web.config 27 dbConnection.ConnectionString = ConfigurationManager.ConnectionStrings("MyDB").ConnectionString 28 29 'Various command settings 30 InsertCommand.CommandText = "INSERT INTO [Orders] ([Quantity], [Product], [OrderDate], [ItemPrice]) VALUES (@Quantity, @Product, {fn NOW()}, @Price)" 31 InsertCommand.CommandType = Data.CommandType.Text 32 InsertCommand.Connection = dbConnection 33 34 'Add the rows to the database 35 For intCounter = 0 To objDT.Rows.Count - 1 36 37 'Re-inserts the data rows from objDT 38 objDR = objDT.Rows(intCounter) 39 40 'Set param values 41 prmQuantity.Value = objDR("Quantity") 42 prmProduct.Value = objDR("Product") 43 prmPrice.Value = objDR("Price") 44 45 'Add params to insert command 46 InsertCommand.Parameters.Add(prmQuantity) 47 InsertCommand.Parameters.Add(prmProduct) 48 InsertCommand.Parameters.Add(prmPrice) 49 50 'Open connection 51 InsertCommand.Connection.Open() 52 53 'Execute the insert command 54 InsertCommand.ExecuteNonQuery() 55 56 'Close connection and clear parameters for the next loop 57 InsertCommand.Connection.Close() 58 InsertCommand.Parameters.Clear() 59 60 Next 61 62 Response.Redirect("done.aspx") 63 64 End Sub 65 What I'd like to do is see if I can instead use a simplified approach based on 2.0 controls. Specifically, I was hoping that I could use an SqlDataSource and simply tap into its built-in Insert capabilities. So I tried this alternate procedure:Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)Dim intCounter As Integer 'Used to count the loopFor intCounter = 0 To objDT.Rows.Count - 1DSOrdersNew.InsertParameters("Product") = objDR("Product")DSOrdersNew.InsertParameters("Quantity") = objDR("Quantity")DSOrdersNew.Insert()NextEnd SubWhen I run this I get an error message saying: "Unable to cast object of type 'System.String' to type 'System.Web.UI.WebControls.Parameter'."Am I just way out in left field here? I guess I don't mind doing it the old-fashioned way, but it seems like there ought to be a way to do this. Any suggestions would be appreciated. Thanks!
Hello all.... I am trying to submit data from a form(textbox) to a sql table. but I am getting an error message "NullReferenceException was unhandled by user code" Can any help me with this? This is my code inProtected Sub btnSubmit_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click Dim cnstr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ToString()Dim pa1 As Data.SqlClient.SqlParameter = New Data.SqlClient.SqlParameter("Keyword", Data.SqlDbType.VarChar, 50, Data.ParameterDirection.Input) pa1.Value = Keyword.Text SqlHelper.ExecuteNonQuery(cnstr, Data.CommandType.StoredProcedure, "spNewRec", pa1)
It appears that when I insert data into a varchar(8000) field, SQL Server truncates everything after the 256th byte. When I change the field to text, this problem is eliminated. Can someone give me an explanation of why? And can I actually to the insert with the field being a varchar(8000) instead of a text data type. This will do wonders for the size and indexing.
I have to import data into a empty database, that has many tables.some tables have to be inserted first than others due to the foreignkeys.How do I find out the order of the tables that I have to insert datainto?Thanks in advance!Sam
hi I want to read data from XML file and insert that data from XML file into the Database Table From ASP.NET page.plz give me the code to do this using DataAdapter.Update(ds)
I have question on lock on table in SQL Server while inserting data using multiple processes at a single time into same table.Here are my questions on this,
1) Is it default behavior of SQL server to lock table while doing insert? 2) if yes to Q1, then how we can implicitly mention while inserting data. 3) If I have 4 tables and one table is having foreign keys from rest of the 3 tables, on this scenario do I need to use the table lock explicitly or without that I can insert records into those tables?
I'm inserting data from a c# webservice into a table via a stored procedure, but I get a Column does not allow nulls on the @alert_id column/field. It is set as int and allow nulls is not ticked.
Here's the sql:
USE [aren] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [aren1002].[ArenAlertInsert]
I have 2 tables: Source Table - IncidentDimNew and Destination Table - IncidentDimNew with identical columns: Id and CreatedDate but the data is different.
I would like to insert distinct data from those 2 columns of 1st table into the same 2 columns into the 2nd table but I onlt want to replace the values in the 2nd table where the Created Date is > 2015-04
I compiled the code in order to get the information I want from 2 tables:
Source Table SELECT COUNT(Id), LEFT(CONVERT(VARCHAR(10)), CreatedDate, 126), 7) FROM IncidentDimNew
[Code] ...
This is the code I wrote in order to do it             INSERT INTO IncidentDim [Id]    ,[CreatedDate]    SELECT [Id] ,[CreatedDate]
FROM IncidentDimNew
where left(CONVERT(VARCHAR(10), CreatedDate, 126),7) > '2015-04'
GO
But what it does it's gives the SUM of the values in the corresponding rows where the values are not null which I don't want.
So  how do I leave the values as is from table IncidentDim and insert new values as it is from IncidentDimNew where the Created Date is > 2015-04?
I'd like to select data out of an oracle table, and UPDATE a sybase table. So far I've got as far as the following:
1. create a package 2. add 2 connection managers, set retainsameconnection=true on both just to be sure. 3. add an executesql command which creates a temporary table
i) an oledb source with a select statement: "select instru_id from mytable" ii) an oledb destination with open rowset: #temp_wm added an external column called instru_id in the inputs and outputs tab
when i run, the create temp table task work, the select works, but the insert into fails. If I change the select statement to:
"select instru_id from mytable where 1=0" it all executes fine.
So everythings copacetic as long as i don't need to actually insert any real records = brilliant!
1. HAS ANYONE OUTTHERE SUCCESSULLY USED SSIS TO INSERT DATA INTO A SYBASE TEMPORARY TALBE - MAYBE ITS JUST NOT POSSIBLE?? 2. Any idea how I can fix my setup?
I'm usign Sybase ASE OLE DB Drivers
Note: i also tried ## temp tables, no difference.
This is the error output.
[OLE DB Destination [255]] Error: An OLE DB error has occurred. Error code: 0x80004005. [OLE DB Destination [255]] Error: The "input "OLE DB Destination Input" (268)" failed because error code 0xC020907B occurred, and the error row disposition on "input "OLE DB Destination Input" (268)" specifies failure on error. An error occurred on the specified object of the specified component. [DTS.Pipeline] Error: The ProcessInput method on component "OLE DB Destination" (255) failed with error code 0xC0209029. The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running.
I have 3 tables (accnt, jobcost, and servic15). all with the same fields (code, jno, ven, date). I need to insert the data from these tables into another table called dummy with the same fields, in one statement or query.
i am writing a sproc that calls another sproc. this 2nd sproc returns 3 or 4 rows. i want to be able to insert these rows into a table. this sproc is inside a cursor, as i have to call it many times.
how do i insert the rows that it returns into another table??
I have an application taht requires the use of a table. The device that this application works on, has a local memory that does not allow me to insert the 800,000 records that I need. Therefore I have two approaches:
1. To insert less records into my local memory database e.g 40,000 but not row by row, bulk insert is better. How do I do the bulk insert?
2. This is the most prefferable way: To find a way to insert all 800,000 records into a table on the storage card which is 1GB. What do you suggest? Will using threads be helpfull? Any ideas?
I use C# from VS 2005, SQL ME, compact framework 2.0 and windows 4.2.
the error message I get is {"Object reference not set to an instance of an object."} and it points to < Tickr As String = CType(FindControl("TickerTextbx"), TextBox).Text > this is my code": Protected Sub TickMastBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TickMastBtn.Click REM Collect variablesDim Tickr As String = CType(FindControl("TickerTextbx"), TextBox).Text Dim Comp As String = CType(FindControl("CoTextbx"), TextBox).TextDim Exch As String = CType(FindControl("ExchTextbx"), TextBox).Text REM Create connection and command objectsDim cn As New SqlConnection("Data Source=.SQLEXPRESS;AttachDbFilename=C:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLDataVTRADE.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")Dim cmd As New SqlCommand cmd.Connection = cn REM Build our parameterized insert statementDim sql As New StringBuilder sql.Append("INSERT INTO TickerMaster ")sql.Append("(Ticker,Company,Exchange,) ")sql.Append("VALUES (@Tickr,@Comp,@Exch,)") cmd.CommandText = sql.ToString REM Add parameter values to command REM Parameters used to protect DB from SQL injection attacksWith cmd.Parameters .Add("Tickr", SqlDbType.Int).Value = Tickr.Add("Comp", SqlDbType.VarChar).Value = Comp .Add("Exch", SqlDbType.VarChar).Value = Exch End With REM Now execute the statement cn.Open() cmd.ExecuteNonQuery() cn.Close() End Sub
Do we have any way to insert,update,delete data from one table and update the changes onto second table. Also, while updating records into second table, can the data be encrypted.
I tried using view and it can insert, update, delete without any issues. But if i tried to encrypt any fields after inserting data into view, I am unable to do it.
CREATE Tableb_vw ON TableB Instead of Insert AS Begin update TableA set Lname = ( --UserName = 'User' + substring(convert(varchar(32), UsersTrID), 1, 8) SELECT REPLACE(LEFT(Lname, 2), '''', 'Z') AS LNAME) FROM TableA end
What I would like to get:
1. Can we update base tables and encrypt second table data while inserting or updating data 2. If not supported using base tables, can we do using views to encrypt view data [Some fields]
I have a data load process that reads data from flat file into a Stage table in sql server. The order of the records in the stage table is exactly same as the order in the flat file. The identity column on the Stage table (which is also the clustered index) represents the exact line/row number of the data in the filat file. I perform some transformations on the data in the stage table and then insert it into a cumulative table which has a clustered index on an identity column again. When I do this, does the order of the data in the cumulative table be in the same order as the data in the stage table? Anyone, please let me know if I can rely on SQL server to maintain the same order or I will be forcing a sort order on the Identity column (clustered index) of the stage table when I insert the data into a cumulative table.
I have a pb when i transfer data from a table named INCIDENT to a table GI_INCIDENTS.... In the table INCIDENT i have startdate,enddate,starttime,endtime And in the table GI_INCIDENTS i have startdate and enddate -->format yyyy/MM/dd hh:mm:ss.ttt.. INCIDENT is a migrated table from access...Then with a query i transfered datas to GI_INCIDENTS... The pb is in INCIDENT Table, date of beginning incident is (2003/06/18 ) but when i execute my insert query,in the table GI_INCIDENTS, date of beginning incident is (2003/06/06)... So i have 2 days delay in the all colums... INCIDENT-->enddate (2006/11/30) GI_INCIDENT-->enddate(2006/11/28) I don't understand the fact...
The query:
Insert into GI_INCIDENTS(GIIN_ID,GIIN_STA_INCIDENT,STARTDATE,ENDDATE,GIIN_TYPE,GIIN_RESUME,GIIN_DESCRIPTION,GIIN_IMPACT_ANTENNE,GIIN_INITIATEUR) select NUMINCIDENT,CODETAT,STARTDATE+ ' ' + STARTTIME, ENDDATE + ' ' + ENDTIME,CODETYPE,NATURE,DESINCIDENT,CODBLOQ,NUMEXPL From INCIDENT set IDENTITY_INSERT GI_INCIDENTS off