Forum Moderators: open

Message Too Old, No Replies

How do I merge queries?

Too many tables (greater than 256)

         

MossirgLig

12:02 pm on Sep 14, 2006 (gmt 0)

10+ Year Member



I’ve just started work for a company who have a mess of a SQL database (on SQL Server 2000). They used a piece of form-builder software to create online forms (and miraculously create the sql db in the background without any interaction) and have now discovered that it’s impossible to do anything with it as it stands.

Rather than attempt to fix it, they’ve decided to scrap it and migrate all the data to a new, clean, properly-designed db. All well and good, but I’m having nightmares when it comes to extracting the data.

The company responsible for the form-building software gave us query examples for getting out some information, but once we adapt them for our purposes they become far too large.

Example:

Select obstable.nhi_number, obstable.taskname as taskname, obstable.observation_report_id, obstable.stream_id,
Title.Title, Details.Details, Forename. Forename, Surname.Surname, PreferredName.PreferredName, OtherNoTbl.OtherNoTbl, AddressLine2.AddressLine2, PostCode.PostCode, kcid.kcid from (select p.nhi_number, tt.name as taskname, orr.observation_report_id, pp.stream_id
from
patient p, patientprogramme pp, programme pr, observationreport orr, taskrequest tr, tasktype tt
where p.patient_id = pp.patient_id
and pp.programme_id = pr.programme_id
and pr.name = 'CarenapE'
and pp.patient_programme_status = 'ACTIVE'
and p.record_status = 'A'
and p.patient_id = orr.patient_id
and p.patient_id = tr.patient_id
and orr.record_status = 'A'
and orr.taskrequest_id = tr.taskrequest_id
and tr.tasktype_id = tt.tasktype_id
and tt.name = 'CNE_BasicInfo'
and pp.stream_id = tr.stream_id
)as obstable
left outer join
(select ao.observation_report_id, dt.name, ao.value as Title
from
atomicobservation ao
inner join datatype dt
on dt.datatype_id=ao.datatype_id
where dt.name = 'Title')
as Title
on Title.observation_report_id=obstable.observation_report_id
left outer join
(select ao.observation_report_id, dt.name, ao.value as Details
from
atomicobservation ao
inner join datatype dt
on dt.datatype_id=ao.datatype_id
where dt.name = 'Details')
as Details
on Details.observation_report_id=obstable.observation_report_id

etc etc.

I’ve had to split the queries into _Part1 and _Part2 because I was getting the “too many field/tables names (greater than 256)” error message.

My question is this: how can I merge the two parts of the query together so that I get a one-lump large result? I wasn’t sure that I could use Union to join them since they don’t really hold the same fieldnames, or tablenames.

Any ideas?

Thanks,
-Mossie-

FalseDawn

2:34 pm on Sep 14, 2006 (gmt 0)

10+ Year Member




and miraculously create the sql db in the background without any interaction

Not sure what you mean by this - either the data is there for querying, or it's not. :-)

In any case, it sounds like an ideal task for DTS to me.
If all you want to run the queries for is to get the data to import to another table, it should be pretty straightforward.

If you're not familiar with DTS, can you not just run the queries separately, and copy/paste the results into a text file/spreadsheet or something, then into the new table?

MossirgLig

5:12 pm on Sep 14, 2006 (gmt 0)

10+ Year Member



Sorry, I meant there was no user designing the database. It's ended up with fifty or sixty references to each table and has created tables numerically - we have no idea what numbers stand for what table.

deally, I'd like a stored procedure which could merge the two, or three, or four, separate parts of the emourmous query into one and then send the results to a new table...but I have no idea how to go about getting the separate queries joined together.

-M-

FalseDawn

8:11 pm on Sep 14, 2006 (gmt 0)

10+ Year Member




but I have no idea how to go about getting the separate queries joined together.

I am unsure why exactly you need to join them together - if you have several queries, all returning "roughly" the same number of fields and datatypes, the fact that the column names may be different is not important - why can't you just run them sequentially as I suggested, copying each set of results to a spreadsheet/file?