2011-08-13 34 views
4

当我进口表在SQL它建议真正的数据类型,现在我想改变所有列双师型 ...Alter在SQL Server中的所有列的数据类型

是否有任何脚本来自动执行此操作在SQL枭雄工作室

我的表是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 
+0

嗯,游标怎么样,不明白... – cMinor

回答

7

下面的代码将放置列的列表到一个临时表称为@cols,遍历该表,生成alter table alter column声明,并为每列执行它。

如果您需要排除列,则应该包含那些位于从information_schema.columns中选择的NOT IN谓词中的列。

declare @cols table (i int identity, colname varchar(100)) 
insert into @cols 
select column_name 
from information_schema.COLUMNS 
where TABLE_NAME = 'yourtable' 
and COLUMN_NAME not in ('exclude1', 'exclude2') 

declare @i int, @maxi int 
select @i = 1, @maxi = MAX(i) from @cols 

declare @sql nvarchar(max) 

while(@i <= @maxi) 
begin 
    select @sql = 'alter table yourtable alter column ' + colname + ' decimal(18,4) NULL' 
    from @cols 
    where i = @i 

    exec sp_executesql @sql 

    select @i = @i + 1 
end 
+0

+1,但我建议操作第一次访问此链接:http://www.sommarskog.se/dynamic_sql.html – Lamak

+0

是否有任何权限是必要的,当执行你的代码时,我得到:'(500行受影响) Msg 156,Level 15,State 1,Line 1 关键字'NULL'附近的语法不正确。 Msg 156,Level 15,State 1,Line 1 关键字'NULL'附近的语法不正确。 Msg 156,Level 15,State 1,Line 1 关键字'NULL'附近的语法不正确。 Msg 156,Level 15,State 1,Line 1 关键字'NULL'附近的语法不正确。 Msg 156,Level 15,State 1,Line 1 ..... 关键字'NULL'附近的语法不正确。 Msg 156,Level 15,State 1,Line 1' – cMinor

+0

此外,我只尝试了一个而不是动态的'alter table traS alter column x1 double null'但错误显示:错误的语法near NULL期望ID。 – cMinor

1

粗糙的伪代码如下所示。这是未经测试但是我没有VM方便

-- Create a cursor that will iterate through 
-- all the rows that meet the criteria DECLARE csr CURSOR FOR 
-- This query attempts to define the set of columns 
-- that are reals 
SELECT 
    SC.name AS column_name 
FROM 
    sys.tables ST 
    INNER JOIN 
     sys.columns SC 
     ON SC.object_id = ST.object_id 
    INNER JOIN 
     sys.types T 
     -- these column names are close but not right 
     ON T.type_id = SC.system_type_id 
WHERE 
    -- make this your table name 
    ST.name = 'traS' 
    -- look at actual values in sys.types 
    AND T.name = 'real' 

DECLARE 
    -- this holds the current column name 
    @column_name sysname 
, @base_query varchar(max) 
, @actual_query varchar(max) 

-- template query for fixing what's buggered 
SET @base_query = 'ALTER TABLE traS ALTER COLUMN [<X/>] decimal(18,2) NULL' 

FETCH NEXT FROM csr 
INTO @column_name 
WHILE (@@FETCH_STATUS <> -1) BEGIN 
    IF (@@FETCH_STATUS <> -2) 
    BEGIN 
     BEGIN TRY 
      SET @actual_query = REPLACE(@base_query, '<X/>', @column_name) 
      EXECUTE (@actual_query) 
     END TRY 
     BEGIN CATCH 
      PRINT 'Failed executing statement ' 
      PRINT @actual_query 
     END CATCH 
    END 
    FETCH NEXT FROM csr 
    INTO @colum_name 
END 
CLOSE csr 
DEALLOCATE csr 

橙色条架空说我太慢了,但我会提交反正我花了太多的时间打字;)

+0

一个问题你如何获得'C.name'? – cMinor

+0

应该是SC,因为sys.column表将保存该列的名称。对于那个 – billinkc

+0

固定的查询也可能想要阅读这个线程并找出你的应用需要什么数据类型http://stackoverflow.com/questions/1209181/what-represents-a-double-in-sql-server – billinkc

相关问题