如何在存储过程中将所有带有空字符串的列值更新为null?

时间:2019-01-08 11:54:57

标签: sql sql-server stored-procedures

我要用null更新整个数据库中的所有空字符串吗?我已在存储过程中使用select into #temptable从源到目标提取值。但是数据库中有许多空字符串。现在我需要用Null更新那些空字符串。在存储过程中是否可以选择一次更新所有列。请帮助我

2 个答案:

答案 0 :(得分:1)

您可以执行此结果。它将使用update语句为您提供仅可空字符串列的列表:

SELECT
    'UPDATE ' +QUOTENAME(C.TABLE_SCHEMA)+'.'+ QUOTENAME(C.TABLE_NAME) + ' SET ' +  QUOTENAME(C.COLUMN_NAME) + '=NULL WHERE ' + QUOTENAME(C.COLUMN_NAME) + '='''';'
FROM INFORMATION_SCHEMA.TABLES T
INNER JOIN INFORMATION_SCHEMA.COLUMNS C ON T.TABLE_SCHEMA = C.TABLE_SCHEMA AND T.TABLE_NAME = C.TABLE_NAME
WHERE T.TABLE_TYPE = 'BASE TABLE'
AND C.DATA_TYPE LIKE '%CHAR'
AND C.IS_NULLABLE = 'YES'

答案 1 :(得分:0)

您可以做的就是这样。...很耗费时间,但可能会有所帮助:

router.beforeEach((to, from, next) => {

    axios.$http.get('http://localhost:3000/me', {
    headers: {
        'x-access-token': $cookies.get('user_session') //the token is a variable which holds the token
    }
    })
    .then(response => {
        if (response.status == "200") {
            console.log('tet3');
        }
    }, response => {
        this.$cookies.remove("user_session");
        this.$router.push('/')
        //window.location.href = "/";
        console.log('erreur', response)
    })


if (to.matched.some(record => record.meta.requiresAuth)) {

    if (!$cookies.get('user_session')) {
        next({
            path: '/login',
            params: {nextUrl: to.fullPath}
        })
    } else {
        let user = JSON.parse(localStorage.getItem('user'))
        if (to.matched.some(record => record.meta.is_admin)) {
            if (user.is_admin == 1) {
                next()
            }
            else {
                next({name: 'userboard'})
            }
        }
        else {
            next()
        }
    }
} else if (to.matched.some(record => record.meta.guest)) {
    if ($cookies.get('user_session') == null) {
        next()
    }
    else {
        next({name: 'userboard'})
    }
} else {
    next()
}
})

这将向您显示在何处(表和列)有空字符串值(请记住,有不同类型的...空字符串)

然后只需浏览每个表并运行它

DECLARE @SearchStr nvarchar(100)
SET @SearchStr = ''



    CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

    SET NOCOUNT ON

    DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
    SET  @TableName = ''
    SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

    WHILE @TableName IS NOT NULL

    BEGIN
        SET @ColumnName = ''
        SET @TableName = 
        (
            SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
            FROM     INFORMATION_SCHEMA.TABLES
            WHERE         TABLE_TYPE = 'BASE TABLE'
                AND    QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
                AND    OBJECTPROPERTY(
                        OBJECT_ID(
                            QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                             ), 'IsMSShipped'
                               ) = 0
        )

        WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)

        BEGIN
            SET @ColumnName =
            (
                SELECT MIN(QUOTENAME(COLUMN_NAME))
                FROM     INFORMATION_SCHEMA.COLUMNS
                WHERE         TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                    AND    TABLE_NAME    = PARSENAME(@TableName, 1)
                    AND    DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
                    AND    QUOTENAME(COLUMN_NAME) > @ColumnName
            )

            IF @ColumnName IS NOT NULL

            BEGIN
                INSERT INTO #Results
                EXEC
                (
                    'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) FROM ' + @TableName + ' (NOLOCK) ' +
                    ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
                )
            END
        END   
    END

    SELECT distinct ColumnName, ColumnValue FROM #Results

DROP TABLE #Results

您可以通过不删除#results表并向其添加row_number,然后创建循环序列来使事物按顺序更新来重用它。希望这可以有所帮助,但这可能就是我会做的。