Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
mfdoom
Starting Member
10 Posts |
Posted - 2006-03-21 : 12:36:39
|
all, i was using the following audit trigger on my inventory table:DECLARE @ACT CHAR(6)DECLARE @DEL BITDECLARE @INS BIT DECLARE @SQLSTRING VARCHAR(2000)SET @DEL = 0SET @INS = 0IF EXISTS (SELECT TOP 1 1 FROM DELETED) SET @DEL=1IF EXISTS (SELECT TOP 1 1 FROM INSERTED) SET @INS = 1 IF @INS = 1 AND @DEL = 1 SET @ACT = 'UPDATE'IF @INS = 1 AND @DEL = 0 SET @ACT = 'INSERT'IF @DEL = 1 AND @INS = 0 SET @ACT = 'DELETE'IF @INS = 0 AND @DEL = 0 RETURNIF NOT EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID(N'[DBO].[AUDIT_inventory]') AND OBJECTPROPERTY(ID, N'ISUSERTABLE') = 1)BEGIN -- CREATE A MEMORY TABLE CONTAINING THE FIELDS AND TYPES OF THE TABLE DECLARE @MEMTABLE TABLE ( ID INT IDENTITY ,COLUMNAME SYSNAME ,TYPENAME VARCHAR(20) ) -- INSERT THE COLUMNAMES AND THE DATATYPES INSERT @MEMTABLE (COLUMNAME,TYPENAME) SELECT NAME,TYPE_NAME(XTYPE) FROM SYSCOLUMNS WHERE ID = OBJECT_ID('[DBO].[inventory]') ORDER BY COLID DECLARE @CUR INTEGER DECLARE @MAX INTEGER DECLARE @SQLSTR AS VARCHAR(8000) DECLARE @CURCOL SYSNAME DECLARE @COLTYPE AS VARCHAR(10) -- SETUP VARIABLES SET @SQLSTR = '' SET @CUR=1 SELECT @MAX = MAX(ID) FROM @MEMTABLE -- LOOP EVEY FIELD WHILE @CUR <= @MAX BEGIN -- GET VALUES FROM THE MEMTABLE SELECT @CURCOL = COLUMNAME,@COLTYPE = TYPENAME FROM @MEMTABLE WHERE ID = @CUR IF @COLTYPE = 'INT' OR @COLTYPE = 'BIGINT' OR @COLTYPE='UNIQUEIDENTIFIER' -- WE DO WANT TO COPY INT/BIGINT/UNIQUEIDENTIFIER FIELDS BUT IF THEY ARE AN -- IDENTITY OR A ROWGUIDCOLUMN WE DO NOT WANT TO COPY THOSE ATTRIBUTES SET @SQLSTR = @SQLSTR + ' CAST(' + @CURCOL + ' AS ' + @COLTYPE + ') AS ['+ @CURCOL + '] 'ELSE -- ANOTHER FIELD DO NOTHING JUST COPY IT AS IT IS SET @SQLSTR = @SQLSTR + ' ' + @CURCOL + @CURCOL + ' AS ['+ @CURCOL +'] ' IF @CUR <= @MAX - 1 SET @SQLSTR=@SQLSTR + @SQLSTR + ',' SET @CUR = @CUR + 1 END -- ADD THE AUDIT FIELDSSET @SQLSTR = @SQLSTR +',CAST('' '' AS CHAR(6)) AS TRG_ACTION,CAST(GETDATE() AS DATETIME) AS TRG_DATE,system_user as TRG_USER'-- SET UP THE SELECT FOR CREATING THE AUDIT TABLESET @SQLSTR = 'SELECT TOP 0' + @SQLSTR + 'INTO [DBO].[AUDIT_inventory] FROM [DBO].[inventory]' EXEC(@SQLSTR)ENDIF @ACT = 'INSERT' INSERT [DBO].[AUDIT_inventory] SELECT *,'INSERT', getdate() , system_user FROM INSERTEDIF @ACT = 'DELETE' INSERT [DBO].[AUDIT_inventory] SELECT *,'DELETE', getdate() , system_user FROM DELETEDIF @ACT = 'UPDATE' INSERT [DBO].[AUDIT_inventory] SELECT *,'UPDATE', getdate() , system_user FROM INSERTEDand i get the following error:Msg 213, Level 16, State 1, Procedure TRG_inventory, Line 88Insert Error: Column name or number of supplied values does not match table definition.i have no idea, any help would be appreciated. |
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2006-03-21 : 12:53:38
|
very simple.1. What is line 882. What the error message basically means is that the values you are trying to insert are either different from what you have in your table or more/less than what in your tableI would advice you search out line 88 and post your table DDL structure hereAfrika |
 |
|
mfdoom
Starting Member
10 Posts |
Posted - 2006-03-21 : 13:04:09
|
no problem:-- ADD THE AUDIT FIELDSSET @SQLSTR = @SQLSTR +',CAST('' '' AS CHAR(6)) AS TRG_ACTION,CAST(GETDATE() AS DATETIME) ASTRG_DATE,system_user as TRG_USER'-- SET UP THE SELECT FOR CREATING THE AUDIT TABLESET @SQLSTR = 'SELECT TOP 0' + @SQLSTR + 'INTO [DBO].[AUDIT_inventory] FROM [DBO].[inventory]'EXEC(@SQLSTR) <- line 88ENDIF @ACT = 'INSERT' INSERT [DBO].[AUDIT_inventory] SELECT *,'INSERT', getdate() , system_user FROM INSERTEDIF @ACT = 'DELETE' INSERT [DBO].[AUDIT_inventory] SELECT *,'DELETE', getdate() , system_user FROM DELETEDIF @ACT = 'UPDATE' INSERT [DBO].[AUDIT_inventory] SELECT *,'UPDATE', getdate() , system_user FROM INSERTEDDDL structure for audit_inventory table:SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[AUDIT_inventory]( [ID] [int] NULL, [name] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [type] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [TRG_ACTION] [char](6) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [TRG_USER] [nvarchar](128) COLLATE SQL_Latin1_General_CP1_CI_AS NULL) ON [PRIMARY]GO |
 |
|
mfdoom
Starting Member
10 Posts |
Posted - 2006-03-21 : 13:08:26
|
yeah, i just answered my own questionmodified the audit_inventory table to include trg_datethanks for pointing me in the right direction though |
 |
|
mfdoom
Starting Member
10 Posts |
Posted - 2006-03-21 : 13:11:43
|
scratch my previous post, i just broke it more, any info would be appreciated. |
 |
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2006-03-21 : 13:14:04
|
were is line 88 ? |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-03-21 : 13:25:22
|
This is obviously code for a trigger since you refer to inserted and deleted tables yet you didn't not post the entire trigger code (there is no create trigger statement or GO statment).I don't understand why you are using information_schema views to assemble a dynamic sql statement. This code is obviously custom to the inventory table so why not just code non-dynamic sql to perform the insert to the audit table? Also, it seems like you are creating the audit table from within the trigger code??? How does that work (more than once)?EDIT:Ok, I see now that most of this code is to create your audit table if it doesn't exist. But I still think even the check for its existance is additional overhead you don't need considering it's checking on every insert, update, and delete.Be One with the OptimizerTG |
 |
|
mfdoom
Starting Member
10 Posts |
Posted - 2006-03-21 : 13:28:47
|
actually afrika, you helped me out alot, i modified the audit_inventory table include another column for the timestamp so its working now.thanks so much for your help! |
 |
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2006-03-21 : 16:11:37
|
Glad to help |
 |
|
|
|
|
|
|