Question

I have written some C# to run through specified SQL scripts, separate the commands at the GO batch separator and execute each command. This is so the SQL objects can be installed at the start of the application. However, I am having an issue with installing CLR assemblies and functions.

It will run through and create the CLR assembly OK, but when it then tried to create the function it is returning the error:

Incorrect syntax near the keyword 'FUNCTION'. Incorrect syntax near the keyword 'AS'.

I have tried creating the assembly and function using the script directly in SSMS and that runs fine. I have also run the function script in my C# after creating the assembly in SSMS and this runs fine. It just doesn't seem to create the function if both scripts are run through C#.

C# Code to execute the SQL script:

var fileInfo = new FileInfo(sqlDirectory + "\\" + fileName);
var script = fileInfo.OpenText().ReadToEnd();

if (!String.IsNullOrEmpty(script))
{
    string sqlBatch = string.Empty;

    script += "\nGO";

    try
    {
        foreach (
            string line in
                script.Split(new string[2] {"\n", "\r"},
                     StringSplitOptions.RemoveEmptyEntries))
        {
            if (line.ToUpperInvariant().Trim() == "GO")
            {
                if (sqlBatch != "")
                {
                    cmd.CommandType = CommandType.Text;

                    cmd.CommandText = sqlBatch;

                    cmd.ExecuteNonQuery();
                    sqlBatch = string.Empty;
                }
            }
            else
            {
                sqlBatch += line + "\n";
            }
        }

        fileInfo.OpenText().Close();
    }
    catch (Exception err)
    {
        var context = HttpContext.Current;
        Global.InstallError = "File Name: " + fileName + "<br/>" +
                                "Error: " + err.Message;
    }
}

CLR Function:

/****** Object:  UserDefinedFunction [dbo].[fn_JSON_FormatArray]    Script Date:     03/07/2013 11:52:18 ******/
SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

CREATE FUNCTION [dbo].[fn_JSON_FormatArray](@value [xml], @label [nvarchar](4000))
RETURNS [xml] WITH EXECUTE AS CALLER
AS 
EXTERNAL NAME [CLRExtensions].[UserDefinedFunctions].[fn_JSON_FormatArray]
GO

EXEC sys.sp_addextendedproperty @name=N'SqlAssemblyFile', @value=N'JSON\fn_JSON_FormatArray.cs' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'FUNCTION',@level1name=N'fn_JSON_FormatArray'
GO

EXEC sys.sp_addextendedproperty @name=N'SqlAssemblyFileLine', @value=N'11' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'FUNCTION',@level1name=N'fn_JSON_FormatArray'
GO
Was it helpful?

Solution

I have now fixed this problem. The issue turned out to be that I was using the same SQLCommand later on to execute a stored procedure (as I have given the command a SQLTransaction so I can rollback if it fails) and had forgotten to remove the parameters that were added when I run the next text command. Therefore it was running the first script file and failing afterwards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top