第一表的[index]与第二表的哪个字段关联?

解决方案 »

  1.   

    或者说第一表的ID和第二表的ID相关联?
      

  2.   

    这是一张存储过程参数表
    ID对应的是存储过程的ID,(外键对应的是存储过程主信息表的ID)
    index 对应的是参数的序号,
    param 参数的名字
    badvalue 错误的参数值
    badtype  错误值的类型 
    badreturnvalue 错误的返回值ID     index    param             badvalue   badtype  badreturnvalue 
    1       1      @Accounts           45645      1             1
    1       2      @Pwd                456        2             1
    1       3      @LoginIP            1131       1             1
    1       4      @OutPutValue        8978       3             2
    2       1      @Accounts           4656       4             4
    3       1      @UserId             45645      5             2
    11      1      @UserID             456456     4             4
    12      1      @ServerID           45645      8             8
    13      1      @ServerID           456456     9             7另外一张存储过程主信息表ID:存储过程的ID,
    name: 存储过程的名字,
    backtype:返回值类型
    rightvalue:返回值预期的正确值,
    ID             name                  backtype          rightvalue 
    1          UP_UserInfo_Login          1                  0
    2         UP_UI_ID_OneUserInfo_S      2                  1
    3         UP_MIf_DoorTicket_S         2                  2
    4         UP_TGt_HavePrizeLayout_S    2                  9
    5         UP_TGt_ZorePrizeLayout_S    2                  4
    6         UP_SSt_ServerList_S         2                  1
    7         UP_TGt_ZorePrizeRunTerm_S   2                  4
    8         UP_TGt_HavePrizeRunTerm_S   2                  9
    9         UP_TGt_HallSeverList_S      2                  1
    10        UP_DDo_List_S               2                 16
    11        UP_UIf_UID_Coin_S           2                  1
    12        UP_SSt_ID_S                 2                  1
    13        UP_SSt_ID_Port_S            2                  1
    转换成:
    param2 param3,param4 ...paramN 与param1相同 有对应错误的参数值,对应的返回值类型,对应返回值预期的正确值....表示省略ID rightvalue  param1    badvalue1   badtype1  badreturnvalue1    param2  ....        param3    ....        param4 ....
    1      0       @Accounts    45645       1              1           @Pwd                @LoginIP            @OutPutValue
    2      1       @Accounts     ....       ....         ....          
    3      2       @UserId        ....      ....          ....
    11     1       @UserID        ....       ....          ....
    12     1       @ServerID      ....        ....           ....
    13     1       @ServerID      ....         ....          ....存储过程ID和它的所有参数 信息第一表的[index]与第二表的字段关联,
    只通过ID关联,
      

  3.   

    /*
    -- Author:Flystone 
    -- Version:V1.001  Date:2008-05-15   初稿
    -- Version:V1.002  Date:2008-05-16  1、 处理空格带来的异常
    --                                  2、 增加了形如yyyy-mm-dd hh:mm:ss
    --                                               yyyy-m-d h:m:s     格式的处理
    */-- Test Data: Ta
    If object_id('Ta') is not null 
        Drop table Ta
    Go
    Create table Ta(ID int,[index] int,param varchar(20),badvalue int,badtype int,badreturnvalue int)
    Go
    Insert into Ta
    select 1,1,'@Accounts',45645,1,1 union all
    select 1,2,'@Pwd',456,2,1 union all
    select 1,3,'@LoginIP',1131,1,1 union all
    select 1,4,'@OutPutValue',8978,3,2 union all
    select 2,1,'@Accounts',4656,4,4 union all
    select 3,1,'@UserId',45645,5,2 union all
    select 11,1,'@UserID',456456,4,4 union all
    select 12,1,'@ServerID',45645,8,8 union all
    select 13,1,'@ServerID',456456,9,7 
    Go
    --Start
    declare @sql varchar(8000)
    --set @sql = ''
    select @sql=isnull(@sql+',','')+'max(case when [index]='+ltrim([index])+' then param end) [param'+ltrim([index])+']
    '+
    ',max(case when [index]='+ltrim([index])+' then badvalue end) [badvalue'+ltrim([index])+']
    '+
    ',max(case when [index]='+ltrim([index])+' then badtype end) [badtype'+ltrim([index])+']
    '+
    ',max(case when [index]='+ltrim([index])+' then badreturnvalue end) [badreturnvalue'+ltrim([index])+']
    '
    from ta group by [index]
    exec('select id,'+ @sql + ' from ta group by id')
    --Result:
    /*
    id          param1               badvalue1   badtype1    badreturnvalue1 param2               badvalue2   badtype2    badreturnvalue2 param3               badvalue3   badtype3    badreturnvalue3 param4               badvalue4   badtype4    badreturnvalue4 
    ----------- -------------------- ----------- ----------- --------------- -------------------- ----------- ----------- --------------- -------------------- ----------- ----------- --------------- -------------------- ----------- ----------- --------------- 
    1           @Accounts            45645       1           1               @Pwd                 456         2           1               @LoginIP             1131        1           1               @OutPutValue         8978        3           2
    2           @Accounts            4656        4           4               NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL
    3           @UserId              45645       5           2               NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL
    11          @UserID              456456      4           4               NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL
    12          @ServerID            45645       8           8               NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL
    13          @ServerID            456456      9           7               NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL警告: 聚合或其它 SET 操作消除了空值。*/
    --End 
      

  4.   

    第一表的[index]字段不与第二表的任何字段关联
      

  5.   

    /*
    -- Author:Flystone 
    -- Version:V1.001  Date:2008-05-15   初稿
    -- Version:V1.002  Date:2008-05-16  1、 处理空格带来的异常
    --                                  2、 增加了形如yyyy-mm-dd hh:mm:ss
    --                                               yyyy-m-d h:m:s     格式的处理
    */-- Test Data: Ta
    If object_id('Ta') is not null 
        Drop table Ta
    Go
    Create table Ta(ID int,[index] int,param varchar(20),badvalue int,badtype int,badreturnvalue int)
    Go
    Insert into Ta
    select 1,1,'@Accounts',45645,1,1 union all
    select 1,2,'@Pwd',456,2,1 union all
    select 1,3,'@LoginIP',1131,1,1 union all
    select 1,4,'@OutPutValue',8978,3,2 union all
    select 2,1,'@Accounts',4656,4,4 union all
    select 3,1,'@UserId',45645,5,2 union all
    select 11,1,'@UserID',456456,4,4 union all
    select 12,1,'@ServerID',45645,8,8 union all
    select 13,1,'@ServerID',456456,9,7 
    Go
    -- Test Data: pro
    If object_id('pro') is not null 
        Drop table pro
    Go
    Create table pro(ID int,name varchar(40),backtype int,rightvalue int)
    Go
    Insert into pro
    select 1,'UP_UserInfo_Login',2,1 union all
    select 2,'UP_UI_ID_OneUserInfo_S',2,2 union all
    select 3,'UP_MIf_DoorTicket_S',2,2 union all
    select 4,'UP_TGt_HavePrizeLayout_S',2,9 union all
    select 5,'UP_TGt_ZorePrizeLayout_S',2,4 union all
    select 6,'UP_SSt_ServerList_S',2,1 union all
    select 7,'UP_TGt_ZorePrizeRunTerm_S',2,4 union all
    select 8,'UP_TGt_HavePrizeRunTerm_S',2,9 union all
    select 9,'UP_TGt_HallSeverList_S',2,1 union all
    select 10,'UP_DDo_List_S',2,16 union all
    select 11,'UP_UIf_UID_Coin_S',2,1 union all
    select 12,'UP_SSt_ID_S',2,1 union all
    select 13,'UP_SSt_ID_Port_S',2,1 
    Go
    --Start
    declare @sql varchar(8000)
    --set @sql = ''
    select @sql=isnull(@sql+',','')+'max(case when [index]='+ltrim([index])+' then param end) [param'+ltrim([index])+']
    '+
    ',max(case when [index]='+ltrim([index])+' then badvalue end) [badvalue'+ltrim([index])+']
    '+
    ',max(case when [index]='+ltrim([index])+' then badtype end) [badtype'+ltrim([index])+']
    '+
    ',max(case when [index]='+ltrim([index])+' then badreturnvalue end) [badreturnvalue'+ltrim([index])+']
    '
    from ta group by [index]
    exec('select a.id,rightvalue,'+ @sql + ' from ta a,pro b where a.id = b.id  group by a.id,b.rightvalue')
    --Result:
    /*
    id          rightvalue  param1               badvalue1   badtype1    badreturnvalue1 param2               badvalue2   badtype2    badreturnvalue2 param3               badvalue3   badtype3    badreturnvalue3 param4               badvalue4   badtype4    badreturnvalue4 
    ----------- ----------- -------------------- ----------- ----------- --------------- -------------------- ----------- ----------- --------------- -------------------- ----------- ----------- --------------- -------------------- ----------- ----------- --------------- 
    1           1           @Accounts            45645       1           1               @Pwd                 456         2           1               @LoginIP             1131        1           1               @OutPutValue         8978        3           2
    11          1           @UserID              456456      4           4               NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL
    12          1           @ServerID            45645       8           8               NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL
    13          1           @ServerID            456456      9           7               NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL
    2           2           @Accounts            4656        4           4               NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL
    3           2           @UserId              45645       5           2               NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL            NULL                 NULL        NULL        NULL警告: 聚合或其它 SET 操作消除了空值。*/
    --End