Hi,
I have a stored procedure that has to extract the child records for
particular parent records.
The issue is that in some cases I do not want to extract all the child
records only a certain number of them.
Firstly I identify all the parent records that have the requird number
of child records and insert them into the result table.
insert into t_AuditQualifie dNumberExtractD etails
(BatchNumber,
EntryRecordID,
LN,
AdditionalQualC ritPassed)
(select t1.BatchNumber,
t1.EntryRecordI D,
t1.LN,
t1.AdditionalQu alCritPassed
from
(select BatchNumber,
RecordType,
EntryRecordID,
LN,
AdditionalQualC ritPassed
from t_AuditQualifie dNumberExtractD etails_Temp) as t1
inner join
(select BatchNumber,
RecordType,
EntryRecordID,
Count(*) as AssignedNumbers ,
max(TotalNumber s) as TotalNumbers
from t_AuditQualifie dNumberExtractD etails_Temp
group by BatchNumber, RecordType, EntryRecordID
having count(*) = max(TotalNumber s)) as t2
on t1.BatchNumber = t2.BatchNumber
and t1.RecordType = t2.RecordType
and t1.EntryRecordI D = t2.EntryRecordI D)
then insert the remaining records into a temp table where the number of
records required does not equal the total number of child records, and
thenloop through each record manipulating the ROWNUMBER to only select
the number of child records needed.
insert into @t_Qualificatio nMismatchedAllo cs
([BatchNumber],
[RecordType],
[EntryRecordID],
[AssignedNumbers],
[TotalNumbers])
(select BatchNumber,
RecordType,
EntryRecordID,
Count(*) as AssignedNumbers ,
max(TotalNumber s) as TotalNumbers
from t_AuditQualifie dNumberExtractD etails_Temp
group by BatchNumber, RecordType, EntryRecordID
having count(*) <max(TotalNumbe rs))
SELECT @QualificationM ismatched_RowCn t = 1
SELECT @MaxQualificati onMismatched = (select count(*) from
@t_Qualificatio nMismatchedAllo cs)
while @QualificationM ismatched_RowCn t <= @MaxQualificati onMismatched
begin
--## Get Prize Draw to extract numbers for
select @RecordType = RecordType,
@EntryRecordID = EntryRecordID,
@AssignedNumber s = AssignedNumbers ,
@TotalNumbers = TotalNumbers
from @t_Qualificatio nMismatchedAllo cs
where QualMismatchedA llocsRowNum = @QualificationM ismatched_RowCn t
SET ROWCOUNT @TotalNumbers
insert into t_AuditQualifie dNumberExtractD etails
(BatchNumber,
EntryRecordID,
LN,
AdditionalQualC ritPassed)
(select BatchNumber,
EntryRecordID,
LN,
AdditionalQualC ritPassed
from t_AuditQualifie dNumberExtractD etails_Temp
where RecordType = @RecordType
and EntryRecordID = @EntryRecordID)
SET @QualificationM ismatched_RowCn t =
QualificationMi smatched_RowCnt + 1
SET ROWCOUNT 0
end
Is there a better methodology for doing this .....
Is the use of a table variable here incorrect ?
Should I be using a temporary table or indexed table if there are a
large number of parent records where the child records required does
not match the total number of child records ? 2 3333
Looping is always to be avoided if possible. What version of SQL
Server are you running? In the (unlikely) event it is 2005, I think
it should be possible to eliminate the looping, as TOP accepts a
variable in 2005, rather than requiring a constant.
Even with other releases I think I see a way to eliminate the loop.
Assuming that QualMismatchedA llocsRowNum in table
t_AuditQualifie dNumberExtractD etails_Temp is an identity column or
other unique value, I think this can replace the entire loop process.
INSERT INTO t_AuditQualifie dNumberExtractD etails
(BatchNumber,
EntryRecordID,
LN,
AdditionalQualC ritPassed)
SELECT BatchNumber,
EntryRecordID,
LN,
AdditionalQualC ritPassed
FROM t_AuditQualifie dNumberExtractD etails_Temp as A
WHERE TotalNumbers <=
(select count(*)
from t_AuditQualifie dNumberExtractD etails_Temp as B
where A.BatchNumber = B.BatchNumber
and A.RecordType = B.RecordType
and A.EntryRecordID = B.EntryRecordID
and A.QualMismatche dAllocsRowNum <=
B.QualMismatche dAllocsRowNum)
That would require a good index on the columns (BatchNumber,
RecordType, EntryRecordID).
I take it that QualMismatchedA llocsRowNum in table
@t_Qualificatio nMismatchedAllo cs is an identity column? Are the
values always starting at 1? My concern is looping through a lot of
numbers that might not exist in the table.
Looking at the tests that split the data into two categories:
>having count(*) = max(TotalNumber s)) as t2 having count(*) <max(TotalNumbe rs))
brings up a question. What sort of row count do we get from each of
the three relationships?
count(*) = max(TotalNumber s)
count(*) < max(TotalNumber s) -- If this is significant, see below.
count(*) max(TotalNumber s)
The reason I ask is that I do not see a difference in the final
results between count(*) < max(TotalNumber s) and when they are
equal. In both those cases all the rows are used. The looping
process appears to me to only be required when there are MORE rows
(count(*) max(TotalNumber s)) than should be inserted. If I have not
missed something, and if a significant part of the processing is for
count(*) < max(TotalNumber s), then the first test could be modified
to:
having count(*) <= max(TotalNumber s)) as t2
And the one used to generate the exceptions used in the loop changed
to:
having count(*) max(TotalNumber s))
Which would cut down on the looping (or the alternate joining
proposed) process.
Roy Harvey
Beacon Falls, CT
On 28 Oct 2006 23:26:51 -0700, "Catch_22" <ca********@yah oo.co.uk>
wrote:
>Hi,
I have a stored procedure that has to extract the child records for particular parent records.
The issue is that in some cases I do not want to extract all the child records only a certain number of them.
Firstly I identify all the parent records that have the requird number of child records and insert them into the result table.
insert into t_AuditQualifie dNumberExtractD etails
(BatchNumber, EntryRecordID, LN, AdditionalQualC ritPassed) (select t1.BatchNumber,
t1.EntryRecordI D,
t1.LN,
t1.AdditionalQu alCritPassed from (select BatchNumber,
RecordType,
EntryRecordID,
LN,
AdditionalQualC ritPassed
from t_AuditQualifie dNumberExtractD etails_Temp) as t1
inner join (select BatchNumber, RecordType, EntryRecordID, Count(*) as AssignedNumbers , max(TotalNumber s) as TotalNumbers from t_AuditQualifie dNumberExtractD etails_Temp group by BatchNumber, RecordType, EntryRecordID having count(*) = max(TotalNumber s)) as t2 on t1.BatchNumber = t2.BatchNumber and t1.RecordType = t2.RecordType and t1.EntryRecordI D = t2.EntryRecordI D)
then insert the remaining records into a temp table where the number of records required does not equal the total number of child records, and thenloop through each record manipulating the ROWNUMBER to only select the number of child records needed.
insert into @t_Qualificatio nMismatchedAllo cs ([BatchNumber], [RecordType], [EntryRecordID], [AssignedNumbers], [TotalNumbers]) (select BatchNumber, RecordType, EntryRecordID, Count(*) as AssignedNumbers , max(TotalNumber s) as TotalNumbers from t_AuditQualifie dNumberExtractD etails_Temp group by BatchNumber, RecordType, EntryRecordID having count(*) <max(TotalNumbe rs))
SELECT @QualificationM ismatched_RowCn t = 1
SELECT @MaxQualificati onMismatched = (select count(*) from @t_Qualificati onMismatchedAll ocs)
while @QualificationM ismatched_RowCn t <= @MaxQualificati onMismatched begin --## Get Prize Draw to extract numbers for select @RecordType = RecordType, @EntryRecordID = EntryRecordID, @AssignedNumber s = AssignedNumbers , @TotalNumbers = TotalNumbers from @t_Qualificatio nMismatchedAllo cs where QualMismatchedA llocsRowNum = @QualificationM ismatched_RowCn t
SET ROWCOUNT @TotalNumbers
insert into t_AuditQualifie dNumberExtractD etails (BatchNumber, EntryRecordID, LN, AdditionalQualC ritPassed)
(select BatchNumber, EntryRecordID, LN, AdditionalQualC ritPassed from t_AuditQualifie dNumberExtractD etails_Temp where RecordType = @RecordType and EntryRecordID = @EntryRecordID)
SET @QualificationM ismatched_RowCn t =
QualificationM ismatched_RowCn t + 1 SET ROWCOUNT 0 end
Is there a better methodology for doing this .....
Is the use of a table variable here incorrect ?
Should I be using a temporary table or indexed table if there are a large number of parent records where the child records required does not match the total number of child records ?
Hopefully you saw my response to your other message.
Roy Harvey
Beacon Falls, CT This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Stuart McGraw |
last post by:
I haven't been able to figure this out and would
appreciate some help...
I have two tables, both with autonumber primary
keys, and linked in a conventional master-child
relationship. I've created forms for both those
tables, and inserted the child table form into the
master table form as a subform. It works just as
it is supposed to, in that I can create a new master
record, and then add detail records.
|
by: Johann Blake |
last post by:
I have a dataset that contains a parent table and a child table. A
DataRelation exists between the two. I was under the impression from
reading the VS docs that when I filled the parent table, the child
table would be automatically filled with the child records. When I fill
the parent, I limit the table to only a single record, so that all the
records in the child table will belong to this parent. But when the
Fill method is executed with...
|
by: Aaron Smith |
last post by:
I have a parent table that has one child table. The parent has a single
field (ID, AutoIncrement, Seed 0, Step -1 in the DataSet, Seed 1, step
1, in DataSource). The child is linked to this ID column in the parent.
I have the parent fields in textboxes and the child is a DataGrid. When
I add a new parent record, then go to add child records, I am getting an
exception that says, "ForeignKeyConstraint requires the child key values
(0) to...
|
by: PAUL |
last post by:
Hello,
I have 2 tables with a relationship set up in the dataset with vb
..net. I add a new record to the parent table then edit an existing child
record to have the new parent ID. However when I do the update the changed
parentid in the child table fails to change. No error is given its just that
the change is not written to the Database.
When I step through the records for the child table the one I would expect
to be changed has a row...
|
by: Thelma Lubkin |
last post by:
My ColorSet building form/subform now works beautifully, thanks to the
help that I've gotten from people in this group.
The working form displays the parent ColorSet record with the
child records displayed in the subform below it.
ParentTable fields: ColorsetName Classsize
ChildTable fields: ColorsetName ColorSequenceNumber RedVal GreenVal BlueVal
I've now been asked to allow the user to generate a new ColorSet from
| |
by: Bob |
last post by:
I got three related datagrid views one parent and two children of the same.
The two child tables contain many thousands of records and some of the
contents are bitmap files in a sql server database. The default behaviour of
loading all the contents of the parent data table and also all the related
data is not acceptable, its takes too long to complete. What I need to do
IMHO, is to load the parent table and after its loaded, and it gets...
|
by: Hexman |
last post by:
Hello All,
What I'm trying to do is update a child record using a parent-child relation. I want to find out if it is faster than than doing multiple selects.
Anyways, I've created a dataset (ds), have 2 datatables (dtRC and dtST). Created the parent-child relationship with multiple columns and added the
relation to the dataset.
Now what I want is to move through each of the dtRC (parent) records, get all related dtST (child) records...
|
by: Swinky |
last post by:
I hope someone can help...I feel like I'm walking in the dark without a
flashlight (I'm NOT a programmer but have been called to task to do
some work in Access that is above my head).
I have code that will successfully copy a record and append the
information to a new record in the same table (parent table) within a
form. However, there are related child tables with primary keys (set
to Autonumber) stored in sub-forms. That information...
|
by: Rick |
last post by:
VS 2005
I' m setting up a parent/child datagridviews in a form.
I am doing a lot of this by hand coding in order to get the feel of things.
I want a change in the parent table to trigger a change in the child.
Where is the best place (if I cannot set it up to be automatic through
relationships) to catch the change in the parent to refetch the child rows?
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |