Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Programming / CLR / August 2008

Tip: Looking for answers? Try searching our database.

GO in Clr code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dan Holmes - 18 Aug 2008 19:00 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
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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.