SQL Learning Center - GoldMine

Use SQL queries to get answers from GoldMine

May 18, 2008

Create table with GoldMine Field Usage Counts

by @ 1:46 pm. Filed under MSSQL Query Analyzer

Description:
Run this only in Query Analzyer to create a table called ‘gm_userdef_usage_count’ which holds a row for each field and a count for how many times it has been used.

  1.  
  2. IF EXISTS(SELECT name FROM sysobjects WHERE name = 'gm_userdef_usage_count' AND type = 'U')
  3.      DROP TABLE gm_userdef_usage_count
  4.        
  5. CREATE TABLE gm_userdef_usage_count
  6. (    FieldName    VARCHAR(50),
  7.     RecCount    INT    )
  8.        
  9. --Declare cursor
  10. DECLARE curs CURSOR
  11. GLOBAL
  12. SCROLL
  13. STATIC
  14. FOR
  15. SELECT Field_Name,Field_Type FROM contudef WHERE dbfname ='CONTACT2' and field_name like 'U%'
  16. go
  17.        
  18. --Declare variables
  19. DECLARE @fldname VARCHAR(50)
  20. DECLARE @fldtype VARCHAR(1)
  21. DECLARE @temp VARCHAR(150)
  22. DECLARE @MAX INT
  23. DECLARE @counter INT
  24.        
  25. SET @counter = 1
  26. SELECT @MAX = COUNT(field_name) FROM contudef WHERE dbfname ='CONTACT2' and field_name like 'U%'
  27. --Open cursor and fetch record
  28. OPEN curs
  29. FETCH NEXT FROM curs INTO @fldname,@fldtype
  30.        
  31. --loop through them all
  32. WHILE @counter < = @MAX
  33. BEGIN
  34.     SELECT @counter = @counter+1
  35.     --print @fldname
  36.     IF @fldtype = 'C' or  @fldtype = 'D'
  37.         SET @temp = 'Insert into gm_userdef_usage_count Select ' + CHAR(39) + @fldname + CHAR(39) + ', count(accountno) from contact2 where ' + @fldname + ' is not null '
  38.     ELSE
  39.         SET @temp = 'Insert into gm_userdef_usage_count Select ' + CHAR(39) + @fldname + CHAR(39) + ', count(accountno) from contact2 where ' + @fldname + ' = 0 '
  40.     EXEC(@temp)
  41.     FETCH NEXT FROM curs INTO @fldname,@fldtype
  42. END
  43.        
  44. CLOSE curs
  45.        
  46. DEALLOCATE curs
  47.  
Bookmark:

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Live
  • Google
  • Facebook
  • bodytext
  • StumbleUpon
  • Slashdot

Comments are closed.

GoldMine SQL Tutorial:

General SQL Tutorials:

SQL Help Links:

Recommended Reading - SQL Books:

RSS:

Links:

Search For SQL Query:

GoldMine MS SQL Queries:

GoldMine dBase SQL Queries (LocalSQL):

22 queries. 0.104 seconds