“Cara memeriksa apakah kolom ada di tabel SQL” Kode Jawaban

Cara memeriksa apakah kolom ada di tabel SQL

IF EXISTS(SELECT 1 FROM sys.columns WHERE Name = N'columnName' AND Object_ID = Object_ID(N'schemaName.tableName'))
BEGIN
	ALTER TABLE MyTable ADD COLUMN MyOtherColumn(DATATYPE)
END
Fortune Mbulazi

SQL Periksa apakah ada kolom

* Using the below query, You can check whether the table1 has a column named "id"

SHOW COLUMNS FROM table1 LIKE 'id'
Arslan

Bagaimana cara memeriksa apakah ada kolom di tabel SQL Server?


A more concise version

IF COL_LENGTH('table_name','column_name') IS NULL
BEGIN
/* Column does not exist or caller does not have permission to view the object */
END
The point about permissions on viewing metadata applies to all answers not just this one.

Note that the first parameter table name to COL_LENGTH can be in one, two, or three part name format as required.

An example referencing a table in a different database is

COL_LENGTH('AdventureWorks2012.HumanResources.Department','ModifiedDate')
One difference with this answer compared to using the metadata views is that metadata functions such as COL_LENGTH always only return data about committed changes irrespective of the isolation level in effect.
shafeeque

Jawaban yang mirip dengan “Cara memeriksa apakah kolom ada di tabel SQL”

Pertanyaan yang mirip dengan “Cara memeriksa apakah kolom ada di tabel SQL”

Lebih banyak jawaban terkait untuk “Cara memeriksa apakah kolom ada di tabel SQL” di Sql

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya