Check whether a table or Column exists in SQL Server

Source: Internet
Author: User

Check whether the table exists

1. Use System View: SYS. Tables

Select name from SYS. tables where name = 'tablaname' and type = 'U'

Type = 'U' is used to exclude stored procedures, views, and system tables. It refers to the user table user_table.

2. Use the System View: sysobjects
Select * From sysobjects where id = object_id ('usersubsyscenthistory _ null') and xtype = 'U'

Xtype = 'U' is used to exclude stored procedures, views, and system tables. It refers to the user table user_table.

Determine whether a Column exists

1. Assume that the table is AAA and the field to be added is the name field.
Select * From syscolumns where [name] = 'name' and objectproperty (ID, 'isusertable') = 1 and object_name (ID) = 'aaa ')

Bytes -------------------------------------------------------------------------------------------------------------------------------

Note;

Entitymanager em = super. getentitymanager (appconfig. persistence_cent );
Object result = em. createnativequery (SQL). getsingleresult ();

Getsingleresult ()

 

Throws:
NoResultException-If there is no result: the returned value is 0.
NonUniqueResultException-If more than one result: one additional result is returned.

Therefore, ensure that there is only one record. You can use list instead.Getresultlist(), Determine whether there is a result by judging its size.

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.