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

MS SQL的BUG?
直接上代码:
SQL code

 IF 1=2
    begin
        SELECT * INTO #t FROM t1
        DROP TABLE #t
    end
IF 1=3 
    begin
        SELECT * INTO #t FROM t1
        DROP TABLE #t
    end



我认为上面的代码是没问题的.可是MS SQL2005/2008总是报错,数据库中已存在名为 '#t' 的对象。
你说这是不是BUG?
凭啥不让我创建两次#t!

------解决方案--------------------
当然会报错,在同一会话时检查语句通不过

这样改,把if放在where条件处

SELECT * INTO #t FROM t1 where 1=2
union all
select * from t1 where 1=3

或创建临时表接收

SQL code
create table #T(col.....)

if ..
insert into #T...
else if 
insert into #T ...

------解决方案--------------------
String sql = "insert into stu values(" + id + "," + name + "," + sex + "," + age + "," + weight + ")";
这句话有错吗
------解决方案--------------------
探讨
String sql = "insert into stu values(" + id + "," + name + "," + sex + "," + age + "," + weight + ")";
这句话有错吗

------解决方案--------------------
当两个表结构很不一样的时候,用动态SQL完成,
SQL code

declare @sql varchar(2000)

IF 1=2
begin
  select @sql='SELECT * INTO #t FROM '+'t1'+';DROP TABLE #t'
end
IF 1=3
begin
  select @sql='SELECT * INTO #t FROM '+'t1'+';DROP TABLE #t'
end

exec(@sql)