User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 391,582 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,665 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 1076 | Replies: 1
Reply
Join Date: Mar 2008
Posts: 1
Reputation: sauronbrad is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sauronbrad sauronbrad is offline Offline
Newbie Poster

Create an extra table (copy the columns and new table) for every existing table

  #1  
Mar 28th, 2008
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:


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. ..
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Posts: 75
Reputation: manoshailu is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
manoshailu's Avatar
manoshailu manoshailu is offline Offline
Junior Poster in Training

Re: Create an extra table (copy the columns and new table) for every existing table

  #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:

  1. DECLARE @TABLE_NAME SYSNAME
  2. DECLARE @AUDIT_TABLE VARCHAR(50)
  3. DECLARE @SQL VARCHAR(100)
  4.  
  5. SELECT @TABLE_NAME= MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES
  6. WHERE
  7. TABLE_TYPE= 'BASE TABLE'
  8. AND TABLE_NAME NOT LIKE 'audit%'
  9. AND TABLE_NAME!= 'sysdiagrams'
  10. AND TABLE_NAME!= 'Audit'
  11. AND TABLE_NAME = 'sales'
  12.  
  13. WHILE @TABLE_NAME IS NOT NULL
  14. BEGIN
  15.  
  16. SELECT @TABLE_NAME= MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES
  17. WHERE TABLE_NAME> @TABLE_NAME
  18. AND TABLE_TYPE= 'BASE TABLE'
  19. AND TABLE_NAME!= 'sysdiagrams'
  20. AND TABLE_NAME!= 'Audit'
  21. --AND TABLE_NAME = 'sales'
  22.  
  23. SET @AUDIT_TABLE = 'Audit'+ @TABLE_NAME
  24. SELECT @AUDIT_TABLE
  25. SELECT @TABLE_NAME
  26.  
  27. ---Drop THE TABLE IF EXISTS
  28. SET @SQL ='DROP TABLE ' + @AUDIT_TABLE
  29. EXEC (@SQL)
  30.  
  31. --CREATE THE STRUCTURE OF THE TABLE FROM THE ANOTHER TABLE
  32. SET @SQL= 'SELECT * INTO ' + @AUDIT_TABLE + ' FROM ' + @TABLE_NAME
  33. EXEC (@SQL)
  34.  
  35. --REMOVE THE RECORDS IF EXISTS
  36. SET @SQL= 'TRUNCATE TABLE ' + @AUDIT_TABLE
  37. EXEC (@SQL)
  38.  
  39. --ADD COLUMNS TO THE TABLE
  40. SET @SQL= 'ALTER TABLE ' + @AUDIT_TABLE + ' ADD UserAction Char(10),AuditStartTime Char(50),AuditUser Char(50)'
  41. EXEC (@SQL)
  42.  
  43.  
  44. SELECT @TABLE_NAME= MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES
  45. WHERE TABLE_NAME> @TABLE_NAME
  46. AND TABLE_TYPE= 'BASE TABLE'
  47. AND TABLE_NAME!= 'sysdiagrams'
  48. AND TABLE_NAME!= 'Audit'
  49. AND TABLE_NAME NOT LIKE 'audit%'
  50.  
  51.  
  52.  
  53. END
Have a nice Day
Shailaja
Last edited by peter_budo : Apr 10th, 2008 at 4:19 pm. Reason: Keep It Organized - please use [code] tags
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 10:56 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC