| |

VerySource

 Forgot password?
 Register
Search
View: 783|Reply: 7

How should this stored procedure be implemented?

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-18 22:30:01
| Show all posts |Read mode
Data tables A and B are now available:

Form A Form B
--------------------------- ----------------------- ---
uid name topic uid
1 David xxxxx 1
2 Jack xxx 3
3 Mike xxxxxx 0

After processing the stored procedure, we get such a table C:
Form C
-------------------------
topic name
xxxxx David
xxx Mike
xxxxxx anonymous

Excuse me, how to write such a stored procedure?
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-4-23 19:15:01
| Show all posts
select b.topic, (case a.name when null then 'a.name' else a.name end) as NAME

FROM B table b
LEFT JOIN
Table A
on a.uid = b.uid
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-4-23 20:45:01
| Show all posts
SQL:
select b.topic, (case a.name when null then 'anonymous' else a.name end) as NAME
FROM B table b
LEFT JOIN
Table A
on a.uid = b.uid
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-4-23 22:45:01
| Show all posts
Stored procedure:

CREATE PROCEDURE Getname
AS

begin
select b.topic, (case a.name when null then 'anonymous' else a.name end) as NAME
FROM B table b
LEFT JOIN
Table A
on a.uid = b.uid
end
GO
Reply

Use magic Report

3

Threads

29

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-4-23 23:45:01
| Show all posts
Ha ha, a statement upstairs is completed, also use stored procedures?
But to ask, can the outer of the LEFT outer JOIN be omitted? Never dared to save.
Reply

Use magic Report

0

Threads

8

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-4-24 00:00:01
| Show all posts
select b.topic, ISNULL (a.name, 'Anonymous') from B table b LEFT JOIN A table a ON a.uid = b.uid
Reply

Use magic Report

0

Threads

13

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-4-24 16:30:02
| Show all posts
create procedure P_test
as
begin
    select topic, uname = case when (select [NAME] from table A where table A.uid = table B.uid) is null then 'anonymous' else (select [NAME] from table A where table A.uid = table B. uid) end from Table B

end
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-4-25 12:15:01
| Show all posts
Thank you all upstairs, thezhouyin1981(Bobo) statement is feasible. Thelisongfoxstatement "anonymous" cannot be written into the new field, and the new field still displays null.
But thank you all for your help.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list