Question

I have a stored procedure in SQL Server 2008 CLR project. This function call a console application with a parameter that contain some xml.

[Microsoft.SqlServer.Server.SqlProcedure()]
public static SqlInt32 ExecuteApp(SqlString utilPath, SqlString arguments, ref SqlString result)  
{
    ProcessStartInfo info  = new ProcessStartInfo("cmd.exe");
    Process p = new Process();

    try
    {
        //ProcessStartInfo to run the DOS command attrib

        info.RedirectStandardOutput = true;
        info.RedirectStandardError = true;
        info.UseShellExecute = false;
        info.CreateNoWindow = true;
        info.Arguments = string.Format(@"/c ""{0}"" {1}", utilPath, arguments);
        SqlContext.Pipe.Send(info.Arguments);

        p.StartInfo = info;
        p.Start();
        String processResults = p.StandardOutput.ReadToEnd();
        SqlContext.Pipe.Send(processResults);
        result = processResults;
        if (String.IsNullOrEmpty((String)result))
            result = p.StandardError.ReadToEnd();

        return 1;
    }
    finally
    {
        p.Close();
    }
}

The problem here with param that contain xml. This is a expression for execute stored procedure:

    declare @Result nvarchar(max)
exec ExecuteApp 'C:\Console.exe', 'send "<messages><message>my message</message></messages>"', @Result out 
select @Result

After executing stored procedure I have got an error like:

< was unexpected at this time.

I want to noticed that without xml all is working properly.

Was it helpful?

Solution

Your XML contains special characters which are interpreted by the command shell (> and <).
You need to quote the parameter and escape quotes inside of it with "".

Also, you should execute your program directly (not through cmd /c); that will resolve some of your issues.

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