Whing Dela Cruz
asked on
Fields on the table
Hi experts, I wish to know how can I retrieve the existing field in a certain table. I have an existing table named DTR, I want to know what are the fields inside on it by using sql commands. Is it possible?
For a table of columns and their types...
In Oracle:
In Oracle:
SELECT
*
FROM
DBA_TAB_COLUMNS
WHERE
OWNER = '[schemaname]'
AND TABLE_NAME = '[tablename]'
ORDER BY
COLUMN_ID;
In Microsoft SQL Server:SELECT
[sys].[columns].*
FROM
[sys].[columns]
INNER JOIN
[sys].[tables] ON
[sys].[tables].[object_id] = [sys].[columns].[object_id]
INNER JOIN
[sys].[schemas] ON
[sys].[schemas].[schema_id] = [sys].[tables].[schema_id]
WHERE
[sys].[schemas].[name] = '[schemaname]'
AND [sys].[tables].[name] = '[tablename]'
ORDER BY
[column_id]
If you're using something other than one of those two you'll need to specify the database platform being used.
ASKER
Hi experts, I am using Microsoft SQL Server 2008
Then the 2nd query in my above example will give you what you want.
ASKER
Thanks PortletPaul
ASKER
Hi Russ Suter,
I wish to use this approach, Set rs = cn.Execute("Select * from DTR") and obtain the list of fields inside DTR. Can you help me what are the next commands should I use to achieve the goal?
I wish to use this approach, Set rs = cn.Execute("Select * from DTR") and obtain the list of fields inside DTR. Can you help me what are the next commands should I use to achieve the goal?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Russ Suter, Thank you for providing me such code. It's actually working but what I want here is the tables name in the DTR. I want to apply maybe loop so that I could get the existing table in DTR. Hope you can extend your help. Thanks!
This isn't making sense. What is 'DTR'? Is it a table, a schema, a database?
If it's a table then it has fields (or columns). If it's a schema then it has tables. If it's a database then it has a whole bunch of things.
If it's a table then it has fields (or columns). If it's a schema then it has tables. If it's a database then it has a whole bunch of things.
ASKER
Sorry Russ Suter, DTR is a Table and I want to get the all columns name on it.
I've already answered that question.
SELECT
[sys].[columns].*
FROM
[sys].[columns]
INNER JOIN
[sys].[tables] ON
[sys].[tables].[object_id] = [sys].[columns].[object_id]
AND [sys].[tables].[name] = 'DTR'
ORDER BY
[column_id]
What am I missing?
ASKER
Hi Russ, your code is working very well. The way I applied your code is something like this;
Set rs = cn.Execute("Select [sys].[columns].* FROM [sys].[columns] INNER JOIN [sys].[tables] ON [sys].[tables].[object_id] = [sys].[columns].[object_id] AND [sys].[tables].[name] ='" & "DTR" & "' ORDER BY [column_id] ")
with rs
If rs.BOF = True Then 'Not exist
response.write("Not exist")
response.end()
cn.close
else
response.write (trim(rs("column_name"))
response.end()
cn.close
end if
end with
Base on above, I want to obtain columns name. Thank you!
Replace "column_name" with "name" in your code
Also, if all you're interested in is the name of the column you an replace the '*' in the query with "name"
ASKER
Thanks a lot, Russ Suter, It's now working...
please note the topic "SQL" refers to the generic standardized language; not to any specific vendor's product such as "Microsoft SQL Server"