Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL
Expertise: Intermediate
Jun 23, 2003

A Useful Retrieval Procedure

This procedure retrieves the table name, table id, column or fieldname, data type, the length of the field in bytes, and the order in which the fields were created. I use it as a data source for my MS Access utility. I also use it to get needed project development info.

To use it immediately, copy and paste the following to SQL Server Query Analyzer, choose the database you want to use and press Execute Query (F5):


SELECT SO.name as [Table],SO.id as [Table ID],SC.name AS
[Fieldname],Sc.xtype as  [Storage Type],ST.name AS [Data
Type],SC.Length,colorder as [Order] FROM sysobjects  SO INNER JOIN
syscolumns SC ON SO.id=SC.id LEFT JOIN systypes ST ON
SC.xtype=ST.xusertype WHERE SO.xtype='U' ORDER BY SO.name,SC.name
To use this for Access Data Projects, use this procedure in the RecordSource property of a form and run it. For example, place this code in your main form:

Dim frm as Form 'at the topmost (General) portion of the code

Private Sub cmdListFields_Click()  'based on a cmdListFields button in the
Main Form
On Error GoTo ErrHandler
    Dim strTable As String
    Dim sQRY As String
    Set frm = New Form_ListForm 'use the name of the form you created to
list the Recordset
    strTable = InputBox("Please enter Table name: ", "Needs a Name",
"(Default Table)")
   If Len(strTable) > 0 Then
       sQRY = "SELECT SO.Name as [Table],SO.id as [Table ID],SC.Name AS
[Fieldname], "
       sQRY = sQRY & "Sc.xtype as [Storage Type],ST.name AS [Type],"
       sQRY = sQRY & "SC.Length,colorder as [Order] FROM sysobjects SO "
       sQRY = sQRY & "INNER JOIN syscolumns SC ON SO.id=SC.id LEFT JOIN
systypes ST "
       sQRY = sQRY & "ON SC.xtype=ST.xusertype WHERE SO.xtype='U' "
       sQRY = sQRY & " AND SO.Name = '" & strTable
       sQRY = sQRY & "' ORDER BY SC.name"

        frm.RecordSource = sQRY
        frm.Visible = True
        frm.SetFocus
    Else
         MsgBox "No table name provided."
    End If
Exit Sub
ErrHandler:
    MsgBox "Error: " & Err.Description
    Err.Clear
End Sub
Arsenio Tacardon
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap