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

后端 未结 2 1438
鱼传尺愫
鱼传尺愫 2020-12-21 04:24

This question has been asked before but it all involved using \"go\" which I am not in need of here, at least I believe so.

I am following this tut https://www.youtu

相关标签:
2条回答
  • 2020-12-21 04:50

    Yes, SQL Server wants the create procedure as the first line. Just remark out the select above, it is not what you want anyway, because you have specified the fields in the stored procedure.

    0 讨论(0)
  • 2020-12-21 05:09

    If you want to keep the script as it is (select followed by a create procedure), you can construct the creation of the stored procedure in a NVARCHAR and EXECUTE it using sp_executesql. This way the CREATE statement is the first statement. Like this:

    Select * From Snacks
    
    EXECUTE sp_executesql N'
      Create Proc spGetSnackByID
      @Id int
      as
      Begin
        Select Id, Name, Location
        from Snacks where Id = @Id
      End
    ';
    
    0 讨论(0)
提交回复
热议问题