Update rows in Database using XML data - c#

This is the first time I am using XML to insert data into a table.I am saving the data from the front end(all the Datagridview rows) into an xml file and sending it to database to insert into table SD_ShippingDetails.Below is the Query for reading the XML data and saving data.As you can see from the Query I am deleting the related ShippingID details and inserting again.(DELETE FROM SD_ShippingDetails WHERE ShippingID=#ShippingID).Can we update already existing rows in the SD_ShippingDetails by getting the data from XML.If Yes,Please help me with the query.
CREATE PROCEDURE SD_Insert_ShippingDetails
#PBMXML as varchar(Max),
#ShippingID as INT
AS
BEGIn
declare #i int
exec sp_xml_preparedocument #i output,#PBMXML
DELETE FROM SD_ShippingDetails WHERE ShippingID=#ShippingID
INSERT INTO SD_ShippingDetails(ShippingID,Weight,Height,TotalBoxes,Price)
SELECT ShippingID,Weight,Height,TotalBoxes,Price FROM OPENXML(#i,'Root/ShippingBox',2)
WITH (
ShippingID int,Weight varchar(20),Height varchar(20),TotalBoxes varchar(20),Price numeric(18,2))
exec sp_xml_removedocument #i
END
Thanks.

You are on SQL Server 2005 so you can use the XML datatype instead of openxml so this answer uses that instead. Using the XML datatype is not necessary for the solution. You can rewrite using openxml if you want to.
You specified in a comments that there is an ID identity field in SD_ShippingDetails (I assume that is the primary key) but you also said that the combination of ShippingID and Weight is unique. That leaves us with a table structure that looks like this.
create table dbo.SD_ShippingDetails
(
ID int identity primary key,
ShippingID int not null,
Weight varchar(20) not null,
Height varchar(20),
TotalBoxes varchar(20),
Price numeric(18,2),
unique (ShippingID, Weight)
);
The stored procedure first needs to update all rows that already exist in SD_ShippingDetails and after that it needs to insert the rows that are missing.
create procedure dbo.SD_Insert_ShippingDetails
#PBMXML as xml
as
update dbo.SD_ShippingDetails
set Height = T.N.value('(Height/text())[1]', 'varchar(20)'),
TotalBoxes = T.N.value('(TotalBoxes/text())[1]', 'varchar(20)'),
Price = T.N.value('(Price/text())[1]', 'numeric(18,2)')
from #PBMXML.nodes('Root/ShippingBox') as T(N)
where ShippingID = T.N.value('(ShippingID/text())[1]', 'int') and
Weight = T.N.value('(Weight/text())[1]', 'varchar(20)');
insert into dbo.SD_ShippingDetails(ShippingID, Weight, Height, TotalBoxes, Price)
select T.N.value('(ShippingID/text())[1]', 'int'),
T.N.value('(Weight/text())[1]', 'varchar(20)'),
T.N.value('(Height/text())[1]', 'varchar(20)'),
T.N.value('(TotalBoxes/text())[1]', 'varchar(20)'),
T.N.value('(Price/text())[1]', 'numeric(18,2)')
from #PBMXML.nodes('Root/ShippingBox') as T(N)
where not exists (
select *
from dbo.SD_ShippingDetails
where ShippingID = T.N.value('(ShippingID/text())[1]', 'int') and
Weight = T.N.value('(Weight/text())[1]', 'varchar(20)')
);
SQL Fiddle

If you have Sql Server 2005, then placing the values in #temp or #variables tables is best.
With 2008 and up, you could piggy back on the MERGE functionality.
http://msdn.microsoft.com/en-us/library/bb522522(v=sql.105).aspx
Here is a good link for xml shredding. Note, you are using the older version of OPENXML. That was a more Sql Server 2000 command. Check Plamen's blog below for 2005 and above syntax.
http://pratchev.blogspot.com/2007/06/shredding-xml-in-sql-server-2005.html

I would populate your XML into a variable table and then use an Update Statement and an Insert with a Not Exists.
If you had SQL 2008 you could replace your delete and insert statements with this...
MERGE SD_ShippingDetails AS Target
USING (SELECT ShippingID,
Weight,
Height,
TotalBoxes,
Price
FROM OPENXML(#i,'Root/ShippingBox',2)
WITH (ShippingID int,
Weight varchar(20),
Height varchar(20),
TotalBoxes varchar(20),
Price numeric(18,2)) ) AS source (ShippingID,Weight,Height,TotalBoxes,Price)
ON (target.ShippingID = source.ShippingID)
WHEN MATCHED THEN
UPDATE SET Weight = source.Weight,
Height = source.Height,
TotalBoxes = source.TotalBoxes,
Price = source.Price
WHEN NOT MATCHED THEN
INSERT (ShippingID,Weight,Height,TotalBoxes,Price)
VALUES (source.ShippingID,source.Weight,source.Height,source.TotalBoxes,source.Price);

Related

How to get auto generated Id before insertion?

I have a table like the below picture, and I want to get IDwithChar column value concatenating the value with image address, and inserting that into ImageCover column at same query, how can I achieve that ?
Try to concate the two column values and Use UPDATE statement to update the value in the related column.
CREATE TABLE Test (
IdWithChar NVARCHAR(MAX),
ImageAddress NVARCHAR(MAX),
CoverImage NVARCHAR(MAX)
)
INSERT INTO Test VALUES ('B00001','Test Address','Test-Image-Url')
Query:
UPDATE TEST
SET CoverImage= IdWithChar +' '+ ImageAddress
WHERE Your_condition
first you must insert values and get the last identity and then update the field in the record.
insert into item values ('test', 'test')
declare #itemId int = ##IDENTITY
update item set item_code = #itemId where item_id = #itemId

Is there any way to return IDs of objects inserted with TVP?

I am doing an insert using tvp of large amount of products. After insert I need IDs of these products. Is there any safe way to get them straight away after stored procedure insert?
I assume you want to return inserted IDs. You can use OUTPUT clause and magic table inserted to return what you need.
CREATE TYPE product_type AS TABLE(name VARCHAR(100), barcode INT);
CREATE TABLE products(ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
name VARCHAR(100), barcode INT);
CREATE PROCEDURE dbo.my_insert_product
#products product_type READONLY
AS
BEGIN
INSERT INTO products(name, barcode)
OUTPUT inserted.ID, inserted.name -- add columns you need
SELECT name, barcode
FROM #products;
END;
Calling:
DECLARE #t AS product_type;
INSERT INTO #t
VALUES ('Product1', 1), ('Product2', 2), ('Product3', 2),
('Product4', 3), ('Product5', 4), ('Product6', 5);
EXEC dbo.my_insert_product #t;
SqlFiddleDemo
With MERGE you can output values from the TVP which (without any good reason) is not possible with INSERT. You could add a unique key to the TVP that allows your app to understand what rows received what ID.

stored procedure calling from c# and iteration issue in Merge

I have over a million records in the list. I pass all records at once from table to stored procedure .In stored procedure i have to have iteration to go thorugh all the rows in the table and for each row it takes table row modified date based on jobid and checks if it exist in database and based on it either it updates or insert the record. I feel that my procedure is not correct, would be glad if someone help on this.
foreach (No_kemi no_list in newforSQL)
{
DataTable _dt = new DataTable("table");
_dt.Columns.Add("JobID", typeof(string));
_dt.Columns.Add("CreatedDate", typeof(datetime));
_dt.Columns.Add("ModifiedDate", typeof(datetime));
_dt.Columns.Add("DbDate", typeof(datetime));
_dt.Columns.Add("SubGUID", typeof(string));
_dt.Columns.Add("eType", typeof(string));
// adding over a million records in the table
_dt.Rows.Add(no_list.ID,no_list.CreatedDate,no_list.ModifiedDate,no_list.DbDate,no_list.SubGUID,no_list.eType);
}
using (SqlCommand sqlCommand = new SqlCommand())
{
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.CommandText = "Process_NO_table";
sqlCommand.Connection = connection;
SqlParameter typeParam = sqlCmd.Parameters.AddWithValue("#track", _dt);
typeParam .SqlDbType = SqlDbType.Structured;
sqlCmd.ExecuteNonQuery();
}
my tabletype and procedure:
CREATE TYPE TrackType AS TABLE
(
t_Id uniqueidentifier, t_JobID nvarchar(50), t_CreatedDate datetime2(7), t_ModifiedDate datetime2(7), t_DbDate datetime2(7)
t_SubGUID nvarchar(MAX), t_eType nvarchar(MAX)
);
GO
ALTER/CREATE PROCEDURE [dbo].[Process_NO_table] // i will change to alter after i create it
#track TrackType READONLY
AS
// i need to iterate all the rows of the table(over a million)
Declare #rows INT
Declare #i int = 0
Declare #count int = (SELECT COUNT(*) FROM #track)
DECLARE #is INT
WHILE (#i < #count)
BEGIN
-- first i check modified date from the database table
SELECT #is = COUNT(*) FROM NO_table WHERE [JobID] IN (SELECT [t_JobID] FROM #track)
MERGE [dbo].[NO_table] AS [Target]
USING #track AS [Source]
-- if the database modifed date is less than the modifeid date from the proceduretable(#track) then it updates the records
ON [Target].[ModifiedDate] < [Source].[t_ModifiedDate] AND JobID = t_JobID
WHEN MATCHED THEN
UPDATE SET [JobID] = [Source].[t_JobID],
[CreatedDate] = [Source].[t_CreatedDate]
[DbDate]= [Source].[t_DbDate]
[ModifiedDate] = [Source].[t_ModifiedDate]
[SubGUID] = [Source].[t_SubGUID]
[eType] = [Source].[t_eType]
-- if the database modifed dateis not existing then it insert the record
MERGE [dbo].[NO_table] AS [Target]
USING #track AS [Source]
ON (#is != 0)
WHEN NOT MATCHED THEN
INSERT INTO [NO_table] ( [JobID], [CreatedDate], [ModifiedDate], [DbDate], [SubGUID], [eType] )
VALUES ( [Source].[t_JobID], [Source].[t_CreatedDate], [Source].[t_ModifiedDate], [Source].[t_DbDate], [Source].[t_SubGUID], [Source].[t_eType] );
SET #i = #i + 1
END
GO
I think you have a large number of syntax errors in your SQL (assuming MS SQL), but your merge condition is probably giving you the invalid syntax near WHERE, because you need to use AND, not WHERE.
ON [Target].[ModifiedDate] < [Source].[t_ModifiedDate] WHERE JobID = t_JobID
should be
ON [Target].[ModifiedDate] < [Source].[t_ModifiedDate] AND JobID = t_JobID
The Select Top 1 and the WHEN MATCHED THEN after the null check for #dbmoddate need to go away as well, as those are also causing syntax issues.
The insert after the null check for #dbmoddate needs a table specified so it actually knows what to insert into.
You also need to end your merge statement with a semicolon.
UPDATED ANSWER:
Now that you have this more cleaned up, I can better see what you're trying to do. At a high level, you want to simply update existing records where the modified date is less than the modified date of on your custom type. If there does not exist a record in your table that does exist in your custom type, then insert it.
With that said, you don't actually need to loop because you aren't doing anything with your loop. What you currently have and what I'm posting below this is all set-based results, not iterative.
You can make this much simpler by getting rid of the merge statements and doing a simple Update and Insert like I have below. The merge would make more sense if your condition between the two statements was the same (i.e. if you didn't have the check for modified date, then merge would be OK) because then you can use the keywords WHEN MATCHED and WHEN NOT MATCHED and have it in one single merge statement. I personally stay away from MERGE statements because they tend to be a little buggy and there are a number of things you have to watch out for.
I think this solution will be better in the long run as it is easier to read and more maintainable...
CREATE TYPE TrackType AS TABLE
(
t_Id uniqueidentifier, t_JobID nvarchar(50), t_CreatedDate datetime2(7), t_ModifiedDate datetime2(7), t_DbDate datetime2(7)
,t_SubGUID nvarchar(MAX), t_eType nvarchar(MAX)
);
GO
CREATE PROCEDURE [dbo].[Process_NO_table] -- i will change to alter after i create it
#track TrackType READONLY
AS
-- i need to iterate all the rows of the table(over a million)
Update [NO_table]
SET [JobID] = T.[t_JobID],
[CreatedDate] = T.[t_CreatedDate],
[DbDate]= T.[t_DbDate],
[ModifiedDate] = T.[t_ModifiedDate],
[SubGUID] = T.[t_SubGUID] ,
[eType] = T.[t_eType]
From #track T
Where [NO_table].[JobID] = T.[t_JobID]
And [NO_table].[ModifiedDate] < T.[t_ModifiedDate]
Insert [NO_Table]
(
[JobID],
[CreatedDate],
[ModifiedDate],
[DbDate],
[SubGUID],
[eType]
)
Select T.[t_JobID],
T.[t_CreatedDate],
T.[t_ModifiedDate],
T.[t_DbDate],
T.[t_SubGUID],
T.[t_eType]
From #track T
Where Not Exists (Select 1 From [NO_table] where T.[t_JobID] = [NO_table].[JobID])
GO

Returning data which is not found in where clause and didn't update in sql

In a c# desktop application I am getting this list of data which I am reading by barcode into text file; here is the result;
R900, 27674T07, 27438T17, 27736T21, 26609T08,
R901, 27770T12, 27833T07, 26402T12, 27771T09, 26003T13,
R902, 26003T14, 26402T11, 26246T17,
R904, 28055T09, 25356T08, 25825T07, 25556T09,
and I am transforming it to update queries;
UPDATE TABLE SET NUMBER = R900 WHERE id in ( 27674T07, 27438T17, 27736T21, 26609T08)
UPDATE TABLE SET NUMBER = R901 WHERE id in ( 27770T12, 27833T07, **26402T12**, **27771T09**, 26003T13)
UPDATE TABLE SET NUMBER = R902 WHERE id in ( 26003T14, **26402T11**, 26246T17)
UPDATE TABLE SET NUMBER = R904 WHERE id in ( 28055T09, 25356T08, 25825T07, **25556T09**)
Finally I am executing this SQL query. But the problem is I don't know which id is not found in IN clause in database. I need to report back to user which id didn't found with its NUMBER
For example the bold id's are not found in database, and couldn't update. So expected result is:
NUMBER id
R901 26402T12
R901 27771T09
R902 26402T11
R903 25556T09
how can I return this?
You could do something like this
declare #mytable as TABLE
(
Id nvarchar(20)
)
UPDATE TABLE SET NUMBER = R900
OUTPUT INSERTED.Id into #mytable
WHERE id in ( 27674T07, 27438T17, 27736T21, 26609T08)
Select * from #mytable
#mytable will contain updated Ids only.
Hope this helps.
create a temp table to store the splitted value into it.
then
SELECT temp.number, temp.Id
FROM #temp temp
LEFT OUTER JOIN TABLE ON temp.id = TABLE.id
WHERE TABLE.id is null

Updating data with same primary key

I am reading data from csv file and adding data in database. At time of inserting data into database I want to update data with same primary key.
e.g.) I am using two Columns Bar-codes (PK) and Quantity. So, when I insert data from csv file similar barcode quantity will get added.
Can anyone help me? I am using C#.NET and SQL.
Thanks,
Rushabh Shah.
check out the merge keyword. it should do pretty much waht you're asking for.
here's a stored proc that should do it for you.
CREATE PROCEDURE dbo.InsertBarcodeData
#Barcode varchar(255),
#Quantity int
AS
BEGIN
SET NOCOUNT ON;
MERGE myTableName AS target
USING (SELECT #Barcode, #Quantity) AS source (BarCode, Quantity)
ON (target.Barcode= source.Barcode)
WHEN MATCHED THEN
UPDATE SET Quantity = source.Quantity + target.Quantity
WHEN NOT MATCHED THEN
INSERT (BarCode, Quantity)
VALUES (source.BarCode, source.Quantity)
END;
GO
create procedure InsertOrUpdateSales
(
#bar_code nvarchar(100),
#quantity int
)
as
if exists (select * from sales where bar_code = #bar_code)
update sales set quantity = quantity + #quantity where bar_code = #bar_code
else
insert into sales ( bar_code, quantity) values ( #bar_code, #quantity )
go
And
public static void InsertOrUpdateSales(string connection, string barCode, int quantity)
{
using(SqlConnection conn = new SqlConnection(connection))
{
using(SqlCommand comm = new SqlCommand("InsertOrUpdateSales", conn))
{
comm.CommandType = CommandType.StoredProcedure;
comm.Paramters.AddWithValue("#bar_code", barCode);
comm.Paramters.AddWithValue("#quantity", quantity);
comm.ExecuteNonQuery();
}
}
}
Alternatively, if you want to use the merge statement (as #Chris Lively and #nathan gonzalez mentioned) you could get really fancy and do it like this:
BULK INSERT the data from the CSV file to an empty temp table.
MERGE the temp table with the existing table.
TRUNCATE the temp table.
This might give you the best results. (For certain values of "best".)
If you can assume that there is already an existing entry for all of the bar codes in the table you could do this with a Stored procedure with two incominig parameters (#BarCodeID and #AdditionalQuantity)
UPDATE yourTable SET Quantity = Quantity + #AdditionalQuantity WHERE BarCode = #BarCodeID
You can add a Trigger to the table. When ever something is inserted in the table, you can have it run a stored procedure.

Categories

Resources