有没有更有效的方法来编写这个4x嵌套游标循环?

时间:2016-08-16 20:33:52

标签: sql-server tsql recursion

这是我的代码。

它应该做的是查看[parentChildRelationship]中的每条记录,并且记录是否已定义列[parent_table_name]以检查并查看是否在父表中找到每条记录的childtable.childcolumn值。如果没有标记错误标志并将错误消息写入问题记录,则不执行任何操作。 每个childColumn可以有多个父级。

一切正常,但我不得不想象有更好的方法来写这个......

BEGIN

    SET NOCOUNT ON;
    DECLARE @cTableName nvarchar(max)

    DECLARE c1 CURSOR FOR
        SELECT DISTINCT tableName FROM [phil_ic1].[dbo].[parentChildRelationship] where parent_table_id is not null
    OPEN c1
    FETCH NEXT FROM c1 INTO @cTableName

    --set @cTableName='enrollment'
    WHILE @@FETCH_STATUS=0
        begin
            DECLARE @cColumnName nvarchar(max)
            DECLARE c2 CURSOR FOR
                SELECT DISTINCT column_name FROM [phil_ic1].[dbo].[parentChildRelationship]
                    where parent_table_id is not null
                    and tableName=@cTableName
                    and not (column_type='String' AND column_length is null)
            PRINT  'cTableName: '+@cTableName
            OPEN c2

            FETCH NEXT FROM c2 into @cColumnName
            WHILE @@FETCH_STATUS=0
                BEGIN
                    PRINT ' cColumnName: '+@cColumnName
                    DECLARE @pTableName nvarchar(max)
                    DECLARE c3 CURSOR FOR
                        SELECT parent_table_name from parentChildRelationship
                            WHERE tableName=@cTableName
                            AND column_name=@cColumnName
                    OPEN c3
                    FETCH NEXT FROM c3 into @pTableName
                    WHILE @@FETCH_STATUS=0
                        BEGIN
                            PRINT '     pTableName:'+@pTableName
                            DECLARE @pColumnName nvarchar(max)
                            DECLARE c4 CURSOR FOR
                            SELECT parent_column_name from parentChildRelationship
                                WHERE tableName=@cTableName
                                AND column_name=@cColumnName
                                AND parent_table_name=@pTableName
                            OPEN c4


                            FETCH NEXT FROM c4 INTO @pColumnName
                            WHILE @@FETCH_STATUS=0
                                BEGIN
                                    DECLARE @upSQL nvarchar(max)
                                    set @upSQL = 'UPDATE [' +@cTableName+'] SET vError = 1,vMessage=concat(vMessage, ''['+@cTableName+'].['+@cColumnName
                                        +']='','+@cColumnName+','' NOT FOUND IN ['+@pTableName+'].['+@pColumnName
                                        +'];  '') WHERE NOT ['+@cColumnName+'] IN (SELECT ['+@pColumnName+'] FROM ['+@pTableName+'])'
                                    --PRINT @upSQL
                                    exec sp_executesql @upSQL
                                    FETCH NEXT FROM c4 into @pColumnName
                                END
                            CLOSE C4
                            DEALLOCATE c4
                            FETCH NEXT FROM c3 into @pTableName
                        END
                        CLOSE C3
                        DEALLOCATE c3
                    FETCH NEXT FROM c2 into @cColumnName
                END
                PRINT 'LOOP'
                CLOSE C2
                DEALLOCATE c2
            FETCH NEXT FROM c1 into @cTableName
        end
        CLOSE c1
        DEALLOCATE c1
END

2 个答案:

答案 0 :(得分:1)

通过以下查询只做一个光标:

SELECT DISTINCT column_name, tableName, parent_column_name, parent_table_name 
FROM [phil_ic1].[dbo].[parentChildRelationship] 
where parent_table_id is not null
and not (column_type='String' AND column_length is null)

答案 1 :(得分:0)

这是以下查询的输出:

SELECT tableName 'Child Table', column_name 'Child Column ',parent_table_name 'Parent Table' ,   parent_column_name 'Parent Column'
FROM [phil_ic1].[dbo].[parentChildRelationship] 
where parent_table_id is not null
and not (column_type='String' AND column_length is null)
order by tableName,column_name 



    [Child Table]   [Child Column]  [Parent Table]  [Parent Column]
    contact state   State   Code
    contact studentNum  student studentNum
    counselor   employNum   employee    employNum
    counselor   studentNum  student studentNum
    course  calendarName    calendar    calendarName
    course  department  department  name
    course  schoolNum   calendar    schoolNum
    course  schoolNum   school  schoolNum