473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Issue with inserting record in db table.

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 1784
deepuv04
227 Expert 100+
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 Expert 100+
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
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...
7
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...
0
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**...
3
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...
5
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...
5
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...
2
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...
3
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: ...
5
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. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.