Question

I am trying to execute a sql script read from a file; however, when I go to execute the script I am met with the exception:

Column, parameter, or variable #1: Cannot find data type dbo.ExistingTableType.
Must declare the table variable "@existingTableType".
Parameter or variable '@existingTableType' has an invalid data type.

I have tried testing the script by copying the text and running it in SQL server management studio and it does run successfully. Additionally, other scripts are able to run but for some reason those with a user-defined table type as a parameter are not. Below is a simple example:

C#

string script = scriptFile.OpenText().ReadToEnd();
SqlConnection connection = new SqlConnection(connectionString);
Server server = new Server(new ServerConnection(connection));
server.ConnectionContext.StatementTimeout = 100;
server.ConnectionContext.ExecuteNonQuery(script);

SQL

CREATE PROCEDURE [dbo].[failedStoredProcedure]
(
    @existingTableType dbo.ExistingTableType READONLY
)
AS

-- Do something here

GO

Other tests done:

  • Tried another user in connection string.

Edit: Additionally, when scripts run it returns 'Invalid Object Name {x}' when the object does exist.

Was it helpful?

Solution 2

It seems that at some point something happens to the SMO object where it no longer applies to the specified database, despite the database name being the intended object.

To correct this I created a new database object and applied the scripts from there:

Server server = new Server();
server.ConnectionContext.ConnectionString = connectionString;
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
Database db = server.Databases[builder.InitialCatalog];

Then later instead run

db.ExecuteNonQuery(script);

OTHER TIPS

I tried your code on my end (SQL 2012 Express) and it worked just fine. Does the sql account has grants to the type?

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