zoukankan      html  css  js  c++  java
  • 查询Table name, Column name, 拼接执行sql文本, 游标, 存储过程, 临时表

    018_Proc_UpdateTranslations.sql:

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    if (exists (select * from sys.objects where name = 'UpdateTranslationsForTable'))
        drop proc [UpdateTranslationsForTable]
    go
    
    CREATE PROCEDURE [dbo].[UpdateTranslationsForTable]
        (
        @tableName NVARCHAR(128)    
        )
    AS
    BEGIN
    -- --- 
    SET NOCOUNT ON
    -- --- 
    -- --- 
    DECLARE @translationTableName NVARCHAR(128)
    DECLARE @sql NVARCHAR(4000)
    
    -- --- 
    SET @tableName = REPLACE(@tableName, '''', '''''') -- injection protection
    IF @tableName = '_dynamicText'
        BEGIN
            SET @translationTableName = @tableName
        END
    ELSE
        BEGIN
            SET @translationTableName = @tableName + 'Translation'
        END
    
    
            -- ---------------------------------------------------------------------------
            -- --- Build strings of columns that we will insert into / select from
            -- --- Certain columns are NOT to be translated and those are listed here.
            -- --- 
            -- ---------------------------------------------------------------------------
            DECLARE @colTbl TABLE (rowId INT NOT NULL IDENTITY(1,1), columnName NVARCHAR(128), maxLen INT NULL, appendQ TINYINT NOT NULL, PRIMARY KEY (rowId))
            -- --- 
            INSERT INTO @colTbl (columnName, maxLen, appendQ)
            SELECT column_name, character_maximum_length, 
                CASE WHEN data_type = 'nvarchar' AND (character_maximum_length >= 100 OR character_maximum_length = -1)
                    AND column_name NOT IN ('OwnerUserId', 'UserName', 'UserId', 'OwnerUserId', 'AssessingUserName', 
                    'CompletedById', 'TargetAttractionRationale', 'ResourceFileName', 'ResourceURL')
                THEN 1 ELSE 0 END -- this case value indicates to only append ?xx- to certain columns, not these ones listed
            FROM INFORMATION_SCHEMA.COLUMNS 
            WHERE table_name = @translationTableName
            AND column_name NOT IN ('Language', 'Id', 'tVersion','IsSynchronized')
            ORDER BY ordinal_position
            -- --- 
            DECLARE @update NVARCHAR(4000)
            DECLARE @colName NVARCHAR(128)
            DECLARE @conditions NVARCHAR(128)
            DECLARE @appendQ TINYINT
            DECLARE @colLen NVARCHAR(20) -- for nvarchar columns, this is the column size number converted to nvarchar
            DECLARE @minRow INT
            DECLARE @maxRow INT
            SELECT @minRow = MIN(rowId), @maxRow = MAX(rowId) FROM @colTbl
            WHILE (@minRow <= @maxRow)
                BEGIN
                SELECT @colName = columnName, @appendQ = appendQ, @colLen = CAST(CASE WHEN maxLen = -1 THEN 4000 ELSE maxLen END AS VARCHAR) FROM @colTbl WHERE rowId = @minRow
                
                SET @update = ISNULL(@update + ', ', '') + '[' + @colName + ']='+ CASE WHEN @appendQ = 1 THEN 'SUBSTRING([' + @colName + '], 5, ' + @colLen + ')' ELSE @colName END
                SET @conditions = ISNULL(@conditions + 'and ', '') + '[' + @colName + '] like ''?en-%'' '
                
                SET @minRow = @minRow + 1
                END                
            -- ---------------------------------------------------------------------------
            -- --- update certain languages in the translation table
            ------------------------------------------------------------------------------    
    
            SET @sql = N'UPDATE ' + @translationTableName + ' SET '+@update+' where language like ''en-%'' and '+@conditions
    
                
            EXEC sp_executesql @statement = @sql    
            
    
    END

    019_Cur_UpdateTranslations.sql :

    -- ===================================================================================
    -- === Script to update translations for specified languages to be the same as xx-XX in all translations tables
    -- ===================================================================================
    DECLARE @sql NVARCHAR(4000),@tableName NVARCHAR(128)
    -- === 
    PRINT '--============ Starting Update for All Translations ============--'
    -- ===================================================================================
    -- === create a cursor for each sql table that holds translated data
    -- ===================================================================================
    DECLARE tableCursor CURSOR FOR
        -- Select all tables that follow the 'standard' translations system
        SELECT T.name
        FROM sys.tables T
        WHERE EXISTS (SELECT NULL FROM sys.tables TT WHERE TT.name = T.name + 'Translation')
        AND T.name NOT IN ('Permission')    
        -- Add any tables that follow the 'standard' translations system
        UNION SELECT '_dynamicText'
        
        
    OPEN tableCursor
    FETCH tableCursor INTO @tableName
    WHILE @@FETCH_STATUS = 0
        BEGIN
        PRINT ''
        PRINT '------ Updating Translations For ' + @tableName + ' ------'    
        
        EXEC UpdateTranslationsForTable @tableName
        
        PRINT '------ Update Translations For ' + @tableName + ' Done ------'
        
        FETCH tableCursor INTO @tableName
        END
    CLOSE tableCursor
    DEALLOCATE tableCursor
    PRINT '--============ Finished Updating All Translations ============--'
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
     
    if (exists (select * from sys.objects where name = 'UpdateTranslationsForTable'))
        drop proc [UpdateTranslationsForTable]
    go
     
    CREATE PROCEDURE [dbo].[UpdateTranslationsForTable]
    	(
    	@tableName NVARCHAR(128)	
    	)
    AS
    BEGIN
    -- --- 
    SET NOCOUNT ON
    -- --- 
    -- --- 
    DECLARE @translationTableName NVARCHAR(128)
    DECLARE @sql NVARCHAR(4000)
     
    -- --- 
    SET @tableName = REPLACE(@tableName, '''', '''''') -- injection protection
    IF @tableName = '_dynamicText'
    	BEGIN
    		SET @translationTableName = @tableName
    	END
    ELSE
    	BEGIN
    		SET @translationTableName = @tableName + 'Translation'
    	END
     
     
    		-- ---------------------------------------------------------------------------
    		-- --- Build strings of columns that we will insert into / select from
    		-- --- Certain columns are NOT to be translated and those are listed here.
    		-- --- 
    		-- ---------------------------------------------------------------------------
    		DECLARE @colTbl TABLE (rowId INT NOT NULL IDENTITY(1,1), columnName NVARCHAR(128), maxLen INT NULL, appendQ TINYINT NOT NULL, PRIMARY KEY (rowId))
    		-- --- 
    		INSERT INTO @colTbl (columnName, maxLen, appendQ)
    		SELECT column_name, character_maximum_length, 
    			CASE WHEN data_type = 'nvarchar' AND (character_maximum_length >= 100 OR character_maximum_length = -1)
    				AND column_name NOT IN ('OwnerUserId', 'UserName', 'UserId', 'OwnerUserId', 'AssessingUserName', 
    				'CompletedById', 'TargetAttractionRationale', 'ResourceFileName', 'ResourceURL')
    			THEN 1 ELSE 0 END -- this case value indicates to only append ?xx- to certain columns, not these ones listed
    		FROM INFORMATION_SCHEMA.COLUMNS 
    		WHERE table_name = @translationTableName
    		AND column_name NOT IN ('Language', 'Id', 'tVersion','IsSynchronized')
    		ORDER BY ordinal_position
    		-- --- 
    		DECLARE @update NVARCHAR(4000)
    		DECLARE @colName NVARCHAR(128)
    		DECLARE @conditions NVARCHAR(128)
    		DECLARE @appendQ TINYINT
    		DECLARE @colLen NVARCHAR(20) -- for nvarchar columns, this is the column size number converted to nvarchar
    		DECLARE @minRow INT
    		DECLARE @maxRow INT
    		SELECT @minRow = MIN(rowId), @maxRow = MAX(rowId) FROM @colTbl
    		WHILE (@minRow <= @maxRow)
    			BEGIN
    			SELECT @colName = columnName, @appendQ = appendQ, @colLen = CAST(CASE WHEN maxLen = -1 THEN 4000 ELSE maxLen END AS VARCHAR) FROM @colTbl WHERE rowId = @minRow
    			
    			SET @update = ISNULL(@update + ', ', '') + '[' + @colName + ']='+ CASE WHEN @appendQ = 1 THEN 'SUBSTRING([' + @colName + '], 5, ' + @colLen + ')' ELSE @colName END
    			SET @conditions = ISNULL(@conditions + 'and ', '') + '[' + @colName + '] like ''?en-%'' '
    			
    			SET @minRow = @minRow + 1
    			END				
    		-- ---------------------------------------------------------------------------
    		-- --- update certain languages in the translation table
    		------------------------------------------------------------------------------	
     
    		SET @sql = N'UPDATE ' + @translationTableName + ' SET '+@update+' where language like ''en-%'' and '+@conditions
     
    			
    		EXEC sp_executesql @statement = @sql	
    		
     
    END
     
    
  • 相关阅读:
    Java 反射 调用 demo
    Java 使用 DBCP mysql 连接池 做数据库操作
    Java 使用 Dbutils 工具类库 操作mysql
    NodeJs 遍历文件夹内容 上传到服务器.并输出上传记录文件
    Java-JDBC.mysql 工具类 读取本地文件配置
    vue使用axios发送数据请求
    word2vec 理论与实践
    pytorch_SRU(Simple Recurrent Unit)
    Highway Networks Pytorch
    Highway Networks
  • 原文地址:https://www.cnblogs.com/cw_volcano/p/5956410.html
Copyright © 2011-2022 走看看