Showing posts with label SQL Server 2005. Show all posts
Showing posts with label SQL Server 2005. Show all posts

Thursday, 28 January 2010

NHibernate.HibernateException: Incorrect syntax for definition of the 'TABLE' constraint.

While using Spring.NET and NHibernate on a ASP.NET web application I came across the following error:

System.Data.SqlClient.SqlException: Incorrect syntax for definition of the 'TABLE' constraint.
NHibernate.HibernateException: Incorrect syntax for definition of the 'TABLE' constraint.

The problem was that I was trying to name a column in a SQL Server database as "References", which must be a protected name because it didn't like it. When I changed my code to call the column "Refs" instead the problem went away.

[Property(Name = "Refs", Column = "References", Length = 4001)]
//Length 4001 is equivalent to nvarchar(MAX)
public virtual string Refs { get; protected internal set; }

Friday, 30 October 2009

Truncate all database tables in a SQL Server database

The following is pretty useful for completely emptying a SQL Server database. I've tested it for SQL Server 2005.

-- disable referential integrity
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO

EXEC sp_MSForEachTable '
IF OBJECTPROPERTY(object_id(''?''), ''TableHasForeignRef'') = 1
DELETE FROM ?
else
TRUNCATE TABLE ?'
GO

-- enable referential integrity again
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO