当前位置: 代码迷 >> Sql Server >> 怎么写一个SQL语句 当不存在某表时,先新建该表,再添加数据,否则直接添加数据
  详细解决方案

怎么写一个SQL语句 当不存在某表时,先新建该表,再添加数据,否则直接添加数据

热度:71   发布时间:2016-04-27 16:16:49.0
如何写一个SQL语句 当不存在某表时,先新建该表,再添加数据,否则直接添加数据
需要实现这样的条件语句  
当#linshi未建立时,建立该表,然后   insert   相应的数据
当#linshi已存在,则直接INSERT数据
希望在一次执行中实现
应该怎么写语句?
只用SQL   SERVER   2000
谢谢

------解决方案--------------------
if object_id( 'tempdb..#linshi ') is not null
print '1 '--create table #linshi .....
--insert ...
------解决方案--------------------
if object_id( 'tempdb..#tempTable ') is not null
Begin
插入数据
End
else
begin
创建临时表
插入数据
end
------解决方案--------------------
才知道错了,还能简化
if object_id( 'tempdb..#linshi ') is null
create table #linshi ...
insert #linshi select ...
------解决方案--------------------
if object_id( 'tempdb..#linshi ') is not null
insert into #linshi values(4,5)
else
begin
create table #linshi (id int, value int)
insert into #linshi values(2,3)
end
------解决方案--------------------
if exists(select 1 from #linshi)
insert into #linshi select * from tab
else
begin
create table #linshi(col1....)
insert into #linshi select * from tab
end
  相关解决方案