13

Is it OK to execute a stored procedure without 'EXEC' or 'EXECUTE' word in the beginning ?

Normally to execute stored procedure I do

EXEC DeleteProfile 'Joe Smith'

But, I've noticed that next command works as well:

DeleteProfile 'Joe Smith'

Why should I write 'EXEC' or 'EXECUTE' word?

Is it ok not to write it before stored procedure name to execute stored procedure?

Thanks.

1 Answer 1

19

I've found in MSDN "You do not have to specify the EXECUTE keyword when you execute stored procedures when the statement is the first one in a batch."

Get it. Sorry for question.

This wouldn't work without GO at the end of each statement:

DeleteProfile 'Joe Smith'
DeleteProfile 'Joe Smith'
DeleteProfile 'Joe Smith'
Sign up to request clarification or add additional context in comments.

2 Comments

It's quite acceptable to ask a (reasonable) question and then answer it. I didn't know that the exec was at all optional, so you've taught me something today (+1 for that)
I did notice Exec was now optional in Sql server 2008+higher, but didn't know about the "when the statement is the first one in a batch" requirement which seems like a big "gotcha" that would have been confusing to debug one day. So I have also learned something useful :) Thanks!

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.