|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Verify if a Table ExistsIs there a command to verify that a table exists? I am writing a .Net
application. I would like to have the .Net application check if a table exists (initiated by a menu command) and then create the table if it does not exist. Is there a similar command to verify if a field exists in a table? I would like to perform these steps using the SqlClient SqlCommand from .Net -- not the Enterprise Manager. Thanks select count(*) FROM information_schema.tables WHERE table_name =
'TableYouAreLookingFor' The above would return 1 if that table exists, 0 if not. select count(*) FROM information_schema.columns WHERE table_name = 'TableName' AND column_name = 'ColumnName' The above returns 1 if the column exists, 0 if it doesn't. Don't know much about ado.net programming, but you could pass that as the sql query string execute it as a scalar. -- Show quote"Paul DeWitt" wrote: > Is there a command to verify that a table exists? I am writing a .Net > application. I would like to have the .Net application check if a table > exists (initiated by a menu command) and then create the table if it does not > exist. Is there a similar command to verify if a field exists in a table? I > would like to perform these steps using the SqlClient SqlCommand from .Net -- > not the Enterprise Manager. > > Thanks If you want to verify the existance of a constant table:
USE <database> IF OBJECT_ID('<TableName>') IS NULL CREATE TABLE [Table] (...) If you want to verify the existance of a temporary table: IF OBJECT_ID('tempdb..<TableName>') IS NULL CREATE TABLE #Table(...) Show quote "Mark Williams" wrote: > select count(*) FROM information_schema.tables WHERE table_name = > 'TableYouAreLookingFor' > > The above would return 1 if that table exists, 0 if not. > > select count(*) FROM information_schema.columns > WHERE table_name = 'TableName' AND column_name = 'ColumnName' > > The above returns 1 if the column exists, 0 if it doesn't. > > Don't know much about ado.net programming, but you could pass that as the > sql query string execute it as a scalar. > > -- > > > "Paul DeWitt" wrote: > > > Is there a command to verify that a table exists? I am writing a .Net > > application. I would like to have the .Net application check if a table > > exists (initiated by a menu command) and then create the table if it does not > > exist. Is there a similar command to verify if a field exists in a table? I > > would like to perform these steps using the SqlClient SqlCommand from .Net -- > > not the Enterprise Manager. > > > > Thanks |
|||||||||||||||||||||||