Get the App
SLTechnology News&Howtos  ›  Database  › 

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

Shulou Source: shulou.com Published: 2022-05-31 19:24:04 09月30日 Update

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.

Tags: Methods content analysis concise short professional small and medium rich in content that is articles more this column knowledge articles classics industry perspective information channels channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Huawei NVidia Shulou Information Docker