sql server 2005中如何修改字段名
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql server 2005中如何修改字段名相关的知识,希望对你有一定的参考价值。
参考技术A 一、在sqlserver
2005企业管理器中修改
打开企业管理器,找到要修改的数据表,点右键,选择设计表,在设计视图中修改字段名
二、通过SQL语句
EXEC
sp_rename
‘表名.字段名','更改后的字段名','COLUMN' 参考技术B SQL
Server
调用
EXECUTE
sp_rename
来进行
对数据库表中,列名的变更。
下面的代码,是在
SQL
Server
Management
Studio
里面做修改
然后把
修改所生成的
SQL
语句复制粘贴出来,在
Sqlcmd
下面执行的
操作是把
test_tab
表的
val字段,
修改为
val2
字段。
1>
BEGIN
TRANSACTION
2>
SET
QUOTED_IDENTIFIER
ON
3>
SET
ARITHABORT
ON
4>
SET
NUMERIC_ROUNDABORT
OFF
5>
SET
CONCAT_NULL_YIELDS_NULL
ON
6>
SET
ANSI_NULLS
ON
7>
SET
ANSI_PADDING
ON
8>
SET
ANSI_WARNINGS
ON
9>
COMMIT
10>
BEGIN
TRANSACTION
11>
GO
1>
EXECUTE
sp_rename
N'dbo.test_tab.val',
N'Tmp_val2',
'COLUMN'
2>
GO
注意:
更改对象名的任一部分都可能会破坏脚本和存储过程。
1>
EXECUTE
sp_rename
N'dbo.test_tab.Tmp_val2',
N'val2',
'COLUMN'
2>
GO
注意:
更改对象名的任一部分都可能会破坏脚本和存储过程。
1>
COMMIT
2>
go
test_tab
表的
val字段,
修改为
val2
字段。
SQL
Server
Management
Studio
生成的代码为什么要先把
val
字段修改为
Tmp_val2
然后再把
Tmp_val2
修改为
val2
这个原理还不大清楚。
不过简单的写,就是
EXECUTE
sp_rename
N'dbo.test_tab.val',
N'val2',
'COLUMN'
???Sql Server???SQL SERVER ????????????
?????????int with ?????? serve span ?????? inner -- blog
??????:???Sql Server???SQL SERVER ??????????????????SQL SERVER 2005?????????????????????????????????????????????SQL SERVER 2005???????????????CTE?????????????????????CTE?????????????????????
??????CTE??????????????????Common Table Expression ???SQL SERVER 2005????????????????????????????????????
#??????????????????
1???sql
1 Create table GroupInfo([Id] int,[GroupName] nvarchar(50),[ParentGroupId] int) 2 3 Insert GroupInfo 4 5 select 0,??????????????????,null union all 6 7 select 1,??????????????????,0 union all 8 select 2,??????????????????,1 union all 9 select 3,??????????????????,1 union all 10 select 4,????????????????????????,2 union all 11 select 5,????????????????????????,2 union all 12 select 6,????????????????????????,3 union all 13 select 7,????????????????????????,3 union all 14 15 select 8, ???????????????,0 union all 16 select 9, ?????????????????????,8 union all 17 select 10,????????????????????????,8 union all 18 select 11,???????????????????????????,9 union all 19 select 12,???????????????????????????,9 union all 20 select 13,??????????????????????????????,10 union all 21 select 14,??????????????????????????????,10
2????????????
#????????????Demo
1???????????????????????????????????????????????????,???????????????????????????
1 --???????????????????????????????????????????????? 2 with 3 CTE 4 as 5 ( 6 select * from GroupInfo where Id=1 7 union all 8 select G.* from CTE inner join GroupInfo as G 9 on CTE.Id=G.ParentGroupId 10 ) 11 select * from CTE order by Id
1 --???????????????????????????????????????????????? 2 with 3 CTE 4 as 5 ( 6 select * from GroupInfo where Id=14 7 union all 8 select G.* from CTE inner join GroupInfo as G 9 on CTE.ParentGroupId=G.Id 10 ) 11 select * from CTE order by Id
2?????????????????????
1 --?????????????????? 2 with 3 CTE 4 as 5 ( 6 select Id,GroupName,ParentGroupId,GroupPath=CAST( GroupName as nvarchar(max)) from GroupInfo where Id=1 7 union all 8 select G.*,CAST(CTE.GroupPath+???//???+G.GroupName as nvarchar(max)) as GroupPath from CTE 9 inner join GroupInfo as G 10 on CTE.Id=G.ParentGroupId 11 ) 12 select * from CTE
3?????????????????????????????????????????????????????????
1 --??????id????????????????????????????????????sort??????????????????sort?????????????????????????????????????????????????????? 2 WITH 3 CTE 4 AS 5 ( 6 SELECT * ,CAST(RIGHT(???000??? + CAST([Id] AS VARCHAR), 3) AS VARCHAR(MAX)) AS sort FROM GroupInfo 7 WHERE ParentGroupId = 0 8 UNION ALL 9 SELECT GroupInfo.* ,CAST(sort + RIGHT(???000??? + CAST(GroupInfo.[Id] AS VARCHAR),3) AS VARCHAR(MAX)) AS sort 10 FROM CTE 11 INNER JOIN GroupInfo ON CTE.Id = GroupInfo.ParentGroupId 12 ) 13 SELECT * FROM CTE ORDER BY sort
4?????????????????????????????????????????????????????????
1 --?????????????????? 2 WITH CTE AS ( 3 SELECT *,1 AS [Level] FROM GroupInfo WHERE ParentGroupId=0 4 UNION ALL 5 SELECT G.*,CTE.Level+1 FROM GroupInfo as G 6 JOIN CTE ON CTE.Id =G.ParentGroupId 7 ) 8 SELECT * FROM CTE
以上是关于sql server 2005中如何修改字段名的主要内容,如果未能解决你的问题,请参考以下文章
SQL server 2005如何设置一个或几个字段唯一约束?
怎样在SQL server 2005 的表中插入图片字段为无效值怎么办
sql 检测更新触发器中修改的字段(sql server 2005)?