473,499 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Update or Insert Query Help needed

2 New Member
I have a table in the following format

ID RecType PKIdentifier RecordData

1 01 acc001 Text
2 03 NULL Text
3 04 NULL Text
4 01 acc002 Text
5 02 NULL Text
6 03 NULL Text
7 01 acc003 Text
8 03 NULL Text
9 04 NULL Text
10 01 acc004 Text

etc.

I need a query that will update all the NULL PKIdentifiers with the 01 RecType that precedes it until I get to the next 01. Example Records 2 and 3 should be updated with the PKIdentifier from Record 1 and Records 5 and 6 should be updated with the PKIdentifier from record 4 and so on. Basically RecType 01 are the parent Records and types 02 -04 are the child records until the next 01 record. I have approximately 20 Million rows to update.
What is the best way to do this?
Oct 8 '08 #1
2 1550
ck9663
2,878 Recognized Expert Specialist
This will not really update your table but this should get your started. The last query can be used as subquery in an update statement or to create a temp table that you can analyze and use for your update.

Until someone has a better way, try doing this:

BACKUP YOUR TABLE FIRST


Expand|Select|Wrap|Line Numbers
  1. set nocount on
  2. declare @yourtable table (recnum int, RecType varchar(2), PKIdentifier varchar(15), RecordData varchar(10))
  3.  
  4. insert into @yourtable values (1, '01', 'acc001', 'Text')
  5. insert into @yourtable values (2, '03', NULL, 'Text')
  6. insert into @yourtable values (3, '04', NULL, 'Text')
  7. insert into @yourtable values (4, '01', 'acc002', 'Text')
  8. insert into @yourtable values (5, '02', NULL, 'Text')
  9. insert into @yourtable values (6, '03', NULL, 'Text')
  10. insert into @yourtable values (7, '01', 'acc003', 'Text')
  11. insert into @yourtable values (8, '03', NULL, 'Text')
  12. insert into @yourtable values (9, '04', NULL, 'Text')
  13. insert into @yourtable values (10, '01', 'acc004', 'Text') 
  14.  
  15. select * 
  16. from @YourTable where PKIdentifier is not null
  17.  
  18. select * 
  19. from @YourTable where PKIdentifier is null
  20.  
  21. select recnum, RecType, PKIdentifier, RecordData,
  22. newPKIdentifier = isnull((select top 1 PKIdentifier from @YourTable b where a.recnum > b.recnum and b.PKIdentifier is not null and a.pkidentifier is null order by b.recnum desc),a.pkidentifier)
  23. from @YourTable a
  24.  
Happy Coding.

-- CK
Oct 8 '08 #2
cdex33
2 New Member
Thanks. That's what I was looking for.
Oct 8 '08 #3

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

Similar topics

1
6594
by: Jim Geissman | last post by:
Help, please. I am trying to update a table with this structre: CREATE TABLE Queue (PropID int, EffDate smalldatetime, TxnAmt int) INSERT Queue (PropID) SELECT 1 INSERT Queue (PropID)...
4
5296
by: Rustam Bogubaev | last post by:
Hi, I have a table with the following columns: ID INTEGEDR, Name VARCHAR(32), Surname VARCHAR(32), GroupID INTEGER, SubGroupOneID INTEGER, SubGroupTwoID...
16
16972
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
2
5819
by: Joshua Moore-Oliva | last post by:
I have a query that is asking me to GROUP a column, yet when I GROUP it it causes an error near GROUP. What is very strange about the following query is that the line list_size_active =...
3
3424
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
4
1238
by: Daniel | last post by:
Hi All, I have question regarding to the read, and update the row in the table. for example: user A, read the row of data from the table. user B, read the row of data from the table as well....
4
11269
by: Nick Barr | last post by:
Hi, I am trying to gather stats about how many times a resource in our web app is viewed, i.e. just a COUNT. There are potentially millions of resources within the system. I thought of two...
0
2271
by: magnolia | last post by:
i created a trigger that will record the changes made to a table .everything works fine except the insert query.whenerever i try to insert a record it fires insert and update triger at the same time...
13
4140
by: Neil | last post by:
I'm running an update query in SQL 7 from QA, and it runs forever. Has been running for 20 minutes so far! The query is quite simple: update a single field in a table, based on a join with another...
3
3930
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
0
7134
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
7012
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...
1
6901
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...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4920
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...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
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...

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.