1

I'm looking for something that lets me better organize my SQL scripts.

I want to be able to include SQL code from one script into another, similar to how in C++ you can do include foo.c to import the contents of foo.c into your program.

Is that possible with SQL?

(FYI, I'm using SQL Server)

3
  • 1
    why don't you use functions for each query? Create classes with queries for each entity Commented Dec 16, 2013 at 23:10
  • @SamD I'm looking more so at ways to organize my stored procedures and functions, or to split my database creation scripts into chunks responsible for different types of tables, views, etc Commented Dec 16, 2013 at 23:19
  • 1
    There are various tools available for scripting and maintaining databases. It turns out that it is rather difficult to generate a script, in general, to recreate a database due to the myriad ways in which dependencies can exists between entities. (A trigger that references a view that uses a function that ... .) Have a look at redgate just to get some idea of what a commercial product handles. (Not a product recommendation, just the first name that came to mind.) Commented Dec 17, 2013 at 3:38

1 Answer 1

2

SQL is not designed to work like structured or object oriented programming languages.

In case you want to re-use scripts you built, I suggest you create functions and/or stored procedures which you will then be able to call, so you would avoid having to rewrite the code (this would be like "importing").

Functions are the basis of returning data in a custom format. You can read more about them here. You will find tips on when/where and how to use functions.

If you think functions are not enough, try reading about stored procedures here.

Sign up to request clarification or add additional context in comments.

1 Comment

I am using functions and stored procedures. I was hoping for a way to organize them better though. Another scenario would be to split my database creation scripts into chunks responsible for creating different types of tables, views, etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.