如何根据表格单元格的列类型修改表格单元格?

时间:2019-06-25 10:32:04

标签: sql sql-server excel

我想根据列的类型更新SQL表中的单元格。更具体地说,我想根据列类型用'',0,0.0或'1900-01-01'替换NULL值。该表有60列,因此手动处理比较麻烦。

我尝试过的方法使用元数据,并且我想生成一个更新脚本。

用例是用于导入Excel的,其中必须以与列类型相关的方式处理NULL值。

2 个答案:

答案 0 :(得分:1)

如评论中所述,最简单的方法似乎是使用wait.Until(ElementIsVisible(FirstTextField)); 。 (我发现)唯一的例外是'' / decimal,不允许进行隐式和显式转换。

如下所示,您可以写出该语句(但我不包括numeric列)或动态执行该语句:

decimal

db<>fiddle

如果您使用的是SQL Server 2017+,则可以通过使用USE Sandbox; GO CREATE TABLE TestTable (ID int IDENTITY(1,1), SomeInt int, SomeDecimal decimal(10,2), SomeFloat float, SomeMoney money, SomeDate date, SomeDatetime datetime, SomeTime time, SomeDatetime2 datetime2(1), SomeVarbinary varbinary(12), SomeVarchar varchar(10), SomeBit bit); INSERT INTO dbo.TestTable (SomeInt, SomeDecimal, SomeFloat, SomeMoney, SomeDate, SomeDatetime, SomeTime, SomeDatetime2, SomeVarbinary, SomeVarchar, SomeBit) VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL), (NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL); GO UPDATE dbo.TestTable SET SomeInt = ISNULL(SomeInt,''), --SomeDecimal = ISNULL(SomeDecimal,''), SomeFloat = ISNULL(SomeFloat,''), SomeMoney = ISNULL(SomeMoney,''), SomeDate = ISNULL(SomeDate,''), SomeDatetime = ISNULL(SomeDatetime,''), SomeTime = ISNULL(SomeTime,''), SomeDatetime2 = ISNULL(SomeDatetime2,''), SomeVarbinary = ISNULL(SomeVarbinary,CONVERT(varbinary,'')), SomeVarchar = ISNULL(SomeVarchar,''), SomeBit = ISNULL(SomeBit,'') WHERE ID = 1; GO DECLARE @Schema sysname = N'dbo', @Table sysname = N'TestTable'; DECLARE @SQL nvarchar(MAX); SET @SQL = N'UPDATE ' + QUOTENAME(@Schema) + N'.' + QUOTENAME(@Table) + NCHAR(13) + NCHAR(10) + N'SET ' + STUFF((SELECT N',' + NCHAR(13) + NCHAR(10) + N' ' + QUOTENAME(C.COLUMN_NAME) + N' = ISNULL(' + QUOTENAME(C.COLUMN_NAME) + N',' + CASE C.DATA_TYPE WHEN N'decimal' THEN '0)' WHEN N'numeric' THEN '0)' ELSE N'CONVERT(' + QUOTENAME(C.DATA_TYPE) + N',N''''))' END FROM INFORMATION_SCHEMA.COLUMNS C WHERE C.TABLE_SCHEMA = @Schema AND C.TABLE_NAME = @Table AND C.COLUMN_NAME != 'ID' --You'll need a better method to ignore your ID column FOR XML PATH(N''),TYPE).value('.','nvarchar(MAX)'),1,7,N'') + NCHAR(13) + NCHAR(10) + N'WHERE ID = 2;'; --you won't want this if you're doing every row. PRINT @SQL; EXEC sp_executesql @SQL; GO SELECT * FROM dbo.TestTable; GO DROP TABLE dbo.TestTable 而不是STRING_AGGFOR XML PATH来简化上述内容,以在列表中创建列的“定界”列表。桌子。

答案 1 :(得分:-1)

SELECT COLUMN_NAME AS colname, DATA_TYPE AS coltype
INTO #result_columns
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'mytable';


WITH cte(colname, default_value) AS (

SELECT colname,

    CASE
        WHEN coltype IN ('varchar','nvarchar') THEN ''''''
        WHEN coltype IN ('int','money') THEN '0'
        WHEN coltype IN ('date') THEN '''1900-01-01'''
        WHEN coltype IN ('float') THEN '0.0'
    END AS default_value
FROM #result_columns

) 
SELECT 'UPDATE mytable SET ' + colname + '=' + default_value + ' WHERE ' + colname + ' IS NULL;' 

FROM cte;

-结果:

UPDATE mytable SET Caracteristique='' WHERE Caracteristique IS NULL;
UPDATE mytable SET Couleur='' WHERE Couleur IS NULL;
UPDATE mytable SET Forme='' WHERE Forme IS NULL;
UPDATE mytable SET Machine='' WHERE Machine IS NULL;
UPDATE mytable SET QteChargee=0.0 WHERE QteChargee IS NULL;