首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

如何判断临时表里存在一个字段

2012-01-20 
怎么判断临时表里存在一个字段?比如,生成的#temp临时表应该有字段f1,怎么判断这个f1字段是否在#temp临时表

怎么判断临时表里存在一个字段?
比如,生成的#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
[解决办法]
探讨

我的数据库是SQL 2008的,我发现我生成的临时表的名字都带有很长的下划线,然后跟一组16进制的名称。我用上面大家给的方法就行不过……

热点排行