SQL Server Add Delete field to determine whether a table or field exists

Source: Internet
Author: User
Tags join table name

Add Field

ALTER TABLE DOCDSP add Dspcode char (200)

Delete a field

ALTER TABLE table_name DROP COLUMN column_name

Modify a field type

ALTER TABLE table_name ALTER COLUMN COLUMN_NAME NEW_DATA_TYPE

Renamed

sp_rename

Changes the name of a user-created object (such as a table, column, or user-defined data type) in the current database.

Grammar

sp_rename [@objname =] ' object_name ',

[@newname =] ' New_name '

[, [@objtype =] ' object_type ']

=======================================================

--Assume the table name to be processed is: TB

--Determine if there is a primary key in the table to add the column

if exists (select 1 from sysobjects where parent_obj=object_id (' TB ') and xtype= ' PK ')

Begin

The print ' table already has a primary key and the column can only be added as a normal column '

--Add a column of type int with a default value of 0

ALTER TABLE TB add column name int default 0

End

Else

Begin

No primary key in print ' table, add primary key column '

--Add a column of type int with a default value of 0

ALTER TABLE TB add column name int primary key default 0

End

/**************************************************************************************/

To determine if the Name field exists in the Table1

if exists (SELECT * from syscolumns where id=object_id (' table1 ') and name= ' name ') begin

SELECT * from people;

End

========================================================================

If a real table can be used

if exists (select * from sysobjects where id = object_id (N ' [dbo].[ Table name] and OBJECTPROPERTY (ID, N ' isusertable ') = 1) drop table [dbo]. [Table name]--Delete if table exists

If a temporary table can be used

If object_id (' tempdb.. # #temp ') is not null

drop TABLE # #temp

Description, if you use the Find Real table method to hit the temporary table will not be found. Publish a difference to the generation.

==========================================================

Get a description of a table field

I usually use this view

Create View Fielddesc

As

Select O.name as Oname, c.name as Cname,convert (varchar (), p.value) as value,p.smallid as psmallid,t.name as Tname

From syscolumns C

Join systypes t on c.xtype = T.xtype

Join sysobjects o on o.id=c.id

Left join Sysproperties p on P.smallid=c.colid and P.id=o.id

where o.xtype= ' U '

When querying:

Select * from Fielddesc where oname = ' Your table name '

Note : Please pay attention to the Triple tutorial channel for more wonderful tutorials,

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.