• GilaMonster - Saturday, March 3, 2018 9:05 AM

    dastagiri16 - Saturday, March 3, 2018 8:43 AM

    is trigger  blocking my table updation from gui .is it expected behavior of triggers ?

    No.

    It's the behaviour of your specific trigger, which implies that there's a problem with your trigger, but without the code for your trigger, that's all that anyone can say.

    Hi Team,

    this is my source table syntax

    USE [test1]

    GO

    /****** Object: Table [dbo].[test1]  Script Date: 04/03/2018 9:48:14 AM ******/
    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    CREATE TABLE [dbo].[test1](
        [id] [int] NOT NULL,
        [name] [nchar](10) NULL
    ) ON [PRIMARY]

    GO

    I have created one procedure to create automatically one audit table in destination db and create trigger in source table.

    USE [test1]
    GO
    /****** Object: StoredProcedure [dbo].[usp_Common_Audit]  Script Date: 04/03/2018 9:47:37 AM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    ALTER procedure [dbo].[usp_Common_Audit](@TableName nvarchar(1000),@AuditDBName nvarchar(1000),@SourceDBName nvarchar(1000) )

    as

    begin

    if ((@SourceDBName<>'' or @SourceDBName is not null ) and (@TableName<>'' or @TableName is not null ) and (@AuditDBName<>'' or @AuditDBName is not null))
    begin

    set nocount on;
    SET XACT_ABORT ON;

    BEGIN TRY
    BEGIN TRANSACTION
    -- create this procedure in source server.
    declare @COUNT int
    declare @CNT int
    declare @tcount int
    declare @tCNT int

    -- declare @SourceDBName nvarchar(1000)=''
    -- declare @AuditDBName nvarchar(1000) =''
    -- declare @TableName nvarchar(1000)=''
    declare @Chktblname nvarchar(1000)=''
    declare @InsTbl nvarchar(1000)=''
    DECLARE @AddClms nvarchar(1000)=''
    DECLARE @TrName nvarchar(1000)=''
    declare @TrCheck nvarchar(1000)=''
    set @Chktblname='select @CNT=count(*) From '+@AuditDBName+'.'+'INFORMATION_SCHEMA.TABLES where TABLE_NAME='''+@TableName+'_Audit'+''''
    -- SELECT @Chktblname
    EXECUTE SP_EXECUTESQL @Chktblname ,N'@CNT INT OUTPUT',@CNT=@COUNT OUTPUT

    if( @count) =0
    begin

    set @InsTbl='SELECT * into '+@AuditDBName+'.dbo.'+@TableName+'_Audit ' + 'FROM '+@SourceDBName+'.DBO.'+@TableName +' WHERE 1<>1'
    EXEC( @InsTbl)
    set @AddClms='ALTER TABLE '+@AuditDBName+'.dbo.'+@TableName+'_Audit'+
    ' ADD UserName nvarchar(128) NULL,
        HostName nvarchar(128) NULL,
        AppName nvarchar(1000) NULL,
        UpdateDate datetime DEFAULT (getdate()),
        [Type] [nvarchar](100) NULL,
        AuditID bigint IDENTITY(1,1) NOT NULL'
        exec(@AddClms)
    end

     set @TrCheck='select @tCNT=count(*) From '+@SourceDBName+'.'+'sys.objects where TYPE=''TR'' AND NAME='+'''tr_'+@TableName+'''' EXECUTE SP_EXECUTESQL @TrCheck ,N'@tCNT INT OUTPUT',@tCNT=@tCOUNT OUTPUT
     

    if( @tCOUNT)=0
    begin

    set @TrName=
    'CREATE Trigger '+'tr_'+@TableName+
    ' ON' +' DBO.'+@TableName +' AFTER INSERT ,UPDATE,DELETE
    as
    begin

    declare @Action as char(1)
    set @Action= (case when exists (select * from inserted) and exists (select *From deleted) then ''U''
          when exists (select *From inserted) then ''I''
                 when exists (select *From deleted) then ''D''
             else null end)

    if @Action =''U''
    begin
    insert into '+@AuditDBName+'.dbo.'+@TableName+'_Audit '+' select *,system_user,host_name(),APP_NAME(),getdate(),''Old_Version'' From deleted
    insert into '+@AuditDBName+'.dbo.'+@TableName+'_Audit '+' select *,system_user,host_name(),APP_NAME(),getdate(),''New-Version'' From inserted
    end
    else if @Action =''I''
    begin
    insert into '+@AuditDBName+'.dbo.'+@TableName+'_Audit '+' select *,system_user,host_name(),APP_NAME(),getdate(),''Insert'' From inserted
    end
    else if @Action =''D''
    begin
    insert into '+@AuditDBName+'.dbo.'+@TableName+'_Audit '+' select *,system_user,host_name(),APP_NAME(),getdate(),''Delete'' From Deleted
    end
    end'

    exec(@TrName)

    end
    COMMIT TRANSACTION

    END TRY

    BEGIN CATCH
        IF @@TRANCOUNT>0    
         SELECT ERROR_NUMBER() AS 'ErrNo',ERROR_MESSAGE() as 'ErrMsg',ERROR_SEVERITY() as 'ErrSeverity',ERROR_STATE() as 'Err_State';
            ROLLBACK TRANSACTION;
    END CATCH

    END

    else begin

    Print 'Provide Correct Parameters'
    end
    end

    steps to execute:
    first we need to create table the we have to execute below command.
    test1 is table name
    we have to pass sourcedb and destinaiondb in the below procedure..

    exec usp_Common_Audit 'test1','sourceDbname','destinationdbname'