14 feb 2012

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint

ADVANCED VERSION WITH GENERATING THE RIGHT DELETE STATEMENTS



BEGIN
-- The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__". The conflict occurred in database "", table "", column ''.
-- THIS SCRIPT DOESNT EXECUTE DELETE STATEMENTS IT ONLY GENERATES THE SQL STATEMENTS

declare @Subtable as nvarchar(100), @roottable as nvarchar(100),  @idColumn as nvarchar(100)

set @Subtable=''
set @Roottable=''
set @idColumn=''

declare @SQLStatement as nvarchar(MAX) , @SQLStatement1 as nvarchar(MAX) ,@SQLStatement2 as nvarchar(MAX) ,@SQLStatement3 as nvarchar(MAX) ,
 @orderstat as nvarchar(MAX) ,  @wherestat as nvarchar(MAX)
SET @orderstat = ' ORDER BY ' + @idColumn
SET @SQLStatement = 'SELECT TOP 1 @ItemId = ' + @idColumn + ' FROM ' + @Subtable + ' as st ' + ' where not exists(select *  from ' + @Roottable + ' as rt where st.' + @idColumn + '=rt.' + @idColumn + ')and ' + @idColumn + ' is not null'
SET @SQLStatement2 = 'SELECT TOP 1 ' + @idColumn + ' FROM ' + @Subtable + ' as st ' + ' where not exists(select *  from ' + @Roottable + ' as rt where st.' + @idColumn + '=rt.' + @idColumn + ')and ' + @idColumn + ' is not null'
SET @SQLStatement3 = ' '' DELETE FROM ' + @Subtable + ' WHERE ' + @idColumn + '= '' + CONVERT(NVARCHAR(10),@ItemId) '
SET @SQLStatement1 = '
BEGIN
Declare @ItemId bigint
 ' + @SQLStatement + @orderstat + '
 PRINT ' + @SQLStatement3 + '
  WHILE @ItemId is not null 
  BEGIN
     if exists (' + @SQLStatement2 + '   AND   ' + @idColumn + '>@ItemId '+ @orderstat + ')
                  begin                                                       
                        ' + @SQLStatement + '  and ' + @idColumn + '>@ItemId  '+ @orderstat + '
                        PRINT ' + @SQLStatement3 + '
                  end
                  else
                  begin
                        set @ItemId=null
                  end
  END
END' 

exec sp_executesql @SQLStatement1
END

DELETE ALL THE CONTENT OF A DATABASE

 Cannot truncate table '' because it is being referenced by a FOREIGN KEY constraint.


EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'   
EXEC sp_MSForEachTable 'ALTER TABLE ? DISABLE TRIGGER ALL'   
EXEC sp_MSForEachTable 'DELETE FROM ?'
--EXEC sp_MSForEachTable 'TRUNCATE TABLE ?
-- You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint;
-- instead, use DELETE statement without a WHERE clause.
-- Because TRUNCATE TABLE is not logged, it cannot activate a trigger."
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'   
EXEC sp_MSForEachTable 'ALTER TABLE ? ENABLE TRIGGER ALL'
-- reseed identity columns
EXEC sp_MSForEachTable '
    IF OBJECTPROPERTY(object_id(''?''), ''TableHasIdentity'') = 1
    DBCC CHECKIDENT (''?'', RESEED, 0)'

10 feb 2012

example of working with custom attributes on properties in c#

using System.Reflection;
public sealed class MyAttribute : Attribute
    {
        private bool _MYbool = true;

        public bool MYbool
        {
            get { return __MYbool; }
            set { __MYbool= value; }
        }
        public MyAttribute()
        {
        }
        public MyAttribute(Boolean value)
        {
            _MYbool = value;
        }
    }





/// This Routine checks that the CalculationAttribute is Set or not
/// Put this function in a public static Class
/// 
/// the name of the object
/// the propertyname where to check on
/// 
public static Boolean PropertyCheckTo(this object SourceObj, string PropertyName)
 {
   PropertyInfo[] SourceProps = SourceObj.GetType().GetProperties();
   foreach (PropertyInfo pi in SourceProps)
   {
     if (pi.Name == PropertyName)
       {
       MyAttribute att = (MyAttribute )Attribute.GetCustomAttribute(pi, typeof(MyAttribute ));
         if (att != null)
         {
            return true;
         }
        }                
    }
    return false;
  }