将表中的某个字段转换成标识字段,并保留原来的值
来源:互联网 发布:光海君 知乎 编辑:程序博客网 时间:2024/06/08 09:00
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
/*--将表中的某个字段转换成标识字段,并保留原来的值
注意,因为要删除原表,所以,如果表和其他表的关联,这些关联要重新创建
--邹建2003.12--*/
/*--调用示例
execp_setid'表名','要转换的字段名'
--*/
ifexists(select*fromdbo.sysobjectswhereid=object_id(N'[dbo].[p_setid]')andOBJECTPROPERTY(id,N'IsProcedure')=1)
dropprocedure[dbo].[p_setid]
GO
CREATEPROCP_SETID
@tbnamesysname, --要处理的表名
@fdnamesysname --要转换为标识字段的字段名
as
declare@s1varchar(8000),@s2varchar(8000),@tmptbsysname
select@s1='',@s2='',@tmptb='[tmp_'+@tbname+'_bak]'
select@s1=@s1+',['+name+']'
+casenamewhen@fdnamethen'=identity(bigint,1,1)'else''end
,@s2=@s2+',['+name+']'
fromsyscolumnswhereobject_id(@tbname)=id
select@s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)
exec('selecttop0'+@s1+'into'+@tmptb+'from['+@tbname+']
setidentity_insert'+@tmptb+'on
insertinto'+@tmptb+'('+@s2+')select'+@s2+'from['+@tbname+']
setidentity_insert'+@tmptb+'off
')
exec('droptable['+@tbname+']')
execsp_rename@tmptb,@tbname
go
/*==========================================================*/
--使用测试
--创建测试的表
createtable表(编号bigint,姓名varchar(10))
insertinto表
select1,'张三'
unionallselect2,'李四'
unionallselect4,'王五'
go
--调用存储过程,将编号字段改为标识字段
execp_setid'表','编号'
go
--显示处理结果
select*from表
--显示是否修改成功
selectnamefromsyscolumns
whereobject_id('表')=idandstatus=0x80
go
--删除测试
droptable表
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
/*--将表中的某个字段转换成标识字段,并保留原来的值
注意,因为要删除原表,所以,如果表和其他表的关联,这些关联要重新创建
--邹建2003.12--*/
/*--调用示例
execp_setid'表名','要转换的字段名'
--*/
ifexists(select*fromdbo.sysobjectswhereid=object_id(N'[dbo].[p_setid]')andOBJECTPROPERTY(id,N'IsProcedure')=1)
dropprocedure[dbo].[p_setid]
GO
CREATEPROCP_SETID
@tbnamesysname, --要处理的表名
@fdnamesysname --要转换为标识字段的字段名
as
declare@s1varchar(8000),@s2varchar(8000),@tmptbsysname
select@s1='',@s2='',@tmptb='[tmp_'+@tbname+'_bak]'
select@s1=@s1+',['+name+']'
+casenamewhen@fdnamethen'=identity(bigint,1,1)'else''end
,@s2=@s2+',['+name+']'
fromsyscolumnswhereobject_id(@tbname)=id
select@s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)
exec('selecttop0'+@s1+'into'+@tmptb+'from['+@tbname+']
setidentity_insert'+@tmptb+'on
insertinto'+@tmptb+'('+@s2+')select'+@s2+'from['+@tbname+']
setidentity_insert'+@tmptb+'off
')
exec('droptable['+@tbname+']')
execsp_rename@tmptb,@tbname
go
/*==========================================================*/
--使用测试
--创建测试的表
createtable表(编号bigint,姓名varchar(10))
insertinto表
select1,'张三'
unionallselect2,'李四'
unionallselect4,'王五'
go
--调用存储过程,将编号字段改为标识字段
execp_setid'表','编号'
go
--显示处理结果
select*from表
--显示是否修改成功
selectnamefromsyscolumns
whereobject_id('表')=idandstatus=0x80
go
--删除测试
droptable表
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- oracle动态更新(将A表中的某个字段的值赋给B表中的某个字段)
- oracle将一个表中的某个字段的值,同步到另外一张表的某个字段中
- 将A表中某个字段的值赋给B表某个字段
- 将某个表的Code字段变成自动编号字段
- [SQL]启动SQLServer的死锁检查
- Drag & Drop 全解析 (一)
- 说说ARM汇编的LDR伪指令
- 【作图】在图片上直接敲入字符
- 并发问题详述
- 将表中的某个字段转换成标识字段,并保留原来的值
- Taking a Bite Out of ASP.NET ViewState
- 系统设计的一些原则
- 修复SQLSERVER2000之实战经验
- Cutting Edge The ASP.NET View State
- 算法资料链接
- 单表导入/导出文本文件
- 流Mini驱动开发指南(选自DDK联机文档)(一)
- 阿尔卡特笔试题目(一)