问一个数据库方面的超简单问题啊

时间:2022-12-11 18:03:42
select top 1 id into #test from aaaa where type=1 order by getdate desc



这样给临时表插入一条信息后,就不能再向临时表#test中追加信息了么?
比如
select top 1 id into #test from aaaa where type=2 order by getdate desc

这样就会出错,为什么啊

16 个解决方案

#1


id是主键么 会不会读出来的东西一样

#2


读出来的肯定不一样啊,ID是主键,并且是自动增长列

#3


select 和 insert into 分开写
select top 1 id from aaaa where type=1 order by getdate desc
insert into aaa (test) values('')

#4


关键就是要把表aaaa中的数据取出来,直接放在临时表#test中啊

#5


select into 语句只能操作一次,他会把表结构和符合条件的纪录都copy进去.

如果要对临时表多次操作请用insert into

#6


谢谢啊,我也发现这个问题了
我把存储过程改诚这样了
select top 1 id into #test from aaaa where type=1 order by getdate desc
insert into #test select id from aaaa where type=2 order by getdate desc

但这样会报错啊

仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 '#test' 中为标识列指定显式值。

#7


select top 1 id from aaaa where type=1 order by getdate desc
这个是创建表并插入,再这样,由于表已经存在就会出错,你可以

use tempdb
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[#test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[#test]
use userdb --你的库
select top 1 id into #test from aaaa where type=1 order by getdate desc


use tempdb
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[#test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
begin
 use userdb --你的库
 insert into.............
end
else
begin
 use userdb --你的库
 select top 1 id into #test from aaaa where type=1 order by getdate desc
end

#8


if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb.dbo.#test') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

刚才试了在当前库里用上面不行,用这个可以
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb.dbo.#test') )
但又担心怕不准..........
要不然可以减少上面的use...了

仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 '#test' 中为标识列指定显式值
-------------->select ..into #test后,表结构也创建了,id为标识列,不能再插入修改

所以这又是个问题,插入前得修改SET IDENTITY_INSERT ON

#9


谢谢楼上的啊
我主要是想返回几条记录
就是TYPE=1的第一条记录,TYPE=2的第一条记录.........
这样
有什么更好的办法么

#10


你到底想怎么操作?
如果只是type=1 和type=2
你怎么不用 

select id into #test from aaaa where type in(1,2) 
group by type
order by getdate desc

#11


都要只取出一条啊
就是都是top 1

#12


如果用 type in(...)的话还是只能取出一条啊

#13


HELP

#14


帮顶

#15


顶啊

#16


select id into #test from aaaa
where type=1 or type=2
group by type
order by getdate desc
没试验

#1


id是主键么 会不会读出来的东西一样

#2


读出来的肯定不一样啊,ID是主键,并且是自动增长列

#3


select 和 insert into 分开写
select top 1 id from aaaa where type=1 order by getdate desc
insert into aaa (test) values('')

#4


关键就是要把表aaaa中的数据取出来,直接放在临时表#test中啊

#5


select into 语句只能操作一次,他会把表结构和符合条件的纪录都copy进去.

如果要对临时表多次操作请用insert into

#6


谢谢啊,我也发现这个问题了
我把存储过程改诚这样了
select top 1 id into #test from aaaa where type=1 order by getdate desc
insert into #test select id from aaaa where type=2 order by getdate desc

但这样会报错啊

仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 '#test' 中为标识列指定显式值。

#7


select top 1 id from aaaa where type=1 order by getdate desc
这个是创建表并插入,再这样,由于表已经存在就会出错,你可以

use tempdb
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[#test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[#test]
use userdb --你的库
select top 1 id into #test from aaaa where type=1 order by getdate desc


use tempdb
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[#test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
begin
 use userdb --你的库
 insert into.............
end
else
begin
 use userdb --你的库
 select top 1 id into #test from aaaa where type=1 order by getdate desc
end

#8


if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb.dbo.#test') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

刚才试了在当前库里用上面不行,用这个可以
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb.dbo.#test') )
但又担心怕不准..........
要不然可以减少上面的use...了

仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 '#test' 中为标识列指定显式值
-------------->select ..into #test后,表结构也创建了,id为标识列,不能再插入修改

所以这又是个问题,插入前得修改SET IDENTITY_INSERT ON

#9


谢谢楼上的啊
我主要是想返回几条记录
就是TYPE=1的第一条记录,TYPE=2的第一条记录.........
这样
有什么更好的办法么

#10


你到底想怎么操作?
如果只是type=1 和type=2
你怎么不用 

select id into #test from aaaa where type in(1,2) 
group by type
order by getdate desc

#11


都要只取出一条啊
就是都是top 1

#12


如果用 type in(...)的话还是只能取出一条啊

#13


HELP

#14


帮顶

#15


顶啊

#16


select id into #test from aaaa
where type=1 or type=2
group by type
order by getdate desc
没试验