日期:2014-05-18  浏览次数:20485 次

简单EXISTS问题!高手解救........谢谢
IF NOT EXISTS (?????????)
exec( 'create table '+@tb+@curY+ '(dt datetime,tag int,type int,mval real,mmax real,mmin real,mavg real,msum float )')
这样写如果不存在,就创建表 @tb+@curY(字符型+int)

------解决方案--------------------
SQL code

if object_id('数据库名..表名') is not null
   drop table 表名
else
   create table ....
go

------解决方案--------------------
declare @tb varchar(10)
declare @curY int
declare @sql varchar(1000)

select @tb='a'
select @cury=1
select @sql='if not exists (select * from dbo.sysobjects where id = object_id(N''[dbo].['+@tb+convert(varchar(10),@cury)+']'') and OBJECTPROPERTY(id, N''IsUserTable'') = 1)'
+' create table '+@tb+convert(varchar(10),@cury)+ '(dt datetime,tag int,type int,mval real,mmax real,mmin real,mavg real,msum float ) '
exec(@sql)

------解决方案--------------------
SQL code

if not exists(select * from sys.sysobjects where id=object_ID(@tb+@curY))
exec(  'create table   '+@tb+@curY+  '(dt datetime,tag int,type int,mval real,mmax real,mmin real,mavg real,msum float ) ')