SQL Server 2005 - T-SQL 根据参数增加字段大小
我想知道是否有一种方法可以循环遍历表中的所有列并根据当前大小增加每个字段的大小。基本上,我需要将当前字段大小低于 1000 个字符的所有字段设置为 1000 个字符。我可以对我想要更改的每个字段执行类似的操作
ALTER TABLE tableName ALTER COLUMN nvarchar(1000)
,但是有数百个字段,如果可能的话,我想以编程方式执行此操作。谢谢。
I'm wondering if there is a way to loop through all of the columns in a table and increase the size of each field based on what the current size. Basically, I need any fields that currently have a field size under 1000 characters to be set to 1000 characters. I can do something like
ALTER TABLE tableName ALTER COLUMN nvarchar(1000)
for each field that I want changed, but there are hundreds of fields and I'd like to do it programmatically if possible. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用它来生成所有 ALTER TABLEs
使用系统视图更容易。
Use this to generate all your ALTER TABLEs
It's easier with the system views.
下面是一些循环遍历命名表的所有列的代码。它只是选择他们的详细信息;你需要填写你在循环中所做的事情。
您在 INFORMATION_SCHEMA.COLUMNS 视图中感兴趣的字段是“DATA_TYPE”和“CHARACTER_MAXIMUM_LENGTH”
我想我还假设您所做的更改不会改变列序号,但如果它们确实改变了您可以通过其他方式选择它们。
Here's some code to loop through all of the columns of a named table. It just selects their details; you need to fill out what you do in the loop.
The fields that you'll be interested in in the INFORMATION_SCHEMA.COLUMNS view are 'DATA_TYPE' and 'CHARACTER_MAXIMUM_LENGTH'
I guess that I'm also making the assumption that the changes you make won't alter the column ordinals, but if they did you could select them in another way.