| | |
Create an extra table (copy the columns and new table) for every existing table
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 1
Reputation:
Solved Threads: 0
Hi all, please help. I m trying to create an "empty" table from existing table for the audit trigger purpose.
For now, i am trying to create an empty audit table for every table in a database named "pubs", and it's seem won't work.
Please advise.. Thanks in advance.
Here is my code:
Thanks. ..
For now, i am trying to create an empty audit table for every table in a database named "pubs", and it's seem won't work.
Please advise.. Thanks in advance.
Here is my code:
MS SQL Syntax (Toggle Plain Text)
USE pubs DECLARE @TABLE_NAME sysname DECLARE @AUDIT_TABLE VARCHAR(50) SELECT @TABLE_NAME= MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE= 'BASE TABLE' AND TABLE_NAME NOT LIKE 'audit%' AND TABLE_NAME!= 'sysdiagrams' AND TABLE_NAME!= 'Audit' AND TABLE_NAME = 'sales' WHILE @TABLE_NAME IS NOT NULL BEGIN SELECT @TABLE_NAME= MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME> @TABLE_NAME AND TABLE_NAME = 'sales' SELECT @AUDIT_TABLE = 'Audit'+''@TABLE_NAME'' SELECT * INTO @AUDIT_TABLE FROM @TABLE_NAME TRUNCATE TABLE @AUDIT_TABLE ALTER TABLE @AUDIT_TABLE ADD UserAction Char(10),AuditStartTime Char(50),AuditUser Char(50) SELECT @TABLE_NAME= MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME> @TABLE_NAME AND TABLE_TYPE= 'BASE TABLE' AND TABLE_NAME!= 'sysdiagrams' AND TABLE_NAME!= 'Audit' AND TABLE_NAME NOT LIKE 'audit%' END
Thanks. ..
Re: Create an extra table (copy the columns and new table) for every existing table
0
#2 Apr 10th, 2008
hi,
Your code is right but u had used the @TABLE_NAME AND @AUDIT_TABLE. Both the variables are not the Table type so it wont allow to create the table to your requirement. Store the query in a string variable and have to execute.
Try the Below Coding:
Have a nice Day
Shailaja
Your code is right but u had used the @TABLE_NAME AND @AUDIT_TABLE. Both the variables are not the Table type so it wont allow to create the table to your requirement. Store the query in a string variable and have to execute.
Try the Below Coding:
sql Syntax (Toggle Plain Text)
DECLARE @TABLE_NAME SYSNAME DECLARE @AUDIT_TABLE VARCHAR(50) DECLARE @SQL VARCHAR(100) SELECT @TABLE_NAME= MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE= 'BASE TABLE' AND TABLE_NAME NOT LIKE 'audit%' AND TABLE_NAME!= 'sysdiagrams' AND TABLE_NAME!= 'Audit' AND TABLE_NAME = 'sales' WHILE @TABLE_NAME IS NOT NULL BEGIN SELECT @TABLE_NAME= MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.Tables WHERE TABLE_NAME> @TABLE_NAME AND TABLE_TYPE= 'BASE TABLE' AND TABLE_NAME!= 'sysdiagrams' AND TABLE_NAME!= 'Audit' --AND TABLE_NAME = 'sales' SET @AUDIT_TABLE = 'Audit'+ @TABLE_NAME SELECT @AUDIT_TABLE SELECT @TABLE_NAME ---Drop THE TABLE IF EXISTS SET @SQL ='DROP TABLE ' + @AUDIT_TABLE EXEC (@SQL) --CREATE THE STRUCTURE OF THE TABLE FROM THE ANOTHER TABLE SET @SQL= 'SELECT * INTO ' + @AUDIT_TABLE + ' FROM ' + @TABLE_NAME EXEC (@SQL) --REMOVE THE RECORDS IF EXISTS SET @SQL= 'TRUNCATE TABLE ' + @AUDIT_TABLE EXEC (@SQL) --ADD COLUMNS TO THE TABLE SET @SQL= 'ALTER TABLE ' + @AUDIT_TABLE + ' ADD UserAction Char(10),AuditStartTime Char(50),AuditUser Char(50)' EXEC (@SQL) SELECT @TABLE_NAME= MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.Tables WHERE TABLE_NAME> @TABLE_NAME AND TABLE_TYPE= 'BASE TABLE' AND TABLE_NAME!= 'sysdiagrams' AND TABLE_NAME!= 'Audit' AND TABLE_NAME NOT LIKE 'audit%' END
Shailaja
Last edited by peter_budo; Apr 10th, 2008 at 5:19 pm. Reason: Keep It Organized - please use [code] tags
![]() |
Other Threads in the MS SQL Forum
- Previous Thread: add a new table to the database or add a new column to an existing table.
- Next Thread: Urgent Help Needed
| Thread Tools | Search this Thread |
connectingtodatabaseinuse count cursor data database datepart deadlock delete_trigger highperformancecomputing hpc hpcserver2008 ibm iis loop maximum microsoft ms mssql multithreading news number permission query reporting result server services sets source sql sqlserver sqlserver2005 supercomputing uniqueid update view





