Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQL使用随机查找值更新行列_Sql Server_Sql Update - Fatal编程技术网

Sql server SQL使用随机查找值更新行列

Sql server SQL使用随机查找值更新行列,sql-server,sql-update,Sql Server,Sql Update,我正在尝试更新lead表,以便从查找表中随机分配一个人。以下是通用模式: TableA (header), ID int, name varchar (30) TableB (detail), ID int, fkTableA int, (foreign key to TableA.ID) recordOwner varchar(30) null other detail colums.. TableC (owners), ID int, fkTableA int (foreign key

我正在尝试更新lead表,以便从查找表中随机分配一个人。以下是通用模式:

TableA (header),
ID int,
name varchar (30)

TableB (detail),
ID int,
fkTableA int, (foreign key to TableA.ID)
recordOwner varchar(30) null
other detail colums..

TableC (owners),
ID int,
fkTableA int (foreign key to TableA.ID)
name varchar(30)
TableA
有10个条目,每种类型的销售线索池对应一个条目<代码>表格B在
表格A
中的每一行都有数千个条目。我想从
TableC
中为每个行分配正确的
recordOwners
,并分配偶数行(或尽可能接近)<代码>表格C在
表格A
中每行有一个条目,或最多10个条目

这可以在一个声明中完成吗?不一定是这样。我似乎想不出提高速度的最佳方法。任何想法或样品都将不胜感激

更新:
TableA
tablac
有一对多的关系。对于
表格a
的每个记录,
表格C
将至少有一行,表示需要分配给
表格B
中一行的所有者

TableA
int  name
1    LeadSourceOne
2    LeadSourceTwo

TableC
int(id) int(fkTableA) varchar(name)
1       1             Tom
2       1             Bob
3       2             Timmy
4       2             John
5       2             Steve
6       2             Bill

TableB initial data
int(id) int(fkTableA) varchar(recordOwner) (other detail columns)
1       1             NULL                 ....
2       1             NULL                 ....
3       1             NULL                 ....
4       2             NULL                 ....
5       2             NULL                 ....
6       2             NULL                 ....
7       2             NULL                 ....
8       2             NULL                 ....
9       2             NULL                 ....

TableB end result
int(id) int(fkTableA) varchar(recordOwner) (other detail columns)
1       1             TOM                  ....
2       1             BOB                  ....
3       1             TOM                  ....
4       2             TIMMY                ....
5       2             JOHN                 ....
6       2             STEVE                ....
7       2             BILL                 ....
8       2             TIMMY                ....
9       2             BILL                 ....
基本上,我需要根据与
tableA
的关系,将一条记录从
tableC
随机分配到
tableB

UPDATE TabB SET name = (SELECT TOP 1 coalesce(tabC.name,'') FROM TabC  INNER JOIN TabA ON TabC.idA  = TabA.id  WHERE tabA.Id = TabB.idA )
应该可以工作,但未经测试。

尝试以下方法:

UPDATE TableB
SET recordOwner = (SELECT TOP(1) [name] 
                   FROM TableC
                   ORDER BY NEWID())
WHERE recordOwner IS NULL

最后,我循环浏览并根据我拥有的所有者数量更新了x%的详细记录。最终的结果是这样的:

create table #tb_owners(userId varchar(30), processed bit)

    insert into #tb_owners(
        userId, 
        processed)
    select userId = name, 
        processed = 0
    from tableC 
    where fkTableA = 1

    select @percentUpdate = cast(100 / count(*) as numeric(8,2))
    from #tb_owners

    while exists(select 1 from #tb_owners o where o.processed = 0)
        begin
            select top 1 
                @userFullName = o.name
            from #tb_owners o
            where o.processed = 0
            order by newId()


            update tableB
            set recordOwner = @userFullName
            from tableB ptbpd
                inner join (
                            select top (@percentUpdate) percent 
                                id
                            from tableB
                            where recordOwner is null
                            order by newId()
                    ) nd on (ptbpd.id = nd.id)


            update #tb_owners
            set processed = 1
            where userId = @oUserId
        end

    --there may be some left over, set to last person used
    update tableB
    set recordOwner = @userFullName
    from tableB
    where ptbpd.recordOwner is null

请提供更多信息。就像表A和表C之间的关系。在我看来,它们是1:1的关系,表A/C和表B是1:M的关系。你能展示一下预期的产量吗?示例数据将有助于为您编写查询。这不就是将每个记录的名称都设置为相同吗?