SQL修改列数据类型

SQL修改列数据类型,第1张

用的什么数据库
=================================
不同的数据库对SQL语句的兼容性能不一样,你的语句我在SQL Server 2000上测试通过,并没有出错,至于你说的postgresql82数据库我没用过,你觉得你需要仔细阅读一下postgresql82的手册

SQLite 仅仅支持 ALTER TABLE 语句的一部分功能,我们可以用 ALTER TABLE 语句来更改一个表的名字,也可向表中增加一个字段(列),但是我们不能删除一个已经存在的字段,或者更改一个已经存在的字段的名称、数据类型、限定符等等。
改变表名 - ALTER TABLE 旧表名 RENAME TO 新表名
增加一列 - ALTER TABLE 表名 ADD COLUMN 列名 数据类型 限定符
数据表结构变了就要重建表,好像有其他方法解决,但这偶就不知道了

select convert(转化的类型,数据库字段)as 从命名
from 数据库的表名
如:
select convert(int,Userid)as uid
from UserTable
select convert(nvarchar,Userid)as uid
from UserTable
select convert(nvarchar,UserName ) as uName
from UserTable

convert的语法,希望给楼主带来帮助

首先说明一下: 在mysql数据库中可以对表的字段类型进行修改的,这样的好处是正常情况下原来的数据不会丢失的。
它的语法规则是:alter table newexample modify id vaechar(20);
这里详细的讲解一下,这里同样修改表是以alter table 开始然后是表名 接着是要修改的字段名 ,接着是要修改以后的类型。
来个实例讲解一下:alter table newexample modify id vaechar(20);

这个不知道是不是符合你的意思
use 数据库名
go
declare @tname varchar(50),@tnum int,@cname varchar(50)
set @tnum=(select count(1) from information_schemacolumns where data_type=‘timestamp’)
while @tnum>0
begin
select top 1 @tname=table_name,@cname=column_name from information_schemacolumns where data_type=‘timestamp’ order by table_name
print @tname+' '+@cname
exec ('alter table '+@tname+' alter column '+@cname+' binary(8)')
set @tnum=@tnum-1
end


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12828028.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-28
下一篇 2023-05-28

发表评论

登录后才能评论

评论列表(0条)

保存