T-SQL:将 datetime2 类型的所有列的 datatime2 转换为 datetime
我有一个充满 datetime2 列的数据库,需要将其移至 SQL 2005 数据库。因此,我需要将所有这些 datetime2(7) 列转换为日期时间。
我该怎么做呢?
现在,我已经设法为具有 datetime2 数据类型的所有列选择表名和列名,如下所示:
SELECT t.name, c.name, i.DATA_TYPE
FROM sys.tables AS t
JOIN sys.columns c ON t.object_id = c.object_id
JOIN information_schema.columns i ON i.TABLE_NAME = t.name AND i.COLUMN_NAME = c.name
WHERE i.data_type = 'datetime2'
我只是不知道如何执行其余操作。
I've got a database full of datetime2 columns than needs to be moved to a SQL 2005 database. So, I need to convert all these datetime2(7) columns to datetime.
How can I go about doing this?
Right now I've managed to select the table name and column name for all columns with the datetime2 datatype like this:
SELECT t.name, c.name, i.DATA_TYPE
FROM sys.tables AS t
JOIN sys.columns c ON t.object_id = c.object_id
JOIN information_schema.columns i ON i.TABLE_NAME = t.name AND i.COLUMN_NAME = c.name
WHERE i.data_type = 'datetime2'
I just don't know how to do the rest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
...然后您使用 CURSOR 迭代您的结果并动态运行 DDL,如下所示:
以便您得到与此类似的内容(未测试):
编辑:还添加了空性检查:
... then you iterate over your results with the CURSOR and dynamically run the DDL like:
so that you get something similar to this (not tested):
Edit: added null-ability check as well:
改进了上面的答案以适应模式
Improved the above answer to cater for schemas
我知道这个帖子已经很旧了,但我今天也在做同样的事情,只是想提供我的技术。每当我需要执行大量 DDL 语句时,我都会创建一个 TSQL 来生成所需的 TSQL,然后将结果复制到查询窗口中并运行它。您不需要像@van建议一样编写所有光标代码(尽管效果很好)。
因此,对于您的情况,只需运行 sql 语句即可:
然后,将结果复制到新的查询窗口中并运行它。有时您需要在命令之间的自己的行上添加“GO”语句。如果是这样,请将
char(13) + 'GO'
添加到输出字符串中。另外,请确保在 SQL Mgmt Studio 中使用“结果到文本”选项而不是“结果到网格”选项运行查询。
I know this thread is old but I'm doing the same thing today and just wanted to offer up my technique. Whenever I need to do a lot of DDL statements I create one TSQL that generates the TSQL that's needed and then just copy the results into the Query window and run it. You don't need to write all the cursor code like @van suggestion (although that works fine).
So, for your situation, just run the sql statement:
Then, copy the results into a new query window and run it. Sometimes you need to add
"GO"
statements on their own line between commands. If so, addchar(13) + 'GO'
into your output string.Also, make sure to run the query in SQL Mgmt Studio with the "Results to Text" option instead of the "Results to Grid" option.
今天需要对架构中的所有用户表执行此操作,并且对任何现有答案都不满意。特别是,我的一些日期时间列有默认值,实际上没有人需要,但阻碍了 ALTER TABLE 命令。因此,我编写了一个脚本,仅删除这些默认值,然后更改列。它保留可为空性,并且可以处理包含空格、连字符等的名称。注意,它不会在之后重新创建默认值。
如果您遇到同样的情况,您可以使用这个稳定且经过测试的脚本,它还确保用于组成 DDL 语句的 nvarchar(max) 变量不会被静默截断:
它使用古老的语法和模式表,因此它从 SQL Server 2008 版(这是第一个支持
datetime2
)到 2016 年一直有效。Needed to do this today for all user tables in a schema, and was not satisfied with any of the existing answers. Especially, some of my datetime columns had defaults, which actually nobody needed, but hindered ALTER TABLE commands. So I wrote a script that just drops those defaults, and then changes the columns. It preserves nullability, and can handle names containing spaces, hyphens etc. Caution, it does not recreate the defaults afterwards.
If you're in the same situation, you can use this stable and tested script, which also makes sure that there is no silent truncation of the nvarchar(max) variable used to compose the DDL statements:
It uses ancient syntax and schema tables, so it is working from SQL Server version 2008 (which was the first to support
datetime2
) thru 2016.