Domanda

Basically I'm looking to loop through a temp table which lists certain table names which need updated, I take each table name use it to populate another temporary table of all the ID's which are to be updated..

I can select the data in each table needing updated using this structure, but cannot seem to get the inner cursor to run as it isn't picking up the temp table..

Any help would be greatly appreciated as this has been doing my nut in for the past few hours..

Cheers,

    DECLARE @table INT
    DECLARE @prefix nvarchar(3)
    DECLARE @TableName nvarchar(50)
    DECLARE @TableIdName nvarchar(50)
    DECLARE @getTable CURSOR
    SET @getTable = CURSOR FOR
    SELECT DISTINCT(id)
    FROM @t 
    OPEN @getTable
    FETCH NEXT
    FROM @getTable INTO @table
    WHILE @@FETCH_STATUS = 0
    BEGIN

            SELECT @TableName = name FROM @t WHERE id = @table
            SET @TableIdName = @TableName + 'Id'
            SELECT @prefix = prefix FROM @t WHERE name = @TableName 

            --PRINT @table
            PRINT @TableName
            --PRINT @TableIdName
            --PRINT @prefix

            DECLARE @temptable table(rid int, rTableName nvarchar(50), rprefix nvarchar(3), rpk nvarchar(50))

            EXEC ('INSERT INTO ' + @temptable + ' SELECT ' + @TableIdName + ', ' + @TableName + ', ' + @prefix + @TableIdName + ' FROM ' + @TableName)

            DECLARE @rTableName nvarchar(50)
            DECLARE @rpk nvarchar(50)
            DECLARE @rprefix nvarchar(3)
            DECLARE @row INT
            DECLARE @getRow CURSOR
            SET @getRow = CURSOR FOR
            SELECT DISTINCT(rid)
            FROM @temptable
            OPEN @getRow
                    FETCH NEXT
                    FROM @getRow INTO @row
                    WHILE @@FETCH_STATUS = 0
                    BEGIN
                        PRINT @row                          

                        SELECT @rTableName = rTableName FROM @temptable WHERE rid = @row
                        SELECT @rpk = rpk FROM @temptable WHERE rid = @row
                        SELECT @rprefix = rprefix FROM @temptable WHERE rid = @row 

                        EXEC ('UPDATE ' + @rTableName + ' SET CoiRef = ' + @rprefix  + '_' + @row + ' WHERE ' + @rpk + ' = ' + @row) 

                        FETCH NEXT
                        FROM @getRow INTO @row
                    END
                CLOSE @getRow
            DEALLOCATE @getRow

        FETCH NEXT
        FROM @getTable INTO @table
    END
    CLOSE @getTable
    DEALLOCATE @getTable

I have also tried another solution using sp_executesql but get similar errors there, this was using the below code instead on the first EXEC..

    DECLARE @sqlCommand nvarchar(500)
    SET @sqlCommand = 'INSERT INTO @temptable SELECT TableIdName, TableName, prefix, TableIdName FROM' + @TableName

    EXECUTE sp_executesql @sqlCommand, N'@temptable nvarchar(50) output', @temptable OUTPUT

Again, Any help would be greatly appreciated..

Thanks..

Gerry

È stato utile?

Soluzione

I think the error is coming from the fact that you're trying to use a table variable inside of dynamic sql. This is not something that is supported because the table variable is out of scope for the dynamic sql. You should make your @temptable into a temporary table using the create table command.

In the unsolicited advice category, I'd suggest attempting to recreate this without using cursors if at all possible, as cursors go against the concept of set based processing which is the foundation of sql server.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top