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?
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 - set nocount on
-
declare @yourtable table (recnum int, RecType varchar(2), PKIdentifier varchar(15), RecordData varchar(10))
-
-
insert into @yourtable values (1, '01', 'acc001', 'Text')
-
insert into @yourtable values (2, '03', NULL, 'Text')
-
insert into @yourtable values (3, '04', NULL, 'Text')
-
insert into @yourtable values (4, '01', 'acc002', 'Text')
-
insert into @yourtable values (5, '02', NULL, 'Text')
-
insert into @yourtable values (6, '03', NULL, 'Text')
-
insert into @yourtable values (7, '01', 'acc003', 'Text')
-
insert into @yourtable values (8, '03', NULL, 'Text')
-
insert into @yourtable values (9, '04', NULL, 'Text')
-
insert into @yourtable values (10, '01', 'acc004', 'Text')
-
-
select *
-
from @YourTable where PKIdentifier is not null
-
-
select *
-
from @YourTable where PKIdentifier is null
-
-
select recnum, RecType, PKIdentifier, RecordData,
-
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)
-
from @YourTable a
-
Happy Coding.
-- CK
Thanks. That's what I was looking for.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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)...
|
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...
|
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...
|
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 =...
|
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...
| |
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....
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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: 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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |