I have a small CLR proc that i use to execute SQL from a file. If the SQL has "GO" in it as it would if SSMS generated
it, it fails with:
Msg 6522, Level 16, State 1, Procedure ExecuteSQLFromFile, Line 0
A .NET Framework error occurred during execution of user-defined routine or aggregate "ExecuteSQLFromFile":
System.Data.SqlClient.SqlException: Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
System.Data.SqlClient.SqlException:
at StoredProcedures.ExecuteSQLFromFile(SqlString fileName)
.
if i replace the "GO"s with a ";" then it fails with:
Msg 111, Level 15, State 1, Procedure asdf, Line 7
'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.
How can i make this work?
Here is the proc and the SQL in the file.
IF OBJECT_ID('asdf') IS NOT NULL
DROP PROC asdf
;
CREATE PROC asdf
AS
PRINT 'HEllo'
;
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void ExecuteSQLFromFile(SqlString fileName)
{
try
{
using (SqlConnection conn = new SqlConnection("context connection=true"))
{
conn.Open();
SqlCommand cmd = new SqlCommand(System.IO.File.ReadAllText(fileName.ToString()), conn);
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
ex.Data.Add("filename", fileName);
throw ex;
}
}
};
Niels Berglund - 18 Aug 2008 20:30 GMT
> I have a small CLR proc that i use to execute SQL from a file. If the
> SQL has "GO" in it as it would if SSMS generated
[quoted text clipped - 7 lines]
> System.Data.SqlClient.SqlException:
> at StoredProcedures.ExecuteSQLFromFile(SqlString fileName)
GO is not a T-SQL command, it is a command that Management Studio
uses to differentiate batches. So, if you have some text that is
being executed against SQL Server containing GO, you'll get that
exception.
What exactly are you trying to do, apart from creating a proc?
Niels
Dan Holmes - 18 Aug 2008 21:35 GMT
>> I have a small CLR proc that i use to execute SQL from a file. If the
>> SQL has "GO" in it as it would if SSMS generated
[quoted text clipped - 16 lines]
>
> Niels
Execute whatever sql is in the specified file.