在多个变量中插入多个行值

时间:2019-04-09 14:25:13

标签: sql-server foreach cursor

我需要将值添加到变量@ ProfileId1和@ ProfileId2

类似于C#代码的东西。获取第一个Client的配置文件ID,然后将其值添加到声明的变量中,然后执行查询。 对每个客户端重复此操作。

我以前从未使用过游标,也不知道如何在多个变量中添加选定的值。

foreach(var client in clientTable)
{
              DECLARE @LinkedInProfile1 INT,@LinkedInProfile2 INT;
              SET @ProfileId1 = profile.Id  (This should be 1 in first iteration, 3 in second iteration, 5 in 3th iteration)
              SET @ProfileId2 = profile.Id ; (This should be 2 in first iteration, 4 in second iteration, 6 in 3th iteration)

    execute insert query with new values for ProfileId1 and ProfileId2 
    ....
    ....
    ....
}


    Client Table
    -------------
    ID | Name   |
    -------------
    1  | Client1|
    -------------
    2  | Client2|
    -------------
    3  | Clinet3|
    -------------

    Profile Table
    -------------------------
    ID | Name    | ClientId |
    -------------------------
    1  | Profile1|    1     |
    -------------------------
    2  | Profile2|    1     |
    -------------------------
    3  | Profile3|    2     |
    ------------------------
    4  | Profile4|    2     |
    ------------------------
    5  | Profile5|    3     |
    ------------------------
    6  | Profile6|    3     |
    ------------------------

0 个答案:

没有答案