如何将给定的SQL记录填充到所有UserID

问题描述 投票:-1回答:2

我有一个这种形式的表:

id | firstname | lastname | userid
---+-----------+------------------------
1  | john      | smith    | 545868-5434-343435-35353
2  | adam      | finger   | 545868-5434-343435-35353
3  | teri      | marti    | 545868-5434-343435-35353
4  | pei       | port     | 545868-5434-343435-35353

在DB我有很多userid我需要填充相同的firstnamelastnameuserid发现的所有Database

这是我的SQl查询

SELECT  

cID, c.firstname,c.lastname,

[s].UserID,c.OwnerID

FROM  
Customer INNER JOIN [s] ON c.OwnerID = [s].UserID AND c.AssignedtoID =
[s].UserID AND c.CreatedByUserID = [s].UserID

AssignedtoID与UserID相同

sql sql-server
2个回答
0
投票

这对你有帮助吗?

    Create table #tmpCustomer (id int, firstname VARCHAR(50),lastname VARCHAR(50),userid VARCHAR(100))


    INSERT INTO #tmpCustomer 
    SELECT 1, 'john','smith','545868-5434-343435-35353'
    union
    SELECT 2,'adam','finger','545868-5434-343435-35353'
    union
    SELECT 3,'teri','marti','545868-5434-343435-35353'
    union
    SELECT 4, 'pei','port','545868-5434-343435-35353'
    union
    SELECT 5, 'abc','xyz','545868-5434-343435-35354'
    union
    SELECT 6, 'mno','ert','545868-5434-343435-35354'


    --select * from #tmpCustomer

    ;with cte1 AS(Select row_number()over(partition by userid order by id) rn,* from #tmpCustomer ),
          cte2 AS (select * from cte1 where rn=1 ) 

    update t
    set t.firstname=c.firstname
    from #tmpCustomer t
    JOIN cte2 c on t.userid=c.userid 

    select * from #tmpCustomer

    drop table #tmpCustomer

0
投票

我不知道我是否理解你的问题,试试下面发布的解决方案

DECLARE @cust as table(firstname varchar(20),lastname varchar(20))

插入@cust值('Suzan','Smith')

将@id声明为table(id int identity,any varchar(20),row_inserted datetime2 default(cast(sysdatetime()as datetime2)))

INSERT @id(任何东西,row_inserted)

SELECT'x','20180305'union all select'y','20180305'union all select'z','20180305'

从@id选择s.id,c.firstname,c.lastname作为交叉连接@cust为c

© www.soinside.com 2019 - 2024. All rights reserved.