SQL statement to query the field names of a table

Source: Internet
Author: User
Tags how to use sql

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.