国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

將表中的某個字段轉換成標識字段,并保留原來的值

2024-07-21 02:08:25
字體:
來源:轉載
供稿:網友

/*--將表中的某個字段轉換成標識字段,并保留原來的值

 注意,因為要刪除原表,所以,如果表和其他表的關聯,這些關聯要重新創建

--鄒建 2003.12--*/

/*--調用示例

 exec p_setid '表名','要轉換的字段名'
--*/

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[p_setid]') and objectproperty(id, n'isprocedure') = 1)
 drop procedure [dbo].[p_setid]
go

create proc p_setid
@tbname sysname, --要處理的表名
@fdname sysname  --要轉換為標識字段的字段名
as
declare @s1 varchar(8000),@s2 varchar(8000),@tmptb sysname
select @s1='',@s2='',@tmptb='[tmp_'[email protected]+'_bak]'
select @[email protected]+',['+name+']'
 +case name when @fdname then '=identity(bigint,1,1)' else '' end
 ,@[email protected]+',['+name+']'
from syscolumns where object_id(@tbname)=id
select @s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)
exec('select top 0 '[email protected]+' into '[email protected]+' from ['[email protected]+']
set identity_insert '[email protected]+' on
insert into '[email protected]+'('[email protected]+') select '[email protected]+' from ['[email protected]+']
set identity_insert '[email protected]+' off
')
exec('drop table ['[email protected]+']')
exec sp_rename @tmptb,@tbname
go

 

/*==========================================================*/

--使用測試

--創建測試的表
create table 表(編號 bigint,姓名 varchar(10))
insert into 表
select 1,'張三'
union all select 2,'李四'
union all select 4,'王五'
go

--調用存儲過程,將編號字段改為標識字段
exec p_setid '表','編號'
go

--顯示處理結果
select * from 表

--顯示是否修改成功
select name from syscolumns
where object_id('表')=id and status=0x80
go

--刪除測試
drop table 表

國內最大的酷站演示中心!
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐至县| 渝北区| 霍邱县| 德惠市| 施秉县| 安丘市| 永济市| 九寨沟县| 浮梁县| 栾城县| 东明县| 巴楚县| 滦平县| 肥城市| 绥德县| 台东市| 林芝县| 墨脱县| 牡丹江市| 福建省| 浦北县| 东阿县| 寻乌县| 永康市| 桃园市| 辉南县| 松溪县| 凤庆县| 肇东市| 额尔古纳市| 榆树市| 屏山县| 六盘水市| 五大连池市| 建瓯市| 安塞县| 兴文县| 新绛县| 玉龙| 华蓥市| 长寿区|