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.

有帮助吗?

解决方案 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);

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top