更改 SQL Server 中的所有列数据类型
当我在 SQL 中导入表时,它建议使用真实数据类型,现在我想将所有列更改为双精度型...
是否有任何脚本可以在 SQL Managment Studio 中自动执行此操作
我的表有 500 列:
`After doing` EXECUTE sp_help traS
Col Type Comp len Prec Scale Nullable TrimTrailing Fixed Collation
-------------------------------------------------------------------------------
x1 real no 4 24 NULL yes (n/a) (n/a) NULL
x2 real no 4 24 NULL yes (n/a) (n/a) NULL
x3 real no 4 24 NULL yes (n/a) (n/a) NULL
x4 real no 4 24 NULL yes (n/a) (n/a) NULL
...
x500 real no 4 24 NULL yes (n/a) (n/a) NULL
When I imported a table in SQL it suggested real datatype, now I would like to change all columns to double type...
Is there any script to automatically do this in SQL Managment Studio
My table is 500 columns:
`After doing` EXECUTE sp_help traS
Col Type Comp len Prec Scale Nullable TrimTrailing Fixed Collation
-------------------------------------------------------------------------------
x1 real no 4 24 NULL yes (n/a) (n/a) NULL
x2 real no 4 24 NULL yes (n/a) (n/a) NULL
x3 real no 4 24 NULL yes (n/a) (n/a) NULL
x4 real no 4 24 NULL yes (n/a) (n/a) NULL
...
x500 real no 4 24 NULL yes (n/a) (n/a) NULL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下代码将列列表放入名为
@cols
的临时表中,循环遍历该表,生成alter table alter column
语句,并针对每列执行该语句。如果需要排除列,则应将这些列包含在从 information_schema.columns 中进行选择的
NOT IN
谓词中。The following code will place a list of columns into a temporary table called
@cols
, loop through that table, generate analter table alter column
statement, and execute it for each column.If you need to exclude columns, you should include those in the
NOT IN
predicate of the select from information_schema.columns.粗略的伪代码如下所示。不过它未经测试,因为我没有方便的虚拟机,
橙色条表示我速度太慢,但无论如何我都会提交,因为我花了太多时间打字;)
Rough psuedocode would look like the following. It is untested however as I don't have a VM handy
Orange bar overhead says I'm too slow but I'll submit anyways as I spent far too much time typing ;)
感谢bilinkc!
这就是我的解决方案。
我让它在 MSSQL 2019 上工作并添加了 is_nullable 标志。
因此,使用此脚本,您可以更改指定表中的所有列,并更改数据类型,包括可空和 >不能正确为空字段。
Thanks to bilinkc!
That was the solution for me.
I made it work on MSSQL 2019 and added the is_nullable flag.
So with this script you can alter all columns in the named table and change the datatype including nullable and not nullable fields correctly.