如何使用sql脚本更改列的属性 [英] How to alter a column's attribute using sql script

查看:165
本文介绍了如何使用sql脚本更改列的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用sql脚本更改列的属性?



这是我试过的但是我遇到了错误:

  ALTER TABLE [dbo]。[tblBiometricPattern] COLUMN BiometricPatternID TINYINT NOT NULL IDENTITY(1,1)
这是执行时显示的错误讯息:

 关键字COLUMN附近的语法不正确。 


解决方案

如果您试图更改列,它是一个IDENTITY列...你不能这样做。您可以使用identity属性添加新列,但不能更改现有列。



如果这不是您想要做的,也许您可​​以






修改现有的错误讯息的一般形式列是:

  ALTER TABLE [dbo]。[tblBiometricPattern] ALTER COLUMN BiometricPatternID TINYINT NOT NULL IDENTITY $ b  

(也就是说,您在COLUMN之前缺少ALTER字样)。但是,正如我所说,现在将返回一个错误,告诉您无法更改列的IDENTITY属性。






如果列已经是 标识列,而您只是更改了数据类型,那么请关闭IDENTITY()属性。它仍然是一个标识列:

  ALTER TABLE [dbo]。[tblBiometricPattern] ALTER COLUMN BiometricPatternID TINYINT NOT NULL 


How can I alter a column's attribute using a sql script?

Here's what I've tried but I got errors:

ALTER TABLE [dbo].[tblBiometricPattern] COLUMN BiometricPatternID TINYINT NOT NULL IDENTITY(1,1)

Thank you in advance.

Here's the error message that appears when executed:

Incorrect syntax near the keyword 'COLUMN'.

解决方案

If you're trying to alter the column so that it's an IDENTITY column... you can't do that. You can add a new column with the identity property, but you can't alter an existing column.

If that's not what you're trying to do, perhaps you could include the actual error messages you're getting.


The general form for altering an existing column is:

ALTER TABLE [dbo].[tblBiometricPattern] ALTER COLUMN BiometricPatternID TINYINT NOT NULL IDENTITY(1,1)

(that is, you were missing the word "ALTER" before COLUMN). But as I say, this will now return an error telling you that you can't change the IDENTITY property of the column.


If the column is already an identity column, and you're just altering the datatype, then leave off the IDENTITY() property. It will still be an identity column:

ALTER TABLE [dbo].[tblBiometricPattern] ALTER COLUMN BiometricPatternID TINYINT NOT NULL

这篇关于如何使用sql脚本更改列的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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