Find the number of columns in a table
I would like to know if it's possible to find the number of both rows and columns within a table.
SELECT COUNT(*)
FROM tablename
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_catalog = 'database_name' -- the database
AND table_name = 'table_name'
SELECT COUNT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_CATALOG = 'Database name'
AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'table name'
SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'Your_table_name';
Note: Your_table_name should be replaced by your actual table name
Using JDBC in Java:
String quer="SELECT * FROM sample2 where 1=2";
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(quer);
ResultSetMetaData rsmd = rs.getMetaData();
int NumOfCol=0;
NumOfCol=rsmd.getColumnCount();
System.out.println("Query Executed!! No of Colm="+NumOfCol);