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

Create a Sql Server if you decide that a table or column does not exist

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

In this issue, the editor will bring you about how to judge whether the table or column does not exist in Sql Server. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

The first method

IF EXISTS (SELECT 1 FROM SYSOBJECTS T1 INNER JOIN SYSCOLUMNS T2 ON T1.ID=T2.ID WHERE T1.NAMESTRACTA`AND T2.NAMEPROPERATION C') PRINT 'exists' ELSE PRINT 'does not exist'

The second method is short and pithy, which can be described as a classic.

IF COL_LENGTH ('Agar,' C') IS NOT NULL PRINT N' exists' ELSE PRINT N' does not exist'

Method 1:

Select * from syscolumns where id=object_id ('table name') and name=' column name'

Description: if it exists, a description record of this column is returned. There is no empty return.

Method 2:

Select count (*) from sysobjects a Magi syscolumns b where a.id=b.id and b.nameplate class1 'and a.typewritten products and a. Nameplate production products Class'

Description: returns 1 if it exists, 0 if it doesn't exist.

2. Determine whether the table or column exists in Sql Server, and create it if it does not exist.

First, create a table if it does not exist:

If not exists (select * from sysobjects where id = object_id ('mytab') and OBJECTPROPERTY (id,' IsUserTable') = 1) create table mytab (id int, age int, name varchar (max), primary key (id,age)) go

Second, create a column if it does not exist.

If not exists (select * from syscolumns where id=object_id ('mytab') and name='columnname') alter table [mytab] add columnname nvarchar (max) the above is how to determine whether a table or column does not exist in the Sql Server shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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