简体   繁体   English

Dapper - 存储过程 - 'GO' 附近的语法不正确

[英]Dapper - Stored Procedure - Incorrect syntax near 'GO'

I have a stored procedure in a myscript.sql file that looks like this:我在myscript.sql文件中有一个存储过程,如下所示:

CREATE PROCEDURE [dbo].[_GetUserID]
    @EmailAddress NVARCHAR(254)
AS   
    DECLARE @UserID UNIQUEIDENTIFIER;

    SELECT @UserID = [ID] 
    FROM [dbo].[User] 
    WHERE [EmailAddress] = @EmailAddress

    PRINT @UserID    
GO

I have some C# code that relies on Dapper to run this script.我有一些 C# 代码依赖于 Dapper 来运行这个脚本。 I can successfully run this script when I copy-and-paste it into Azure Data Studio.当我将它复制并粘贴到 Azure Data Studio 时,我可以成功运行该脚本。 However, when I am trying to run this script from code, I get an error:但是,当我尝试从代码运行此脚本时,出现错误:

Incorrect syntax near 'GO' “GO”附近的语法不正确

My C# code looks like this:我的 C# 代码如下所示:

try
{
     var script = File.ReadAllText("<path to myScript.sql is here>");

     using (var connection = new SqlConnection(dbConnectionString))
     {
         var command = connection.CreateCommand();
         command.CommandText = script;
         command.CommandType = CommandType.Text;

         connection.Open();
         command.ExecuteNonQuery();
     }
                
     Console.WriteLine("Success.");
}
catch (Exception ex)
{
     Console.WriteLine($"Failed. Reason: '{ex.Message}')");
}

I don't understand why I can run myScript.sql from Azure Data Studio, however, it's not working from my C# code.我不明白为什么我可以从 Azure Data Studio 运行myScript.sql ,但是,它在我的 C# 代码中不起作用。 I'm also creating tables using the same approach and it works fine.我也在使用相同的方法创建表,它工作正常。 I'm not sure what I'm missing.我不确定我错过了什么。

GO is not a valid T-SQL keyword - it's a separator that is used by SQL Server Management Studio and obviously also Azure Data Studio. GO不是有效的 T-SQL 关键字 - 它是 SQL Server Management Studio 使用的分隔符,显然也是 Azure Data Studio。

To fix this, just simply remove that GO line from your .sql script file and run it without this - should be just fine.要解决此问题,只需从您的.sql脚本文件中删除该GO行并在没有它的情况下运行它 - 应该没问题。

On a different note: having nothing but a PRINT statement in your stored procedure doesn't make a lot of sense - don't you want to actually SELECT @UserId to get that data sent back to the caller?? SELECT @UserId说法:在您的存储过程中只有PRINT语句并没有多大意义-您难道不想实际SELECT @UserId将数据发送回调用者吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core 存储过程错误:“@p1”附近的语法不正确 - EF Core Stored Procedure Error: Incorrect syntax near '@p1' Dapper 和列表参数在 ',' 附近返回不正确的语法 - Dapper and list parameters return Incorrect syntax near ',' @indexName附近的语法不正确。 短小精悍的 - Incorrect syntax near @indexName. Dapper 错误“在&#39;nvarchar&#39;附近的语法不正确。”试图将行插入具有存储过程的表中 - Error “Incorrect syntax near 'nvarchar'.” trying to insert row int a table with stored procedure 错误:变量名在查询批处理或存储过程中必须是唯一的。 关键字&#39;WHERE&#39;附近的语法不正确 - Error: Variable names must be unique within a query batch or stored procedure. Incorrect syntax near the keyword 'WHERE' 将 Guid 传递给存储过程会引发 Microsoft.Data.SqlClient.SqlException:“'@Id' 附近的语法不正确。” - Passing Guid to stored procedure throws Microsoft.Data.SqlClient.SqlException: 'Incorrect syntax near '@Id'.' 插入值时,“&#39;GO&#39;附近的语法不正确” - “Incorrect syntax near 'GO'” when inserting values &#39;(&#39;和&#39;=&#39;附近的语法不正确 - Incorrect syntax near '(' and near '=' &#39;&lt;&#39;附近的语法不正确。 标签“ xmlns”已经声明。 标签名称在查询批处理或存储过程中必须唯一 - Incorrect syntax near '<'. The label 'xmlns' has already been declared. Label names must be unique within a query batch or stored procedure Dapper存储过程和视图 - Dapper Stored procedure and View
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM