代码如下(以下语句在northwind数据库中可直接执行)
declare @tmp as char(1)
set @tmp='1'
if @tmp='1'
begin
select top 1 * into #a from employees 
end
else
begin
select top 2 * into #a from employees 
end
按照我的理解应该是分支语句,应该只会有一个结果集插入到#a表中,但是运行结果是报 "#a表已存在"错误,哪位能解释一下这是什么错误。还有如果我想要达到用条件语句判断。按不同的满足条件插入不同的结果集合到#a表中该怎么做比较合理一点,谢谢

解决方案 »

  1.   

    在第一行加一句
    if object_id('tempdb..#a') is not null drop table #a
      

  2.   

    declare @tmp as char(1)
    set @tmp='1'select top 1 * into #a from employees where 1=2if @tmp='1'
    begin
    insert into #a select top 1 *  from employees 
    end
    else
    begin
    insert into #a select top 2 * from employees
      

  3.   

    --先创建临时表,然后再追加declare @tmp as char(1)
    set @tmp='1'select * into #a from employees where 1=2if @tmp='1'
    begin
    insert into #a select top 1 *  from employees 
    end
    else
    begin
    insert into #a select top 2 * from employees