March 31, 2011 at 7:19 am
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
March 31, 2011 at 7:53 am
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 31, 2011 at 8:01 am
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.
March 31, 2011 at 10:04 am
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 1, 2011 at 2:19 am
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.
April 1, 2011 at 2:26 am
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
April 1, 2011 at 2:31 am
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?
April 1, 2011 at 2:51 am
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.
April 1, 2011 at 7:14 am
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 1, 2011 at 8:58 am
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
April 1, 2011 at 9:03 am
Sounds you were not releasing the files correctly. If you streamed them and the properly disposed you should never have any issue reading a file just about any size.
If you are certain that you need to look at files in directories directly from sql I am going to have to defer to somebody else. SQL server is not the most efficient for doing file system stuff but I know it can be done. Can you use SSIS? This would make your life a lot easier instead of trying to do this in just 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 5, 2011 at 8:09 am
Well, seeing as when its reading in a particular file, it errors half way through the reading process i'm not sure what else i can do.
I've attempted splitting the arraylists, only including certain fields (That require no conversion) yet still i have the same issue.
I've never used SSIS, and i only have access to free software via the internet. I will have to look into it.
Many thanks for the help.
April 5, 2011 at 11:15 am
File size of 300MB shouldn't be an issue with streaming. You can get 600MB into a file stream on a 32bit system in .NET. If you are properly disposing of the stream after each file the number of files will not make any difference.
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply