这给了GO的错误 [英] It is giving error for GO

查看:101
本文介绍了这给了GO的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了这个功能,但它给出了错误



I have made this function but it is giving error

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[SuccessEHS].[GetDispense]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [SuccessEHS].[GetDispense]
GO

Create function [SuccessEHS].[GetDispense] 
(
    @text nvarchar(40)
) 
returns varchar(40)
as begin
    -- emulate vba's val() function
    declare @result varchar(40)
 
    declare @tmp varchar(40)
    set @tmp = @text
    while isnumeric(@tmp) = 0 and len(@tmp)>0 begin
        set @tmp=left(@tmp,len(@tmp)-1)
    end
    set @result = @tmp
    return @result
end
GO





我尝试过:



我写的语法正确。但我没有得到解决方案。



What I have tried:

I have written correct syntax. But i don't get the solution.

推荐答案

GO 语句仅适用于SQL Server Management Studio,如果您从代码中使用它您不需要使用GO。

您也可以在SQL Server 2016及更高版本中使用此语法:

The GO statement only works in SQL Server Management Studio, if you use it from code you don't need to use GO.
You can also use this syntax in SQL Server 2016 and higher:
DROP FUNCTION IF EXISTS [SuccessEHS].[GetDispense]


这是一个使用GO命令从嵌入式资源SSMS脚本执行SQL命令的例程:

Here is a routine to execute SQL commands from an embedded resource SSMS script with GO commands:
public static void CreateDatabase(string server, string catalog, string username, string password)
{
	// get the stream
	Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNamespace.DatabaseUnicode.sql");

	if (stream != null)
	{
		var sr = new StreamReader(stream);
		var script = sr.ReadToEnd();
		sr.Close();
		stream.Close();

		var connectionString = GetConnectionString(server, string.Empty, username, password);
		using (var connection = new SqlConnection(connectionString))
		{
			connection.Open();
			
			if (connection.State == System.Data.ConnectionState.Open)
			{
				// replace it with the correct catalog name
				script = script.Replace("##ArchiveName##", catalog);

				IDbCommand command = connection.CreateCommand();
				command.CommandType = CommandType.Text;

				script = script.Replace("'", "''");
				script = script.Replace("\r\nGO\r\n", "\x07");      // "GO" is not a Transact SQL command, but utilities command
				script = script.Replace("\r\ngo\r\n", "\x07");
				script = script.Replace("\r\nGo\r\n", "\x07");
				script = script.Replace("\r\ngO\r\n", "\x07");

				string[] arrBatches = script.Split('\x07');

				for (int i = 0; i < arrBatches.Length; ++i)
				{
					string scriptBatch = arrBatches[i];
					if (scriptBatch == string.Empty)
					{
						continue;
					}

					string sqlScript = string.Format("sp_executesql N'{0}'", scriptBatch);
					command.CommandText = sqlScript;
					command.CommandTimeout = SqlCommandTimeout;
					command.ExecuteNonQuery();
				}
			}
		}

		SqlConnection.ClearAllPools();
	}
	else
	{
		throw new Exception("The resource sql script could not be found");
	}
}


这篇关于这给了GO的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆