Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Loop through multiple tables


Loop through multiple tables

Author
Message
Rixxe
Rixxe
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 75
Hi All,

Bit of background: The data is medical records of patients, so as such each practice (GP practice) has a different ID, and for each ID there can be up to 7 different files all named differently. The files are currently saved in a folder like so:

patient.a0001
patient.a0001
patient.a0004
medical.a0004
etc etc

So the issue arises, I've created a query for a single table, and would like to loop it through all/a selected number of other practices/tables.
I understand the concept of WHILE - DO - END etc as i'm semi confident in C#, however i've yet to find anything for SQL server that explains or shows how to do this.

So for example (To make it easy), if i had this query:

SELECT a.*
FROM patient.a0004
WHERE eventdate = '20100101'

How would i go about looping through the various tables in a folder with ONLY the name 'patient'?

Many thanks for reading.

Rixxe
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16582 Visits: 17024
I don't understand. Your data is saved in files and you want to look at each file?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Rixxe
Rixxe
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 75
No, i wan't to select specific records from each file, and then put them all into one table, I can deal with the last part of the problem.

I cannot however loop through each of the tables, which is the issue.

Sorry i probably didn't explain that very well.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16582 Visits: 17024
Yeah i don't understand what you mean about loop through the tables. Are you saying that the table name is indicated in the file name? Most likely you will have to do some dynamic sql.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Rixxe
Rixxe
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 75
Well for example, i have a folder in C:\
Named Database. In that folder are files like:

patient.0001 etc etc

The name of the table would therefore be patient0001 in SQL, thats what i want to happen.
I then want my query to be run on this table, the query saved, and for it to go to the next File within the folder, So patient.0002

So loop through each file within the folder.

Any ideas?

Thanks

Rixxe.
sachnam
sachnam
SSC-Enthusiastic
SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)

Group: General Forum Members
Points: 159 Visits: 500
It seems that you want a script which could read through the text files and dump the data into tables into sql server.

If , my understanding is correct , then you could try creating linked server with text files.

It is a very fast and easy method to get all your text files data into tables.
But make sure that the structure of your text file must match to your tables in database.

use the below command

EXEC sp_addlinkedserver testingtext_data, 'Jet 4.0',
'Microsoft.Jet.OLEDB.4.0',
'H:\patientdata\', --file path or i would say folder path where all your text files are placed
NULL,
'Text' --for textfile linkedserver
GO


once the linked server is created you coud run a command to read its text

like

select * from testingtext_data...tablename;

For perfoming the same for all files , try create cursor.

Regards,
Sachin
Ian Scarlett
Ian Scarlett
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1421 Visits: 6278
SQL Server doesn't store the data in that structure. I think some other RDBMs do.

SQL Server stores everything (tables, indexes, procedures etc) inside a single file (it can be more than 1, but ignore that for the moment)... are you sure you are talking about a SQL Server database?



Rixxe
Rixxe
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 75
Yes sorry an SQL server database.

I will see what i can learn from using a script, many thanks for the idea.

I'll come back once i hit the next issue.

Thanks

Rixxe.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16582 Visits: 17024
You mentioned using c# in an earlier post, are you doing this from a c# app. If so this should be pretty simple with some dynamic sql in your c# app. If you have to use sql to look at the file system object that is a whole different animal.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Rixxe
Rixxe
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 75
Regarding the C#, I previously created an App that did exactly what i want to do in SQL, however it ran out of Memory due to the size and amount of conversions that are needed to use this data correctly.

To give you a figure, the files are on average over 300,000kb, and theres around 500 files, C# managed to get through about half of those before the Out of Memory Error.

Thanks for the reply.

Rixxe
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search