SQL checks whether a table or field exists and adds a field example

Source: Internet
Author: User

If not exists (select * From DBO. sysobjects where id = object_id (N 'print _ billformat') and objectproperty (ID, N 'isusertable') = 1)
Begin
/* Print _ User format */
Create Table print_billformat
(Billid varchar (50) not null,
Printformat text null,
Formatid varchar (100) not null,
Orgid varchar (50)
)
End

/* Print _ premade format */
If not exists (select * From DBO. sysobjects where id = object_id (N 'print _ Bill') and objectproperty (ID, N 'isusertable') = 1)
Begin
Create Table print_bill
(Billmkid varchar (4) not null,/* module ID of the document */
Billid varchar (50) primary key not null,/* logical document name */
Billname varchar (50) not null,/* Document display name */
Recordspath varchar (200) null,
Paramcount int default 0/* Number of parameters in the generated XML */
)
End
Else
Begin
/* = */
/* Modify the table structure of print_bill */
/* = */
If not exists (select * From DBO. syscolumns where name = 'billkid' and ID = object_id ('print _ Bill '))
Begin
Alter table print_bill add billmkid varchar (4) default ''not null
End

Exec sp_rename 'print _ Bill ', 'print _ billtemp'

Create Table print_bill
(
Billmkid varchar (4) not null,/* module ID of the document */
Billid varchar (50) primary key not null,/* logical document name */
Billname varchar (50) not null,/* Document display name */
Recordspath varchar (200) null,
Paramcount int default 0,/* Number of parameters in the generated XML */
)

Insert into print_bill (billmkid, billid, billname, recordspath, paramcount)
Select billmkid, billid, billname, recordspath, paramcount
From print_billtemp

Drop table print_billtemp
End

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.