MSSQL Column Names

Tim Golden tim.golden at viacom-outdoor.co.uk
Mon Feb 9 10:56:53 EST 2004


>From: Lindstrom Greg - glinds [mailto:Greg.Lindstrom at acxiom.com]
>
>1.  Is there a way to search the archives?  I can pull the 
>archives up, but
>can't find a way to search for the answer to my next question.

http://groups.google.com/groups?q=comp.lang.python

>
>2.  Using MSSQL-0.06, Python 2.3 and Windows, how can I pull the column
>names for a given table.

You have a few options. I usually do this (SQL):

<sql>

SELECT 
  COLUMN_NAME 
FROM 
  INFORMATION_SCHEMA.COLUMNS 
WHERE 
  TABLE_NAME = 'ui_menu_items' 
ORDER BY 
  ORDINAL_POSITION  

</sql>

which, in python would be something like this:

<python>

import MSSQL

db = MSSQL.connect ("VODEV1", "", "", "EVODEV")
q = db.cursor ()
q.execute ("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_NAME = 'ui_menu_items' ORDER BY ORDINAL_POSITION")
for row in q.fetchall ():
  print row[0]

</python>

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________




More information about the Python-list mailing list