日期:2014-05-17  浏览次数:20465 次

sql中执行插入的问题
set nocount on 
use master 
go
if db_id('Performance') is null  
create database Performance
go
use Performance
go
if db_id('dbo.Nums') is not null  
drop table dbo.Numsgo 
create table dbo.Nums(n int not null primary key); 
declare @max as int ,@rc as int; 
   set @max = 100 ;
   set @rc = 1;
   insert into Nums values(1); 
   while @rc*2 <= @max  
   begin   
    insert into dbo.Nums select n+@rc from dbo.Nums; --1
    set @rc = @rc +2;
  end
这儿--1出的这种写法在执行sql时是先执行select n+@rc from dbo.Nums,把查询的结果作为结果集直接插入Num表中,还是怎样的执行顺序??
重复执行时会提示:‘Nums以创建’,这儿已经写了if db_id('dbo.Nums') is not null  drop table dbo.Nums为什么还报这样的错????
------最佳解决方案--------------------
先执行select n+@rc from dbo.Nums,把查询的结果作为结果集直接插入Num表中 ,这个理解是正确的
------其他解决方案--------------------

if object_id('Nums','U') then drop table Nums

或者


if exists(select top(1) name from 服务器.数据库.dbo.sysobjects with(nolock) where name='Nums')
drop table Nums


试试

------其他解决方案--------------------
if db_id('dbo.Nums') is not null  
db_id 是判断数据库的 你这个是表Nums ,无效啊