473,597 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Issue with inserting record in db table.

41 New Member
Hi
i have one database table Table1.which contains almost 20000000 recordes.
record to this table are inserted through storedprocedure . storedprocedure takes parameter as
"value",
Beginningdate,
Endate .

which will insert one record for each day between Beginning date and EndDate.
Before inserting record i check is record exsist for date,if exsist i will update value otherwise insert new record.

so in there is 3000 days diff between Beginning date And EndDate its taking so much time so plz help me.

Here is code of sp

BEGIN

WHILE @BeginningDate <= @EndDate

BEGIN --- Start While LOOP

IF NOT EXISTS
(
SELECT * FROM Table1 WITH (NOLOCK)
WHERE BeginningDate = @BeginningDate

)

BEGIN --- Start Insert Sql Querry

INSERT INTO Table1
(
Val,
BeginningDate,
EndDate

)
Values
(
@Val,
@BeginningDate,
@BeginningDate

)

END --- Endof Insert Sql Querry

ELSE

BEGIN --- Start Update Sql Querry

UPDATE Table1 WITH (ROWLOCK)

SET

Val=@Val

WHERE

BeginningDate = @BeginningDate

END --- Endof Update Sql Querry

SET @BeginningDate = @BeginningDate + 1;

END --- Endof While LOOP
END
Jun 16 '08 #1
2 1799
deepuv04
227 Recognized Expert New Member
Hi,
what i think is instead of looping,
1. take all the dates between beginning date and end date into temporary table.
2. Update the Table1 with value where the dates in Table1 and Temporary table
matches.
3. Delete the common dates from Temporary table.( Now you have new dates in
the temporary table)
4. Insert all the new records into Table1.

and the code is like:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. DECLARE @Table TABLE
  4. ( ID INT IDENTITY(1,1),
  5.   Date datetime
  6. )
  7.  
  8. Declare @startdate datetime 
  9. Declare @enddate datetime
  10.  
  11.  
  12. Set @startdate = '2008-06-01' 
  13. Set @enddate = '2008-06-16' 
  14.  
  15. INSERT INTO @Table(Date)
  16. Select dateadd(dd,number,@startdate) 
  17. from master.dbo.spt_values 
  18. where master.dbo.spt_values.type='p' 
  19.       AND dateadd(dd,number,@startdate)<=(@enddate)
  20.  
  21. -- Update all existing records
  22. UPDATE Table1 WITH (ROWLOCK)
  23. SET Val=@Val
  24. FROM Table1 INNER JOIN
  25.       @Table as T on Table1.BeginningDate = T.Date
  26.  
  27. -- Remove all existing records
  28. DELETE FROM @Table 
  29. FROM @Table as T INNER JOIN
  30.      Table1 ON Table1.BeginningDate = T.Date
  31.  
  32. -- Insert new records
  33. INSERT INTO Table1(Val,BeginningDate,EndDate)
  34. SELECT @Val,Date,Date
  35. FROM @Table as T
  36.  
  37.  
Hope this will help you.

Thanks
Jun 16 '08 #2
Jerry Winston
145 Recognized Expert New Member
Edit:^^^^ I like deepuv04's solution.

hmmm. sounds like a very interesting problem. A trigger might help displace some off load some of the comparisons you perform in your stored procedure. However, i cannot be sure what kind of performance benefits you will get considering by design you wish to compare all date values in a given BeginDate EndDate range.

if storedprocedure is the only way you use to add data to Table1. you could write an INSTEAD OF trigger for INSERT on Table1 that handles the decision making of whether to insert or update a row. that way your original stored procedure will only be responsible for INSERTS to TABLE1.


Expand|Select|Wrap|Line Numbers
  1. CREATE TRIGGER InsteadTrigger on TABLE1
  2. INSTEAD OF INSERT
  3. AS
  4. BEGIN
  5. DECLARE @BD  Varchar
  6. DECLARE @Val Varchar
  7. SET @BD = inserted.BeginningDate
  8. SET @Val = inserted.Val
  9.   if((SELECT Count(ID) FROM TABLE1 WHERE TABLE1.BeginningDate = @BD) > 0)
  10.       BEGIN
  11.           UPDATE Table1 WITH (ROWLOCK)
  12.           SET
  13.           Val=@Val
  14.           WHERE
  15.           BeginningDate = @BD
  16.       END
  17.   ELSE
  18.           BEGIN
  19.               INSERT INTO TABLE1
  20.               (
  21.               Val,
  22.               BeginningDate,
  23.               EndDate
  24.               )
  25.               VALUES
  26.               (
  27.               @Val,
  28.               @BD,
  29.               @BD
  30.               )
  31.           END
  32. END
  33. GO
Jun 16 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3086
by: Darius | last post by:
I was having an issue inserting data into a table in an I-Series DB2 database. The Insert statement itself is very simple: Insert into Table1 select * from Table2 These two tables have identical structures. What happens is that the process gets locked and one cannot kill it regardless of what one does. The only solution seems to be to IPL the
7
3938
by: peter.morin | last post by:
Issue: I am inserting an Oracle record containing insert trigger via Access 2002 using the code below. The issue is that the sequence from the acSaveRecord is not reflected after the insert so the insert into the second table section: ' Copy LOB entries now. fails on unique index constraint (understandable because it has the orig rate_id). The strange thing is that it works fine for new records that have beend added and then...
0
1871
by: hardieca | last post by:
Hi, My formview or gridview control stops updating or deleting a record once the record has a null value. I have table tblTest with the following pkID int NOT NULL **IDENTITY COLUMN** string1 varchar(30) string2 varchar(30)
3
1297
by: =?Utf-8?B?SmltSGVhdmV5?= | last post by:
I have a program which is looping through a data reader, taking the record which is read, creating an instance of a class which respresents that record, and then inserting that record into a database. As I am looking at the task manager in the performance tab, the PF Usage continues to rise until the machine halts with a unavailable memory error. The procedures which insert the record are all static methods. The only substative thing...
5
2019
by: bob44 | last post by:
Hi, I recently created a mysql database using phpmyadmin. I then proceeded to make a form to insert data into the database, but the problem is that the form is only able to insert one record, and then if I try inserting another record, the new one is not seen in the database. There is no error messages seen in the form when entering a new record. Heres the code for the form <html> <head> </head> <center> <form method="post"...
5
5694
by: dos360 | last post by:
Hello, I have two tables, one is a list of activities, the other a list of participants. I want to insert one record in the activities table and then using its identity column as foreign key, I want to insert two or more records into the participants table. My problem is that I have no idea what foreign key to use when inserting names into the participants table. How can I get hold of the row's key or identity column, immediately...
2
3070
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography section: Discography --------------------- DiscID
3
12424
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ====================== Message: Specified cast is not valid. Type: System.InvalidCastException Source: System.Data.Linq TargetSite: Boolean TryCreateKeyFromValues(System.Object, V ByRef)
5
2159
by: rando1000 | last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record. So here's my method. I created two tables with the same structure as the table I'm inserting from. One table, Split_Temp, is the one I'll be inserting to. The other table, Split_Insert, contains the "Blank" record, which actually just has the word...
0
7979
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7894
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3893
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2409
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.