The following describes how to write SQL statements for the field names of a query table. SQL statements can provide many functions. We hope that you can learn how to use SQL statements.
Select name from syscolumns where id = (select id from sysobjects where type = 'U' and name = 'corresponding table name ') or select name from syscolumns where id = object_id ('corresponding table name') enter the corresponding table name in the preceding SQL statement to check the table field name. Check whether the table exists in the corresponding database query.
The method for determining whether the table name exists in the database is
If not object_id ('corresponding table name') is null print 'exists.' The purpose of this query is to write an SQL statement when writing a program, however, there are too many fields in the table. It would be too slow to copy one by one, and some fields may be copied and missed. Therefore, you can use your own database knowledge, if you write an SQL statement to directly generate a field name string, for example, if you want to write a select statement, you need to generate all the fields in the table:
Declare @ s varchar (1000) select @ s = isnull (@ s + ',', '') + [name] from syscolumns where id = object_id ('corresponding table name ') select @ s obtain the field name's already field type, type Length
SELECT. colid as ID,. name as ColumnName, B. name as DataType,. length as Length FROM syscolumns a, policypes B WHERE. id = object_id ('corresponding table name') anda. xtype = B. xtype and B. name <> 'sysname' order by. colid