sql中修改表出错 [英] Error in modify table in sql

查看:98
本文介绍了sql中修改表出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi



i得到此错误

''int''附近的语法不正确




当我修改我的桌子时




 ALTER TABLE上传ALTER COLUMN MODIFY id int IDENTITY(46,1)





帮助我。

解决方案

< blockquote>请此处了解的正确语法ALTER COLUMN 声明。



此外,您不能更改用作主键的列;你必须创建一个具有正确列的全新表,将数据从旧表传输到新表,删除旧表并重命名新表。该过程可能涉及在其他表中抑制/重新创建一些外键。根据您的数据库大小,这可能是一项艰巨的任务。



希望它有意义。


   -   创建测试表 

create table table1(col1 int ,col2 varchar 30 ))

insert into table1 values 100 ' olddata'



- 添加标识列

alter table table1 add col3 int identity 1 1

GO



- 重命名或删除旧列

exec sp_rename ' table1.col1'' oldcol1'' column'



alter table table1 drop column col1

- 将新列重命名为旧列名

exec sp_rename ' table1.col3'' col1'' column'

GO



- 添加新的测试记录和评论表

insert into table1 values ' newdata'

选择 * 来自 table1


hi

i get this Error

Incorrect syntax near ''int''



when i modify my table

ALTER TABLE upload  ALTER  COLUMN MODIFY id int IDENTITY(46,1)



help me .

解决方案

Please this here for correct syntax of ALTER COLUMN statement.

Moreover, you cannot alter a column that is used as primary key ; you have to create a brand new table with correct columns, transfer data from old table to new one, delete old table and rename new one. The process could involve to suppress/recreate some foreign keys in other tables, also. Can be a tough task depending on the size of your database.

Hope it makes sense.


--create test table

create table table1 (col1 int, col2 varchar(30))

insert into table1 values (100, 'olddata')



 --add identity column

alter table table1 add col3 int identity(1,1)

GO



--rename or remove old column

exec sp_rename 'table1.col1', 'oldcol1', 'column'

OR

alter table table1 drop column col1

--rename new column to old column name

exec sp_rename 'table1.col3', 'col1', 'column'

GO



--add new test record and review table

insert into table1 values ( 'newdata')

select * from table1


这篇关于sql中修改表出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆