Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 29-May-19 2:03am
Comments
OriginalGriff 29-May-19 6:05am    
What error do you get?
Santosh kumar Pithani 29-May-19 6:24am    
Hello,your IF condition declare is wrong i.e instead "Else" you given "GO" so how it works?show me your Error Message

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]
 
Share this answer
 
v3
Comments
Member 11776570 29-May-19 7:22am    
Okay but I am using this in Visual Studio in .sql file.
RickZeeland 29-May-19 7:53am    
GO statement only works in SQL Server Management Studio, so you will have to delete those lines !
If you want to remain 'compatible' you can also delete these lines on the fly in your code.
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");
	}
}
 
Share this answer
 
v3
Comments
Richard Deeming 29-May-19 10:22am    
Just wait - someone will add a GO 5 to one of your script files and break your code. "But it works in SSMS..." :)
RickZeeland 29-May-19 12:50pm    
I wonder if anyone is using this, I have never seen it in use, but my SQL Server knowledge is getting rusty as we switched to PostgreSQL years ago.
Anyway, thanks for reminding us of this option :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900