As this goes without saying, I’m a severe novice when it comes to T-SQL and using SSMSE isn’t my first option but I’m stuck with it and a poorly designed database. Essentially I’m trying to output the data from our current database so I can upload it to our new software and I need to get this data in manageable CSV files first.
I’m trying to move measurements associated with subjects in various studies but the data lies across several tables. The tables are as follows (primary keys listed first):
CREATE TABLE Study (StudyID int, StudyName nvarchar(50))
CREATE TABLE Subject (SubjectID int, SubjectName nvarchar(20), StudyID int)
CREATE TABLE Implant (ImplantID int, ImplantName nvarchar(255), StudyID int)
CREATE TABLE Measurement (MeasurementID int, SubjectID int, ImplantID int,
Length float, Width float, Volume float,
PerformDateTime datetime)
I know I’m new to databases but this topology really confused me. We have a lot of studies and I’d love to create an excel file for each study that organizes the data in the format for directly importing. To do that I would need to generate a CSV file for each study that looked like this:
CREATE TABLE ImportStudy (SubjectName, PerformDateTime, Length, Width, Volume)
It’s OK that the Subject Names would be repeated in the first column because that’s how the new software inserts it. Any help would be great and save me about a month or two of my life even if I have to use a WHERE StudyName LIKE N'ABC123' and enter each one. If anyone can help me add the T-SQL equivalent of an Object Oriented “For Each” loop that would be amazing. Thanks again for any help.
Best,
Jeff
I’m not 100% sure what you want but getting a CSV file is pretty easy. If you’d like to extract the data you specified, run a query similar to this and have Management Studio output the results to a CSV file:
What’s a bit strange with your schema is that you’ve linked Measurement to both Subjects and Implants. I’ve ignored the link from Subject to Measurement in the query above.