SQL Statement Implementation table field name query

Source: Internet
Author: User

The following is a description of the SQL statement for the field name of the query table, and the SQL statement can implement many of the functions, hoping you can learn from the use of SQL statements to gain inspiration.

Select name from syscolumns where id = (select id from sysobjects where type = ' u ' and name = ' corresponding table name ') or select name F Rom syscolumns where id = object_id (' corresponding table name ') use the above SQL statement to enter the corresponding table name can be found in the table's field names, corresponding to the database query whether there is a table statement

The way to determine if a table name exists in a database is

If not object_id (' corresponding table name ') is null print ' exists ' the goal of the field name in this query table is to write a little SQL statement when writing the program, however, there are too many fields in the table, if one copy is too slow, and it is possible to duplicate a field, So using the knowledge of their own database, write a SQL statement directly generate a field name string, for example, I 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 gets field name already field type, type length

SELECT A.colid as id,a.name as columnname,b.name as datatype,a.length as length from syscolumns a,systypes b WHERE a.id= OBJECT_ID (' corresponding table name ') Anda.xtype=b.xtype and b.name <> ' sysname ' ORDER by a.colid

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.