Interview Questions Answers.ORG
Interviewer And Interviewee Guide
Interviews
Quizzes
Home
Quizzes
Interviews Databases Programming Interviews:BtrieveClipperData ModelingData StructuresDatabase AdministrationDatabase Administrator (DBA)Database AnalystDatabase DeveloperDB DevelopmentEDI/Data Integration ExpertFirebirdHierarchicalIBM DB2InformixJava DatabaseMariaDBMicrosoft Access DeveloperMongoDBMS SQL ServerMySQL ProgrammingNetworkNoSQLObject RelationalPostgrePostgreSQLProgressRDBMSRelationalSQLSQL AdministratorSQL and PL/SQLSQL Notification ServicesSQL server 2008SSRSStored ProcedureSybaseTeradata
Copyright © 2018. All Rights Reserved
MS SQL Server Interview Question:
How To List All Columns in a Table using odbc_columns()?
Submitted by: AdministratorIf you want to get a list of all columns in a table, you can use the odbc_columns() function, which can actually be used to list all columns in all tables and views in the database. The syntax of odbc_columns() is:
$result_set = odbc_columns($connection_object,
$qualifier, # database name for SQL Server
$schema, # schema name for SQL Server
$table_name, # table or view name for SQL Server
$column_name # column name
)
#- The returning result set contains 5 fields:
#- TABLE_QUALIFIER, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME
#- DATA_TYPE, TYPE_NAME, PRECISION, LENGTH, SCALE, RADIX
#- NULLABLE, REMARKS
The schema, table_name and column_name arguments accept search patterns ( '%' to match zero or more characters and '_' to match a single character).
The tutorial example below shows you how to get a list of columns in a table called ggl_rates in the current database, GlobalGuideLineDatabase, which is hard coded in the DSN definition:
Submitted by: Administrator
$result_set = odbc_columns($connection_object,
$qualifier, # database name for SQL Server
$schema, # schema name for SQL Server
$table_name, # table or view name for SQL Server
$column_name # column name
)
#- The returning result set contains 5 fields:
#- TABLE_QUALIFIER, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME
#- DATA_TYPE, TYPE_NAME, PRECISION, LENGTH, SCALE, RADIX
#- NULLABLE, REMARKS
The schema, table_name and column_name arguments accept search patterns ( '%' to match zero or more characters and '_' to match a single character).
The tutorial example below shows you how to get a list of columns in a table called ggl_rates in the current database, GlobalGuideLineDatabase, which is hard coded in the DSN definition:
Submitted by: Administrator
<?php
$con = odbc_connect('ggl_SQL_SERVER','sa','GlobalGuideLine');
# odbc_tables($con, $database, $schema, $table, $column);
$res = odbc_columns($con, 'GlobalGuideLineDatabase','%','ggl_rates','%');
print("Column list of ggl_rates: ");
while (odbc_fetch_row($res)) {
print(" ".odbc_result($res,2));
print(", ".odbc_result($res,3));
print(", ".odbc_result($res,4));
print(", ".odbc_result($res,5));
print(", ".odbc_result($res,6));
print(", ".odbc_result($res,7));
print(", ".odbc_result($res,8));
print(", ".odbc_result($res,9));
print(", ".odbc_result($res,10));
print(", ".odbc_result($res,11));
print(", ".odbc_result($res,12)." ");
}
odbc_free_result($res);
odbc_close($con);
?>
If you run this script, you will get something like:
Column list of ggl_rates:
dbo, ggl_rates, id, 4, int, 10, 4, 0, 10, 1,
dbo, ggl_rates, comment, 12, varchar, 16, 16, , , 1,
Now you know that table, ggl_rates, has two columns: "id INT" and "comment VARCHAR(16)".
Submitted by: Administrator
$con = odbc_connect('ggl_SQL_SERVER','sa','GlobalGuideLine');
# odbc_tables($con, $database, $schema, $table, $column);
$res = odbc_columns($con, 'GlobalGuideLineDatabase','%','ggl_rates','%');
print("Column list of ggl_rates: ");
while (odbc_fetch_row($res)) {
print(" ".odbc_result($res,2));
print(", ".odbc_result($res,3));
print(", ".odbc_result($res,4));
print(", ".odbc_result($res,5));
print(", ".odbc_result($res,6));
print(", ".odbc_result($res,7));
print(", ".odbc_result($res,8));
print(", ".odbc_result($res,9));
print(", ".odbc_result($res,10));
print(", ".odbc_result($res,11));
print(", ".odbc_result($res,12)." ");
}
odbc_free_result($res);
odbc_close($con);
?>
If you run this script, you will get something like:
Column list of ggl_rates:
dbo, ggl_rates, id, 4, int, 10, 4, 0, 10, 1,
dbo, ggl_rates, comment, 12, varchar, 16, 16, , , 1,
Now you know that table, ggl_rates, has two columns: "id INT" and "comment VARCHAR(16)".
Submitted by: Administrator
Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.
https://InterviewQuestionsAnswers.ORG.