1

When I try to create a procedure, I get an error message:

SQL80001: Incorrect Syntax:" Create Procedure" must be the only statement in the batch

This is my procedure:

CREATE PROCEDURE [dbo].[USP_UserRoles_Select]
(
    @Rolename VARCHAR(30) = ''
)
AS
BEGIN
    Select ID,Name
    FROM
        AspNetRoles
    WHERE
        Name like  @Rolename +'%'
END

What does the error mean and how to fix it?

closed as off-topic by Michael Green, Philᵀᴹ, Andriy M, billinkc, mustaccio Aug 22 '16 at 14:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Too localized - this could be because your code has a typo, basic error, or is not relevant to most of our audience. Consider revising your question so that it appeals to a broader audience. As it stands, the question is unlikely to help other users (regarding typo questions, see this meta question for background)." – Michael Green, Philᵀᴹ, Andriy M, billinkc, mustaccio
If this question can be reworded to fit the rules in the help center, please edit the question.

7

You just need to put a GO batch separator around your CREATE PROCEDURE. You have something else above it, and it needs to be in its own batch. Hence the GO that's needed.

GO
CREATE PROCEDURE [dbo].[USP_UserRoles_Select]
(
    @Rolename VARCHAR(30) = ''
)
AS
BEGIN
    Select ID,Name
    FROM
        AspNetRoles
    WHERE
        Name like  @Rolename +'%'
END
GO

A GO is also needed after the script if there is code following the END. The BEGIN and END statements are superfluous so all code in the batch will be part of the stored procedure.

Not the answer you're looking for? Browse other questions tagged or ask your own question.