| |

VerySource

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

How do I write the results of the execution of exec (@sql) into the table (temporary tables can also be)

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-11 16:00:01
| Show all posts |Read mode
Vertical to horizontal.
There are tables: test (name char (10), km char (10), cj int)
name km cj
----------------------------------------------
Zhang San Chinese 80
Zhang San Mathematics 86
Zhang San English 75
Lee Si Language 78
Lee Si Mathematics 85
Lee Si English 78

Requires display in landscape format, that is:
Want to become

Name Language Mathematics English
----------------------------
Zhang San 80 86 75
Lee Si 78 85 78

Create table test (name char (10), km char (10), cj int)
go
insert test values ​​('Zhang San', 'Language', 80)
insert test values ​​('Zhang San', 'Mathematics', 86)
insert test values ​​('Zhang San', 'English', 75)
insert test values ​​('Li Si', 'Chinese', 78)
insert test values ​​('Li Si', 'Mathematics', 85)
insert test values ​​('Li Si', 'English', 78)
method:
declare @sql varchar (8000)
set @sql = 'select name'
select @sql = @sql + ', sum (case km when' '' + km + '' 'then cj end) [' + km + ']'
 from (select distinct km from test) as a
select @sql = @ sql + 'from test group by name'
exec (@sql)

How do I write the results of the execution of exec (@sql) into the table (temporary tables can also be)
Reply

Use magic Report

0

Threads

35

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-4-7 09:30:01
| Show all posts
insert into #Temporary table
exec (@sql)
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 United States

Post time: 2020-4-7 11:15:01
| Show all posts
--try

declare @sql varchar (8000)
set @sql = 'select name'
select @sql = @sql + ', sum (case km when' '' + km + '' 'then cj end) [' + km + ']'
 from (select distinct km from test) as a
select @sql = @ sql + 'into #T from test group by name'
exec (@sql)

select * from #T
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-4-7 17:00:01
| Show all posts
--Is it possible to use global temporary tables? Partial temporary table does not work

declare @sql varchar (8000)
set @sql = 'select name'
select @sql = @sql + ', sum (case km when' '' + km + '' 'then cj end) [' + km + ']'
 from (select distinct km from test) as a
select @sql = @ sql + 'into ## T from test group by name'
exec (@sql)

select * from ## T
Reply

Use magic Report

0

Threads

35

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-4-7 21:45:01
| Show all posts
insert into #Temporary table
exec (@sql)
========
Write one less sentence
create table #Temporary table (...)
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