Go to-query database-field-whether the table has sqlserver

Source: Internet
Author: User

Use master
Go

/*Check whether the database exists*/


Select * from Master. DBO. sysdatabases where name = 'game _ card' and status <> 512

/*However, in actual use, you need to determineStatusStatus bit:
Some status bits can be used by users.Sp_dboption(Read Only,DBO
Use only,Single User:
1 = autoclose; UseSp_dboption.The database is completely closed, and its resources are released after the last user logs out.
4 = select into/bulkcopy ; UseSp_dboption. Allowed
SelectStatement and fast large-capacity replication.
8 = trunc. Log On chkpt ; UseSp_dboption. If the database is in log truncation mode, the check point truncates the non-active part of the log. Only
MasterSet this option for the database.16 = torn page Detection, UseSp_dboption. Detect incomplete pages.
32 = Loading .
64 = pre recovery .
128 = recovering .
256 = not recovered .
512 = offline ; UseSp_dboption. The database is offline.
1024 = read only ; UseSp_dboption. Users can only read data in the database and cannot modify it.
2048 = DBO use only ; UseSp_dboption. Only the database owner can use the database.
4096 = single user ; UseSp_dboption. Only one user can access the database at a time.
32768 = emergency mode .
4194304 = autoshrink .
1073741824 = cleanly Shutdown .
You can open multiple locations at the same time.

 

 

*/

-- SQL ServerTo determine whether the table object exists:
/*
Select count (*) from sysobjects where id = object_id (' Database Name. Owner.Table Name ')
If exists
(Select count (*) from sysobjects where id = object_id ('Database Name. Owner.Table Name '))
Print'Exist '
Else
Print'Does not exist'

*/
Use game_card
Go
Select count (*) from sysobjects where id = object_id ('game _ card. DBO. game_card ')
If exists
(Select count (*) from sysobjects where id = object_id ('game _ card. DBO. game_card '))
Print'Exist'


Use game_card
Go

Select count (*) from DBO. game_card

/* SQL ServerTo determine whether the fields in the table exist:
If exists (select * From syscolumns where name = 'colname1' and ID = object_id (' Database Name. Owner.Table Name '))
Print'Exist '
Else
Print'Does not exist '
Representative tableTablename1YesColname1Field*/

 

Check whether the index exists

 

Select 1 from SYS. indexes where object_id = object_id (@ tname, n'u') and name = @ INAME

Here, @ tname indicates the name of the index creation table, and @ INAME indicates the index name.

Eg:

[SQL] View plaincopyprint?
    1. Select Top1 1FromSYS. IndexesWhereObject_id = object_id ('Orders', N'U')And Name='Idx _ cl_od'
 
Select top 1 1 from SYS. indexes where object_id = object_id ('Orders ', n'u') and name = 'idx _ cl_od'

Extended knowledge

1. In each database, SYS. sysobjects is used to include each object (such as constraints, default values, logs, rules, and stored procedures) created in the database ). For more information, see the help documentation on msdn: SYS. sysobjects

2. object_id is used to return the database object ID of objects within the framework range. If the database or object name cannot be found, for example, the corresponding name does not exist or the spelling is incorrect, null is returned. For more information, see the help document on msdn: object_id

3. SYS. indexes is used to store the indexes or heap of each table object (such as a table, view, or table value function). For details, refer to the help documentation on msdn: SYS. Indexes

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.