将表中的某个字段转换成标识字段,并保留原来的值
来源:互联网 发布:防雾霾口罩哪个好 知乎 编辑:程序博客网 时间:2024/05/19 17:23
/*--将表中的某个字段转换成标识字段,并保留原来的值
注意,因为要删除原表,所以,如果表和其他表的关联,这些关联要重新创建
--邹建 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_'+@tbname+'_bak]'
select @s1=@s1+',['+name+']'
+case name when @fdname then '=identity(bigint,1,1)' else '' end
,@s2=@s2+',['+name+']'
from syscolumns where object_id(@tbname)=id
select @s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)
exec('select top 0 '+@s1+' into '+@tmptb+' from ['+@tbname+']
set identity_insert '+@tmptb+' on
insert into '+@tmptb+'('+@s2+') select '+@s2+' from ['+@tbname+']
set identity_insert '+@tmptb+' off
')
exec('drop table ['+@tbname+']')
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 表
/*--将表中的某个字段转换成标识字段,并保留原来的值
注意,因为要删除原表,所以,如果表和其他表的关联,这些关联要重新创建
--邹建 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_'+@tbname+'_bak]'
select @s1=@s1+',['+name+']'
+case name when @fdname then '=identity(bigint,1,1)' else '' end
,@s2=@s2+',['+name+']'
from syscolumns where object_id(@tbname)=id
select @s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)
exec('select top 0 '+@s1+' into '+@tmptb+' from ['+@tbname+']
set identity_insert '+@tmptb+' on
insert into '+@tmptb+'('+@s2+') select '+@s2+' from ['+@tbname+']
set identity_insert '+@tmptb+' off
')
exec('drop table ['+@tbname+']')
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 表
/*--将表中的某个字段转换成标识字段,并保留原来的值
注意,因为要删除原表,所以,如果表和其他表的关联,这些关联要重新创建
--邹建 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_'+@tbname+'_bak]'
select @s1=@s1+',['+name+']'
+case name when @fdname then '=identity(bigint,1,1)' else '' end
,@s2=@s2+',['+name+']'
from syscolumns where object_id(@tbname)=id
select @s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)
exec('select top 0 '+@s1+' into '+@tmptb+' from ['+@tbname+']
set identity_insert '+@tmptb+' on
insert into '+@tmptb+'('+@s2+') select '+@s2+' from ['+@tbname+']
set identity_insert '+@tmptb+' off
')
exec('drop table ['+@tbname+']')
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 表
/*--将表中的某个字段转换成标识字段,并保留原来的值
注意,因为要删除原表,所以,如果表和其他表的关联,这些关联要重新创建
--邹建 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_'+@tbname+'_bak]'
select @s1=@s1+',['+name+']'
+case name when @fdname then '=identity(bigint,1,1)' else '' end
,@s2=@s2+',['+name+']'
from syscolumns where object_id(@tbname)=id
select @s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)
exec('select top 0 '+@s1+' into '+@tmptb+' from ['+@tbname+']
set identity_insert '+@tmptb+' on
insert into '+@tmptb+'('+@s2+') select '+@s2+' from ['+@tbname+']
set identity_insert '+@tmptb+' off
')
exec('drop table ['+@tbname+']')
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 表
/*--将表中的某个字段转换成标识字段,并保留原来的值
注意,因为要删除原表,所以,如果表和其他表的关联,这些关联要重新创建
--邹建 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_'+@tbname+'_bak]'
select @s1=@s1+',['+name+']'
+case name when @fdname then '=identity(bigint,1,1)' else '' end
,@s2=@s2+',['+name+']'
from syscolumns where object_id(@tbname)=id
select @s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)
exec('select top 0 '+@s1+' into '+@tmptb+' from ['+@tbname+']
set identity_insert '+@tmptb+' on
insert into '+@tmptb+'('+@s2+') select '+@s2+' from ['+@tbname+']
set identity_insert '+@tmptb+' off
')
exec('drop table ['+@tbname+']')
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 表
/*--将表中的某个字段转换成标识字段,并保留原来的值
注意,因为要删除原表,所以,如果表和其他表的关联,这些关联要重新创建
--邹建 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_'+@tbname+'_bak]'
select @s1=@s1+',['+name+']'
+case name when @fdname then '=identity(bigint,1,1)' else '' end
,@s2=@s2+',['+name+']'
from syscolumns where object_id(@tbname)=id
select @s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)
exec('select top 0 '+@s1+' into '+@tmptb+' from ['+@tbname+']
set identity_insert '+@tmptb+' on
insert into '+@tmptb+'('+@s2+') select '+@s2+' from ['+@tbname+']
set identity_insert '+@tmptb+' off
')
exec('drop table ['+@tbname+']')
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 表
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- 将表中的某个字段转换成标识字段,并保留原来的值
- oracle动态更新(将A表中的某个字段的值赋给B表中的某个字段)
- oracle将一个表中的某个字段的值,同步到另外一张表的某个字段中
- 将A表中某个字段的值赋给B表某个字段
- 将某个表的Code字段变成自动编号字段
- 搜索某个字符串在那个表的那个字段中
- 显示数据库中的表结构(新增了索引及表的描述信息)
- VB实现SQL Server数据库备份/恢复
- SQL Server数据库检修
- 压缩数据库日志
- 将表中的某个字段转换成标识字段,并保留原来的值
- 单表导入/导出文本文件
- 批量将一个库里的所有表里的char改成nchar类型
- 数据库管理员DBA
- 查询区分大小写
- 比较两个数据库的表结构差异
- 查询指定的表在那些数据库中存在
- 将表/查询存储为标准的XML文件
- 查询数据库中,表结构的详细信息