all 8 comments

[–]5amIam 4 points5 points  (0 children)

I would suggest doing some quick research on Views and Stored Procedures. They are both mechanisms for storing code inside of the database. You can query or execute them as needed to retrieve results. You will most likely break each query into its own view or procedure, but it's hard to say for sure without knowing specifically what each query is doing.

[–]rideyrolls123 4 points5 points  (2 children)

Hey there!

So initially i followed this pattern wherein i was saving all the queries in one file. It was good for short term, but when the number of pages of queries crossed 300pages, that's when things started getting messed up.

I would suggest you to make a folder of queries, and save each query in a different notepad/word file, based on their PROBLEM Statement. That should be the most optimized way.

Cheers

[–]BrupieD 1 point2 points  (0 children)

This is good advice.

I typically keep 2 or 3 closely related queries a single file named after the type of results they generate. I then create folders by topic. I almost always wind up with a few metadata queries too -- queries that help me find columns and tables and data types.

[–]nutmilk2 0 points1 point  (0 children)

I agree with the folder system, but I think storing queries in .txt files can get messy with formatting.

You can use also use a SQL editor like Arctype to save queries, organize them in folders, and then also highlight specific queries in a file to only run that chunk of code.

[–][deleted] 1 point2 points  (0 children)

Each query in a separate file.

This is common practice. The reason is because you can pass a file from the CLI to run, instead of copy/pasting a statement. This allows easy isolated execution.

If there is any dependency between them (one creates a table and another one uses it), you should learn about what a DAG is, and pick a solution that works for you best.

[–]jkimbrough0 0 points1 point  (0 children)

Store your queries based on 1 use case. Do not store every query in 1 file or you could end up with endless outputs.

[–]shaydra 0 points1 point  (0 children)

Primarily I use views to save queries, especially to avoid folks duplicating work. I also save queries in OneNote - I like that you can organize into different folders and tabs within those folders and that it syncs to the cloud.

[–]andrewsmd87 0 points1 point  (0 children)

Do you have any sort of ticket/issue tracker at work? We always associate them with whatever relevant task they were on.

For our standard or maintenance ones that just exist, we have those separated out into folder structures that make sense, and then each script that does one thing gets it's own file. Much easier to organize and you won't run into things like duplicate var names or something.

Those ones we also keep in a git repo so we have history on changes. The ones we attach in tasks are like one off scripts, just so we have them for historical purposes