+ -
当前位置:首页 → 问答吧 → 为什么总是我才遇到古怪的问题!!!

为什么总是我才遇到古怪的问题!!!

时间:2011-12-23

来源:互联网

select * into #temp from Enterprise_Account where 1=2

insert into #temp exec('select * from Enterprise_Account')


消息 213,级别 16,状态 7,第 1 行
列名或所提供值的数目与表定义不匹配。

不要给我说列名不对,数目不对,是高手都知道不是这个问题。
经我反复测试,发现在 Enterprise_Account 表里的 ID设的是主键,如果去掉主键就好了,不知道原因

如果改成 insert into #temp from Enterprise_Account 也可以执行通过。


现在的问题是结果集必须由存储返回,所以只能用这种写法,请大师指点!!!

作者: gxboy   发布时间: 2011-12-23

ID确定不是自增的?

作者: wufeng4552   发布时间: 2011-12-23

自增列的问题。

作者: fredrickhu   发布时间: 2011-12-23

SQL code

--这两句话是不一样的

--这句是把Enterprise_Account 的内容插入到#temp中并创建表,因为1=2不成立,所以出现的是空表
select * into #temp from Enterprise_Account where 1=2

--这句是插入到存在的#temp中,不会自动创建表
insert into #temp exec('select * from Enterprise_Account')


作者: maco_wang   发布时间: 2011-12-23

很明显是对应列的问题,看下是哪列出了问题。。 只有出了问题才能进步。

作者: szstephenzhou   发布时间: 2011-12-23

1.首先确认ID设的是主键,但不是自增列.
2.把'select * from Enterprise_Account'这句写在存储过程中.
create procedure my_proc
as
begin
  select * from Enterprise_Account
end
go

insert into #temp exec my_proc

作者: dawugui   发布时间: 2011-12-23

SQL code

create table Enterprise_Account
(id int identity(1,1) primary key, col1 varchar(5), col2 varchar(5))

insert into Enterprise_Account 
select 'aa1','aa2' union all select 'bb1','bb2'

select * from Enterprise_Account

id          col1  col2
----------- ----- -----
1           aa1   aa2
2           bb1   bb2


-- 复制表结构
select * into #temp from Enterprise_Account where 1=2

-- 导入数据时,排除id列: select col1,col2 from ..
insert into #temp exec('select col1,col2 from Enterprise_Account')

select * from #temp

id          col1  col2
----------- ----- -----
1           aa1   aa2
2           bb1   bb2

(2 row(s) affected)

作者: ap0405140   发布时间: 2011-12-23

SQL code
create procedure my_proc
as
begin
  select * from enterprise_account
end
go
insert into #temp 
exec my_proc

作者: lzd_83   发布时间: 2011-12-23

6楼是方法1, 

觉得不适合你的环境的话, 我再告诉你方法2,方法3,方法4..

作者: ap0405140   发布时间: 2011-12-23