【问题标题】:How to Change All SQL Columns of money and smallmoney into decimal?如何将 money 和 smallmoney 的所有 SQL 列更改为十进制?
【发布时间】:2017-01-05 11:14:31
【问题描述】:

我尝试使用this question 的答案,将其修改为以下内容,但出现错误:

fetch next from maxcols into @schema, @table, @col, @dtype

消息 8114,第 16 级,状态 5,第 25 行
将数据类型 nvarchar 转换为十进制时出错。

代码:

declare @schema nvarchar(255)
declare @table nvarchar(255)
declare @col nvarchar(255)
declare @dtype decimal(19,8)
declare @sql nvarchar(max)

declare maxcols cursor for
    select
        c.TABLE_SCHEMA, c.TABLE_NAME,
        c.COLUMN_NAME, c.DATA_TYPE
    from
        INFORMATION_SCHEMA.COLUMNS c
    inner join 
        INFORMATION_SCHEMA.TABLES t on c.TABLE_CATALOG = t.TABLE_CATALOG
                                    and c.TABLE_SCHEMA = t.TABLE_SCHEMA
                                    and c.TABLE_NAME = t.TABLE_NAME
                                    and t.TABLE_TYPE = 'BASE TABLE'
    where
        c.DATA_TYPE like '%money'

open maxcols

fetch next from maxcols into @schema, @table, @col, @dtype

while @@FETCH_STATUS = 0
begin
    set @sql = 'alter table [' + @schema + '].[' + @table + 
        '] alter column [' + @col + '] ' + @dtype
    exec sp_executesql @sql

    fetch next from maxcols into @schema, @table, @col, @dtype
end

close maxcols
deallocate maxcols

【问题讨论】:

    标签: sql sql-server tsql


    【解决方案1】:

    一些变化:

    声明 SYSNAME 类型的 @dtype 变量并分配一个您要使用的数据类型的字符串,在您的情况下,它将是一个字符串 'decimal(19,8)。您正在声明一个 decimal(19,8) 数据类型的变量,这不是您想要的。

    您真的应该声明所有SYSNAME 类型的变量,这些变量将用于保存 SQL Server 对象名称、架构、表、列名称等。

    此外,在您的游标选择中,您不需要使用数据类型,它在游标的选择语句的where 子句中被过滤掉。所以我从选择和Fetch next into 行中取出了数据类型列。

    还可以使用QUOTENAME() 函数在对象名称周围添加方括号。

    declare  @table     SYSNAME
          ,  @col       SYSNAME 
          ,  @dtype     SYSNAME = 'decimal(19,8)'  --<-- Declare variable of sysname type
          ,  @sql       NVARCHAR(MAX) 
          ,  @schema    SYSNAME;
    
        declare maxcols cursor for
        select
            c.TABLE_SCHEMA,
            c.TABLE_NAME,
            c.COLUMN_NAME
        from
        INFORMATION_SCHEMA.COLUMNS c
        inner join INFORMATION_SCHEMA.TABLES t on
            c.TABLE_CATALOG = t.TABLE_CATALOG
            and c.TABLE_SCHEMA = t.TABLE_SCHEMA
            and c.TABLE_NAME = t.TABLE_NAME
            and t.TABLE_TYPE = 'BASE TABLE'
        where
            c.DATA_TYPE like '%money'
    
        open maxcols
    
        fetch next from maxcols into @schema, @table, @col
    
        while @@FETCH_STATUS = 0
        begin
            set @sql = N' alter table ' + QUOTENAME(@schema) + '.' + QUOTENAME(@table) 
                     + N' alter column ' + QUOTENAME(@col) + ' ' + @dtype
    
            exec sp_executesql @sql
    
            fetch next from maxcols into @schema, @table, @col
        end
    
        close maxcols
        deallocate maxcols
    

    【讨论】:

    • 如何在尝试更改每列之前删除默认约束?
    猜你喜欢
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    • 2021-09-29
    • 2012-02-21
    • 1970-01-01
    • 2021-03-14
    • 2015-01-26
    • 2018-06-05
    相关资源
    最近更新 更多