不使用游标,如何将一个结果集插入到新表中?

littlefat 2004-10-27 02:10:04
原表:
Category_ID License_Name License_Full_Name
0 名称一 名称一全称
0 名称二 名称二全称
1 名称三 名称三全称
。。。
(该表是从原来Access库得来)

新表(SQL Server)
CategoryID(主键,自动增长) LicenseName LicenseFullName


因为SQL Server的表的自动增长主键的值是从1开始,现在考虑要将原表中的Category_ID为0的记录导入到新表中,我使用下面的语句不行。。。

Select 1, License_Name, License_Full_Name into NewLicense from OldLicense where Category_ID=0

提示错误:
服务器: 消息 8155,级别 16,状态 1,行 1
没有为第 1 列(属于 'License')指定列。

或者我异想天开地想让插入的新数据从一个结果集中自动获取:

Insert Into NewLicense Values (Select 1, License_Name, License_Full_Name from OldLicense where CategoryID=0)


我知道使用游标可以解决问题,但是我不想使用游标,而且倒数据的工作只做一次,不知道有没有什么简便的方法?

我知道使用游标可以完成,但是太复杂了
...全文
194 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
songzj8 2004-10-27
  • 打赏
  • 举报
回复
2、NewLicense 已存在
insert NewLicense ( License_Name, License_Full_Name)
select License_Name, License_Full_Name
from OldLicense where Category_ID=0
songzj8 2004-10-27
  • 打赏
  • 举报
回复
1、NewLicense 不存在
Select License_Name, License_Full_Name into NewLicense from OldLicense where Category_ID=0

2、NewLicense 已存在
insert NewLicense ( License_Name, License_Full_Name)
from OldLicense where Category_ID=0
lsxaa 2004-10-27
  • 打赏
  • 举报
回复
insert into NewLicense( select LicenseName LicenseFull_Name)
Select License_Name, License_Full_Name
from OldLicense
where Category_ID=0
yjdn 2004-10-27
  • 打赏
  • 举报
回复
再改一下:
insert into 新表( select LicenseName LicenseFullName) select License_Name License_Full_Name from 源表 where Category_ID=0

上面是你已经建好表的情况,如果你还没有建表:
改成这样:
select identity(int,1,1) as CategoryID,License_Name as LicenseName, License_Full_Name
as LicenseFullName into 新表 from 原表 where Category_ID=0
zjcxc 2004-10-27
  • 打赏
  • 举报
回复
--如果新表还没有建好,可以直接这样

Select CategoryID=identity(int,1,1)
,License_Name, License_Full_Name
into NewLicense
from OldLicense where Category_ID=0
zjcxc 2004-10-27
  • 打赏
  • 举报
回复
--已经建好新表了吧
insert NewLicense(LicenseName,LicenseFullName)
Select License_Name, License_Full_Name
from OldLicense where Category_ID=0
yjdn 2004-10-27
  • 打赏
  • 举报
回复
insert into 新表( select LicenseName LicenseFull_Name) select License_Name License_Full_Name from 源表 where Category_ID=0
littlefat 2004-10-27
  • 打赏
  • 举报
回复
自己顶一下。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧