Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this SQL script failing?

I'm trying to run a SQL script against SQL Server 2005 as part of the set up for my integration tests. The script works perfectly fine if I execute it from within SQL Server Management Studio. However, when executing programmatically, this part of the script that enables full-text search refuses to work:

-- lots of tables and indexes created above

create unique nonclustered index [IX_Id] on [MyTable] ( [Id] asc )

-- this line fails
sp_fulltext_database enable
create fulltext catalog MyCatalog with accent_sensitivity = off as default
create fulltext index on MyTable([Title],[Content]) key index IX_Id with change_tracking auto

The error I get is:

Incorrect syntax near 'enable'.

If I remove the lines related to full text, everything works fine. If I change the sp_fulltext_database enable call to this:

EXEC ('sp_fulltext_database enable');

I get a different error:

Cannot use full-text search in master, tempdb, or model database.

This makes no sense to me at all. The correct database is in use.

For reference, the code that executes the script looks like this (I'm using NHibernate):

private static void InitializeDatabase()
{
    var sessionFactory = new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();

    using (var session = sessionFactory.OpenSession())
    {
        session.CreateSQLQuery("IF DB_ID('" + databaseName + @"') IS NOT NULL DROP DATABASE """ + databaseName + @""";").ExecuteUpdate();
        session.CreateSQLQuery(@"CREATE DATABASE """ + databaseName + @""";").ExecuteUpdate();
        session.CreateSQLQuery(@"USE """ + databaseName + @""";").ExecuteUpdate();
        session.CreateSQLQuery(GetCreationScript()).ExecuteUpdate();
    }
}

Can anyone tell me what I need to do to get this to work?

Thanks,
Kent

like image 661
Kent Boogaart Avatar asked Jul 26 '26 00:07

Kent Boogaart


1 Answers

sp_fulltext_database 'enable'

It's a string parameter, not a reserved word.

EDIT BY KENT: I marked as answer to give Quassnoi the rep. But to elaborate, if I add my USE database statement to the top of my script, it works. For reasons I don't understand, it doesn't work when I execute the USE database as a separate statement inside the same session (see my original code).

like image 87
Quassnoi Avatar answered Jul 27 '26 22:07

Quassnoi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!