怎么判断临时表里存在一个字段?
比如,生成的#temp临时表应该有字段f1,怎么判断这个f1字段是否在#temp临时表里有呢?因为我要从临时表做这样的查询:
- SQL code
select (case when [f1]='' then '1' when [f1]='A' then '2' end) as f1 from #temp
但是如果f1不小心不存在的话,就要报错,怎么判断呢?
[解决办法]
- SQL code
if exists(select 1 from tempdb.dbo.syscolumns where ID=OBJECT_ID('Tempdb..#temp') and name='f1')print '存在'else print '不存在'
[解决办法]
- SQL code
create table #temp1 (f1 char(1))create table #temp2 (f2 char(1))use tempdbif col_length('#temp1','f1') is not null print 'exists'else print 'not exists' --> existsif col_length('#temp2','f1') is not null print 'exists'else print 'not exists' --> not exists
[解决办法]