3

I started to learn functions and stored procedures in Microsoft SQL Server and noticed that everything that they do also can be done with a queries. I'm sure, they exist for a reason, so I'd like to ask:

  1. What can be done using functions / procedures that's impossible to do with a query?
  2. In which cases one should use procedures/functions and not queries?

1 Answer 1

4

There's nothing magic about functions or procedures - there's nothing you cannot do in an ad-hoc query as well.

They can be used to reuse some code - write the function once and use it everywhere, instead of writing the same T-SQL code over and over and over again.

And they can be used to combine code that belongs together (like withdraw amount x from account #1 and deposit it into account #2) into a single, reusable procedure which can also handle transactions internally.

So basically: nothing magic - but using functions and procedures (like in any other programming language) can help reuse and centralize some code, and make your life easier

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

Comments

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.