Re: How to Interrogate SQL Server Tables for Specific Values - Here's how to do it in MS Access



On Mar 28, 10:35 pm, Tom van Stiphout <no.spam.tom7...@xxxxxxx> wrote:
On Fri, 28 Mar 2008 19:52:01 -0700 (PDT), robboll

<robb...@xxxxxxxxxxx> wrote:

You may want to read up on catalog views such as sys.objects and
sys.columns.
The creation of XYZ* tables would not be needed.

-Tom.



In MS Access I have a routine as follows that interrogates all the
tables in the database looking for a certain value.

I am looking for similar routine for SQL Server.  It needs to to be
run at the server and not link to the tables.

First the MS Access routine runs to create three tables.  XYZTables,
XYZFields, and XYZResults and populates XYZTables and XYZFields.
XYZFields is needed for the Interrogation routine that follows:

Public Sub DocumentTables()
   Dim db As DAO.Database, tbl As DAO.TableDef, fld As DAO.Field
   Dim rstTable As DAO.Recordset, rstField As DAO.Recordset
   Dim t As String, strQuery As String
   Dim strTableSet As String, strFieldSet As String

   strSQL = "CREATE TABLE XYZTables " & _
            "(TableName TEXT CONSTRAINT " & _
            " PrimaryKey PRIMARY KEY, " & _
            " TableRecords Number) "
            CurrentDb.Execute strSQL, dbFailOnError

   strSQL = "CREATE TABLE XYZFields " & _
            "(TableName CHAR, " & _
            "FieldName CHAR, " & _
            "DataType CHAR, " & _
            "DataSize Number, " & _
            "FieldDesc CHAR, " & _
            "SearchValue CHAR) "
            CurrentDb.Execute strSQL, dbFailOnError

   strSQL = "CREATE TABLE XYZResults " & _
           "(TableName CHAR, " & _
            "FieldName CHAR, " & _
            "DataType CHAR, " & _
            "DataSize Number, " & _
            "FieldDesc CHAR, " & _
            "SearchValue CHAR) "
            CurrentDb.Execute strSQL, dbFailOnError

   strTableSet = "xyzTables"
   strFieldSet = "xyzFields"

   DoCmd.SetWarnings False
   DoCmd.RunSQL "DELETE * FROM [" & strTableSet & "];"
   DoCmd.RunSQL "DELETE * FROM [" & strFieldSet & "];"
   DoCmd.SetWarnings True

   Set db = CurrentDb
   Set rstTable = db.OpenRecordset(strTableSet, dbOpenDynaset)
   Set rstField = db.OpenRecordset(strFieldSet, dbOpenDynaset)

   Debug.Print

   For Each tbl In db.TableDefs
       Debug.Print tbl.Name
       If tbl.Attributes = 0 Then
           With rstTable
               .AddNew
               .Fields("TableName") = tbl.Name
               .Fields("TableRecords") = tbl.RecordCount
               On Error Resume Next
               .Fields("TableDesc") =
tbl.Properties("Description").Value
               On Error GoTo 0
               .Update
           End With
           For Each fld In tbl.Fields
               'add new record for each field in each table,
containing
               'table, field, data type of field
               With rstField
                   .AddNew
                   .Fields("TableName").Value = tbl.Name
                   .Fields("FieldName").Value = fld.Name
                   .Fields("DataType").Value =
GetFieldDataType(fld.Type)
                   .Fields("DataSize").Value = fld..Size
                   On Error Resume Next
                   .Fields("FieldDesc").Value =
fld.Properties("Description").Value
                    On Error GoTo 0
                   .Update
               End With
           Next fld
       End If
   Next tbl

   Debug.Print

   rstField.Close
   rstTable.Close
   Set fld = Nothing
   Set tbl = Nothing
   Set db = Nothing
End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~­~

Function InterrogateDB()
On Error GoTo Err_Line

    Dim db As DAO.Database
    Dim rsXYZFields As DAO.Recordset
    Dim mTable As String
    Dim mField As String
    Dim strSQL As String
    Dim strFIND As String
    strFIND = InputBox("Enter the field name fragment:") ' This
prompts user for a value to search for.

    Set db = CurrentDb
   'Open the Table/Fields table

   Set rsXYZFields = db.OpenRecordset("xyzFields", dbOpenSnapshot)

             With rsXYZFields
             .MoveFirst
             Do Until .EOF
                   mTable = "[" & Trim(.Fields(0)) & "]"
                   mField = "[" & Trim(.Fields(1)) & "]"

                 If DCount("*", mTable, mField & " Like '*" & _
                       strFIND & "*'") > 0 Then
                   strSQL = "INSERT INTO xyzResults ( TableName, " &
_
                   "FieldName, SearchValue ) VALUES ( '" & mTable &
"', '" & _
                   mField & "', '" & strFIND & "' )"
                 db.Execute strSQL, dbFailOnError

             End If
             .MoveNext
             Loop
              End With
           rsXYZFields.Close
           Set rsXYZFields = Nothing
           db.Close
           Set db = Nothing
Exit Function

Err_Line:
MsgBox "Error occurred when inserting record"
Resume Next- Hide quoted text -

- Show quoted text -

You're right about the tables -- actually I'm really not sure why it's
in there at all. It's not used. xyzFields is the one that is used
and hat is definitely needed. But the effort here is to use SQL
Server exclusively on the same machine.


If you're really not sure what I am asking: Say you are looking for
the string "abc123" and you need to look in all the tables and columns
for that value. I have the routine to do so in Access as posted. I
am looking for a way to do it using SQL Server. Appriciate any
suggestions!

.



Relevant Pages

  • Re: Send Word Data to Excel
    ... Dim vConnection As New ADODB.Connection ... Dim oPath As String ... Dim FileArrayAs String ...    Exit Sub ...
    (microsoft.public.word.vba.general)
  • Re: Returning a value from another application
    ... of the variable warning from the excel module to Access ... Dim fName As String ...     For RCOUNT = RCOUNT To LastRow ...
    (microsoft.public.access.formscoding)
  • Re: redemption code doesnt work
    ... Is this supposed to be Outlook VBA code? ...     Set olApplication = Application ... Dim gnspNameSpace As Outlook.NameSpace ... Dim olMailItemBody As String ...
    (microsoft.public.outlook.program_vba)
  • Re: 11-string recuerdos- where on the bell curve?
    ...   Yes, a couple of the ornaments in the first section did not come out ... which there is not room for improvement, slurs included, but I do not ... This is my recording of Recuerdos done on six string several years ... a heavier bass? ...
    (rec.music.classical.guitar)
  • Re: Excel Chart Macro Issue
    ... If the issue is string length, why would forming a large string at the end ... Sometimes this means setting up a summary range that the chart uses. ... Dim rDataX As Range ...   Dim wsData As Worksheet ...
    (microsoft.public.excel.programming)