Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query to fetch column name from a table

Author  Topic 

Bala Dhesingu
Starting Member

1 Post

Posted - 2011-10-20 : 01:30:42
Hi

I have tried the below quries to fetch the column name (only column name not the data) it didn't work..I have posted the error under each query. Please help me on this i am new to SQL. Iam not sure what does the error description mean.Table name here is mytable.

describe mytable;
java.sql.SQLException: [STATEMENT-
EXECUTE] Invalid character or token: "<END-OF-STATEMENT>?. INTO"

select column_name from mytable ;
java.sql.SQLException: [STATEMENT-EXECUTE] Not a column of referenced tables: "COLUMN_NAME


select column_name from all_tab_cols where table_name = 'mytable';
java.sql.SQLException: [STATEMENT-EXECUTE] Undefined name: "mytable.ALL_TAB_COLS"

select column_name from all_tab_columns where table_name = 'mytable'
java.sql.SQLException: [STATEMENT-EXECUTE] Undefined name: "mytable.ALL_TAB_COLUMNS"

select column_name from user_tab_columns where table_name = 'mytable'
java.sql.SQLException: [STATEMENT-EXECUTE] Undefined name: "mytable.USER_TAB_COLUMNS"

SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'mytable'
java.sql.SQLException: [STATEMENT-EXECUTE] Undefined name: "INFORMATION_SCHEMA.COLUMNS

SELECT column_name FROM information_schema.columns WHERE table_name = 'mytable' order by ordinal_position
java.sql.SQLException: [STATEMENT-EXECUTE] Undefined name: "INFORMATION_SCHEMA.COLUMNS"

SELECT name FROM syscolumns WHERE [id] = OBJECT_ID('mytable')
java.sql.SQLException: [STATEMENT-EXECUTE] Invalid character or token: "[?NOT EXISTS XMLEXISTS ( USER ? CURRENT CURRENT_SCHEMA ROW"

Devart
Posting Yak Master

102 Posts

Posted - 2011-10-20 : 03:10:29
If you use MS SQL Server 2005/2008

SELECT c.name
FROM sys.all_columns c
INNER JOIN sys.tables t
ON c.object_id=t.object_id
WHERE t.name='mytable'

Devart,
SQL Server Tools:
dbForge Data Studio
dbForge Schema Compare
dbForge Data Compare
dbForge SQL Complete
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-20 : 04:15:04
i prefer using INFORMATION_SCHEMA.COLUMNS view

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name =<yourtable>

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-10-21 : 04:37:10
Are you using ORACLE or SQL Server?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -