这个嵌套的游标循环永远循环

时间:2016-07-26 19:21:47

标签: sql-server tsql stored-procedures

我有一个表,其中包含我想用其创建新数据库的所需表名,列名,列数据类型和列数据长度。

现在我正在使用嵌套游标来打印每个唯一的表名及其附带的列名。

当我运行下面的代码时,它会永远循环。我错过了什么?

    BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;


    DECLARE spC1 CURSOR

    FOR
    SELECT DISTINCT tblNm       
    FROM tblDef
    DECLARE @curTabNm AS varchar(60) = ''
    DECLARE @curColNm as varchar(60) = ''
    DECLARE @Outer_Loop AS int = 0
    OPEN spC1


    FETCH NEXT FROM spC1 INTO @curTabNm
    WHILE @Outer_Loop = 0
    PRINT @curTabNm
    BEGIN
        DECLARE spC2 CURSOR 
        FOR
        SELECT colNm FROM tblDef
        WHERE tblNm = @curTabNm

        open spC2
        FETCH NEXT FROM spC2 INTO @curColNm
        WHILE @@FETCH_STATUS = 0
        BEGIN
            PRINT @curColNm
            FETCH NEXT FROM spC2 INTO @curColNm
        END
        CLOSE spC2

        DEALLOCATE spC2
        FETCH NEXT FROM spC1 INTO @curTabNm
        SET @Outer_Loop = 1
    END
    CLOSE spC1
    DEALLOCATE spC1
END

4 个答案:

答案 0 :(得分:1)

检查出来

BEGIN
    DECLARE spC1 CURSOR
    FOR
    SELECT DISTINCT tblNm       
    FROM tblDef
    DECLARE @curTabNm AS varchar(60) = ''
    DECLARE @curColNm as varchar(60) = ''
    DECLARE @Outer_Loop AS int = 0
    OPEN spC1


    FETCH NEXT FROM spC1 INTO @curTabNm

    WHILE @@FETCH_STATUS = 0
    BEGIN
        DECLARE spC2 CURSOR FOR
        SELECT colNm FROM tblDef
        WHERE tblNm = @curTabNm
        open spC2

        FETCH NEXT FROM spC2 INTO @curColNm

        WHILE @@FETCH_STATUS = 0
        BEGIN
            PRINT @curColNm
            FETCH NEXT FROM spC2 INTO @curColNm
        END

        CLOSE spC2
        DEALLOCATE spC2

        FETCH NEXT FROM spC1 INTO @curTabNm
    END
    CLOSE spC1
    DEALLOCATE spC1
END

答案 1 :(得分:1)

这里的第一步是创建耗材ddl和样本数据。我遗漏了一些您说尚未从客户端定义的数据类型。

if OBJECT_ID('tempdb..#Something') is not null
    drop table #Something

create table #something
(
    tblNm sysname
    , colNm sysname
    , colTyp varchar(20)
    , colLen int
    , colReq bit
)

insert #something(tblNm, colNm, colTyp, colLen, colReq)
values
('account', 'studentNum', 'String', 15, 0)
,('account', 'employNum', 'String', 15, 0)
,('account', 'firstName', 'String', 35, 0)
,('account', 'lastName', 'String', 40, 0)
,('assignment', 'vSource', 'String', 255, 0)
,('assignment', 'schoolNum', 'String', 7, 1)
,('assignment', 'calendarName', 'String', 30, 1)
,('assignment', 'courseNum', 'String', 13, 1)

既然我们有数据可以使用,我们可以在这里处理实际的查询。这使用FOR XML技巧生成分隔列表。在这种情况下,分隔列表是我们的列。然后我们在外部查询中使用一些分组来获取每个表。代码看起来像这样。

declare @SQL nvarchar(max) = ''

select @SQL = @SQL + 'create table [' + tblNm + '](' + STUFF((select ', [' + s2.colNm + '] ' + Case s2.colTyp when 'String' then 'varchar' end 
            + isnull('(' + convert(varchar(4), s2.colLen) + ') ', ' ')
            + case s2.colReq when 1 then 'NOT ' else '' end + 'NULL'
        from #something s2
        where s2.tblNm = s.tblNm
        order by s2.colNm
        FOR XML PATH('')), 1,1, '') + ');'
from #something s
group by s.tblNm

select @SQL
--exec sp_executesql @SQL

就是这样。整个东西。当您对@SQL变量的内容感到满意时,您可以简单地取消注释execute语句,它将创建您的表。

如前所述,系统上的负载在这里使用游标的可能性不大,但是一旦你学会了这种类型的技术,它实际上比游标简单得多,并且它的代码要少得多。

答案 2 :(得分:0)

啊,它在线

    WHILE @Outer_Loop = 0
PRINT @curTabNm

顺序相反。哎呦!

答案 3 :(得分:0)

tblNm   colNm   colTyp  colLen  colReq
account personID    Decimal NULL    0
account studentNum  String  15  0
account employNum   String  15  0
account firstName   String  35  0
account lastName    String  40  0
account birthDate   String  NULL    0
account accountNumber   Decimal NULL    0
account accountPIN  String  50  0
account reminder    String  5   0
account remAmount   String  5   0
account blockConcessions    String  5   0
account blockAlaCarte   String  5   0
account singlePurch String  5   0
account eligibility String  20  0
account certifiedType   String  20  0
account startDate   String  NULL    0
account endDate String  NULL    0
account beginningBalance    String  12  0
account vSource String  255 0
assignment  vSource String  255 0
assignment  schoolNum   String  7   1
assignment  calendarName    String  30  1
assignment  courseNum   String  13  1
assignment  sectionNum  Int 5   1
assignment  groupName   String  50  1
assignment  assignmentName  String  50  1
assignment  abbreviation    String  5   1
assignment  assignmentSeq   String  5   0
assignment  totalPoints String  5   1
assignment  curveName   String  80  0
assignment  dueDate String  NULL    1
assignment  assignedDate    String  NULL    0
assignment  multiplier  String  6   0
assignment  description String  255 0
assignment  objectives  String  255 0
assignment  references  String  255 0
assignmentScore schoolNum   String  7   1
assignmentScore calendarName    String  30  1
assignmentScore courseNum   String  13  1
assignmentScore sectionNum  Int 5   1
assignmentScore groupName   String  50  1
assignmentScore assignmentName  String  50  1
assignmentScore studentNum  String  15  1
assignmentScore score   String  20  1
assignmentScore flag    String  20  0
assignmentScore comments    String  250 0
assignmentScore vSource String  255 0
attendance  vSource String  255 0
attendance  schoolNum   String  7   1
attendance  calendarName    String  30  1
attendance  studentNum  String  15  1
attendance  date    DateTime    NULL    1
attendance  scheduleSeq String  3   0
attendance  periodSeq   Int 3   0
attendance  code    String  NULL    1
attendance  comments    String  50  0
attendanceExcuse    schoolNum   String  7   1
attendanceExcuse    calendarName    String  30  1
attendanceExcuse    code    String  NULL    1
attendanceExcuse    description String  50  1
attendanceExcuse    status  String  20  1
attendanceExcuse    excuse  String  20  1
attendanceExcuse    vSource String  255 0
behaviorEvent   vSource String  255 0
behaviorEvent   eventType   String  100 1
behaviorEvent   incidentID  Int 0   1
behaviorEvent   eventID Int 50  1
behaviorEvent   referralEmployNum   String  15  0
behaviorEvent   adminEmployNum  String  15  0
behaviorEvent   weaponCode  String  2   0
behaviorEvent   calendarName    String  30  1
behaviorResolution  eventID String  50  1
behaviorResolution  studentNum  String  15  1
behaviorResolution  resolutionDate  DateTime    NULL    1
behaviorResolution  resolutionTime  String  20  1
behaviorResolution  endDate DateTime    NULL    0
behaviorResolution  ParentInvolvementComment    String  255 0
behaviorResolution  ParentCode  Int NULL    0
behaviorResolution  AssignedAlternateEducation  String  50  0
behaviorResolution  AdjudicationCode    String  50  0
behaviorResolution  ArrestCode  String  50  0
behaviorResolution  LLEOffice   String  50  0
behaviorResolution  LLENotified String  50  0
behaviorResolution  Duration    Int NULL    0
behaviorResolution  adminEmployNum  String  50  0
behaviorResolution  DisciplinaryActionComment   String  255 0
behaviorResolution  resolutionType  String  50  0
behaviorResolution  vSource String  255 0
behaviorRole    vSource String  255 0
behaviorRole    OffenderType    String  NULL    0
behaviorRole    injury  String  NULL    1
behaviorRole    relationshipToSchool    String  NULL    1
behaviorRole    InfractionComment   String  NULL    0
behaviorRole    ReceivedServices    Boolean NULL    0
behaviorRole    WeaponCount Int NULL    0
behaviorRole    WeaponDetectedMethod    String  NULL    0
behaviorRole    victimType  String  NULL    0
behaviorRole    eventID String  50  1
behaviorRole    studentNum  String  15  1
behaviorRole    role    String  15  1
behaviorRole    demerits    String  5   0
bus number  String  10  1
bus description String  250 0
bus contracted  String  5   0
bus vSource String  255 0
busRider    vSource String  255 0
busRider    schoolNum   String  7   1
busRider    calendarName    String  30  1
busRider    studentNum  String  15  1
busRider    inBusNumber String  10  0
busRider    inTime  String  20  0
busRider    inBusStop   String  20  0
busRider    outBusNumber    String  10  0
busRider    outTime String  20  0
busRider    outBusStop  String  20  0
busRider    lateBus String  10  0
busRider    transMiles  String  7   0