Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Loop through multiple tables Expand / Collapse
Author
Message
Posted Thursday, March 31, 2011 7:19 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, January 10, 2012 5:12 AM
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
Post #1086823
Posted Thursday, March 31, 2011 7:53 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 13,327, Visits: 12,820
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)
Post #1086851
Posted Thursday, March 31, 2011 8:01 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, January 10, 2012 5:12 AM
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.
Post #1086862
Posted Thursday, March 31, 2011 10:04 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 13,327, Visits: 12,820
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)
Post #1086967
Posted Friday, April 1, 2011 2:19 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, January 10, 2012 5:12 AM
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.
Post #1087293
Posted Friday, April 1, 2011 2:26 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, March 31, 2014 4:12 AM
Points: 159, Visits: 489
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
Post #1087294
Posted Friday, April 1, 2011 2:31 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 9:19 AM
Points: 1,307, Visits: 4,508
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?



Post #1087297
Posted Friday, April 1, 2011 2:51 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, January 10, 2012 5:12 AM
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.
Post #1087306
Posted Friday, April 1, 2011 7:14 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 13,327, Visits: 12,820
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)
Post #1087439
Posted Friday, April 1, 2011 8:58 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, January 10, 2012 5:12 AM
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
Post #1087534
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse