SQLServer获取临时表列名并判断指定列名是否存在

时间:2021-01-07 00:05:54

if(OBJECT_ID('tempdb.dbo.#tempTB') is not null)
begin
drop table #tempTB;
end

create table #tempTB(
ID int NULL,
Name nvarchar(50) NULL,
Pwd varchar(50) NULL
)

--查询所有列名
select name from tempdb.sys.columns where object_id = OBJECT_ID('tempdb.dbo.#tempTB')
--查询定义的列长度(以字节为单位)
select COL_LENGTH('tempdb.dbo.#tempTB','ID')
--判断表中是否包含列
if COL_LENGTH('tempdb.dbo.#tempTB','ID') is null
begin
print '不存在列'
end
else
begin
print '存在'
end

drop table #tempTB;