Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to determine whether a field column exists in SQL

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

SQL how to determine whether the field column exists, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

The copy code for adding fields is as follows: alter table docdsp add dspcode char

The copy code for deleting fields is as follows: ALTER TABLE table_NAME DROP COLUMN column_NAME

The copy code for modifying the field type is as follows: ALTER TABLE table_name ALTER COLUMN column_name new_data_type

Rename sp_rename changes the name of a user-created object in the current database, such as a table, column, or user-defined data type. The syntax copy code is as follows: sp_rename [@ objname =] 'object_name', [@ newname =]' new_name' [, [@ objtype =] 'object_type']

-- suppose the table to be processed is named: tb

-- determine whether there is a primary key in the table to which the column is to be added. If exists (select 1 from sysobjects where parent_obj=object_id ('tb') and xtype='PK') beginprint' table already has a primary key. Columns can only be added as ordinary columns.'

-- add a column of type int. The default value is 0alter table tb add column name int default 0 endelsebeginprint'No primary key in the table, add primary key column'

-add a column of type int. The default value is 0alter table tb add column name int primary key default 0 end/*/

To determine whether there is a name field in table1, the copy code is as follows: if exists (select * from syscolumns where id=object_id ('table1') and name='name') beginselect * from people;end

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report