| |

VerySource

 Forgot password?
 Register
Search
View: 861|Reply: 4

Online etc.: The problem of sql column transfer, master enters,

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2021-3-6 15:30:01
| Show all posts |Read mode
I have two tables, a class schedule for the grade exam, examsubject
Since the score table contains the scores of all courses, some grades have this course, some do not, so when querying the results, I made a second table
examlistid course ClassID
1 Language 1
2 Mathematics 1
3 English 1
4 Physics 2
5 Chemistry 2
There is also a transcript of the whole school: [20161230]
Student ID Chinese Mathematics English Physics Chemistry Biology Geography Politics History
111 50 50 50 NULL NULL 60 60 60 60
112 50 50 50 NULL NULL 65 65 65 65

I want to get the course name through the ClassID value of the examsubject table, and then generate a grade query,
Generate the following style: ClassID=1, the score is the value in [20161230]
Student ID Chinese Mathematics English
111 50 5 50
112 50 50 50
When ClassID=2
The generated grade table style is:
Student ID Physical Chemistry
xx xx xx
xx xx xx
Reply

Use magic Report

0

Threads

21

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2021-3-6 15:45:01
| Show all posts
try
-------------------------

declare @sql varchar(8000),@ClassID int
set @ClassID=2

set @sql ='select student ID'
select @sql = @sql +',' + course from examsubject where ClassID=@ClassID
set @sql = @sql + 'from [20161230]'
exec(@sql)
Reply

Use magic Report

0

Threads

21

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2021-3-6 16:00:01
| Show all posts
-Create a stored procedure
create procedure spExam
@ClassID int
as

declare @sql varchar(8000)
set @sql ='select student ID'
select @sql = @sql +',' + course from examsubject where ClassID=@ClassID
set @sql = @sql + 'from [20161230]'
exec(@sql)

--transfer
spExam 1
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2021-3-6 17:15:01
| Show all posts
Master upstairs, agree!
Reply

Use magic Report

0

Threads

1

Posts

26.00

Credits

Newbie

Rank: 1

Credits
26.00

 China

Post time: 2021-8-31 06:22:43
| Show all posts
666666666666666666
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