tbl_city:
id city
1 北京
2 上海
tbl_school:
id cityid school
1 1 北京大学
2 1 清华大学
3 2 上海交大
4 2 上海电大
现在想把:
select 'fld'+cast(id as varchar) as fldstr from tbl_city
的结果变成一个临时表的字段名,并将其对应的城市内学校的数量插入到临时表中:
temp_tbl_citycount
fld1 fld2
2 2
注:上述描述只是一个需求简化,真实项目中会增加地区级别,学校种类划分等。
11 个解决方案
#1
select 'fld'+cast(id as varchar) as fldstr into #tmp from tbl_city
或
create #tmp(col1 datatyp,col2 datetype,....)
insert into #tmp(col1,col2,...) select col1,col2,...from tbl_city
或
create #tmp(col1 datatyp,col2 datetype,....)
insert into #tmp(col1,col2,...) select col1,col2,...from tbl_city
#2
需要SELECT临时表还是建立临时表?
#3
DECLARE @X VARCHAR(100)
SET @X=''
select @X=',fld'+cast(id as varchar) as fldstr from tbl_city
SET @X=STUFF(@X,1,1,'')
PRINT @X
这有用,但需要你的实际操作才能用
SET @X=''
select @X=',fld'+cast(id as varchar) as fldstr from tbl_city
SET @X=STUFF(@X,1,1,'')
PRINT @X
这有用,但需要你的实际操作才能用
#4
无语了,蹭点分!
#5
^_^
#6
这有三个过程了
一是先查询出有多少个城市
然后就是以各城市标识为字段名创建临时表
再把各城市的统计数量插入到临时表中(有个关键就是统计数量要与城市对应得上)。
这是用来做统计的,所有统计值插入到一个临时表后,就可以一次性将所有需要(如华北区,或广东的城市)统计结果查询到(asp页面)。
一是先查询出有多少个城市
然后就是以各城市标识为字段名创建临时表
再把各城市的统计数量插入到临时表中(有个关键就是统计数量要与城市对应得上)。
这是用来做统计的,所有统计值插入到一个临时表后,就可以一次性将所有需要(如华北区,或广东的城市)统计结果查询到(asp页面)。
#7
需要用动态SQL语句。
--------------------------------------------------------------------------------
动态sql语句基本语法
1 :普通SQL语句可以用Exec执行
eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N
2:字段名,表名,数据库名之类作为变量时,必须用动态SQL
eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格
当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名
declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错
declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句正确
3. 输出参数
declare @num int,
@sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中?
declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num
--------------------------------------------------------------------------------
动态sql语句基本语法
1 :普通SQL语句可以用Exec执行
eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N
2:字段名,表名,数据库名之类作为变量时,必须用动态SQL
eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格
当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名
declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错
declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句正确
3. 输出参数
declare @num int,
@sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中?
declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num
#8
又是行转列.
CREATE TABLE tbl_city(id INT,City VARCHAR(10))
INSERT tbl_city SELECT 1 , '北京'
UNION ALL SELECT 2,'上海'
DECLARE @sql VARCHAR(8000)
SET @sql = ''
SELECT @sql = @sql + ',(CASE id WHEN '+RTRIM(id)+' THEN 0 ELSE 1 END) [fld'+RTRIM(id)+']' FROM tbl_city
SET @sql='SELECT ' + STUFF(@sql,1,1,'') + 'INTO tmp FROM tbl_city WHERE 1=0'
PRINT @sql
EXEC(@sql)
SELECT * FROM tmp
DROP TABLE tbl_City,tmp
CREATE TABLE tbl_city(id INT,City VARCHAR(10))
INSERT tbl_city SELECT 1 , '北京'
UNION ALL SELECT 2,'上海'
DECLARE @sql VARCHAR(8000)
SET @sql = ''
SELECT @sql = @sql + ',(CASE id WHEN '+RTRIM(id)+' THEN 0 ELSE 1 END) [fld'+RTRIM(id)+']' FROM tbl_city
SET @sql='SELECT ' + STUFF(@sql,1,1,'') + 'INTO tmp FROM tbl_city WHERE 1=0'
PRINT @sql
EXEC(@sql)
SELECT * FROM tmp
DROP TABLE tbl_City,tmp
#9
一楼领会错了,这样试试:
create table #tbl_city (id int,city varchar(100))
create table #tbl_school (id int,cityid int,scholl varchar(100))
insert #tbl_city
select 1,'北京' union all
select 2,'上海'
insert #tbl_school
select 1,1,'北京大学' union all
select 2,1,'清华大学' union all
select 3,2,'上海交大' union all
select 4,2,'上海电大'
declare @sql varchar(2000)
declare @str varchar(1000)
set @str = ''
select @str = @str + ',fld' + cast(id as varchar) + ' = (select count(1) from #tbl_school where cityid = ' + rtrim(id) + ')'
from #tbl_city
set @sql = 'select ' + stuff(@str,1,1,'') + ' into #tmp
select * from #tmp'
exec (@sql)
drop table #tbl_city,#tbl_school
create table #tbl_city (id int,city varchar(100))
create table #tbl_school (id int,cityid int,scholl varchar(100))
insert #tbl_city
select 1,'北京' union all
select 2,'上海'
insert #tbl_school
select 1,1,'北京大学' union all
select 2,1,'清华大学' union all
select 3,2,'上海交大' union all
select 4,2,'上海电大'
declare @sql varchar(2000)
declare @str varchar(1000)
set @str = ''
select @str = @str + ',fld' + cast(id as varchar) + ' = (select count(1) from #tbl_school where cityid = ' + rtrim(id) + ')'
from #tbl_city
set @sql = 'select ' + stuff(@str,1,1,'') + ' into #tmp
select * from #tmp'
exec (@sql)
drop table #tbl_city,#tbl_school
#10
CREATE TABLE tbl_city(id INT,City VARCHAR(10))
INSERT tbl_city SELECT 1 , '北京'
UNION ALL SELECT 2,'上海'
UNION ALL SELECT 3,'广州'
CREATE TABLE tbl_School(id INT,cityid INT,school VARCHAR(20))
INSERT tbl_School SELECT 1, 1, '北京大学'
UNION ALL SELECT 2 ,1, '清华大学'
UNION ALL SELECT 3 , 2 , '上海交大'
UNION ALL SELECT 4, 2 , '上海电大'
DECLARE @sql VARCHAR(8000)
SET @sql = ''
SELECT @sql = @sql + ',SUM(CASE cityid WHEN '+RTRIM(id)+' THEN 1 ELSE 0 END) [fld'+RTRIM(id)+']' FROM tbl_city
SET @sql='SELECT ' + STUFF(@sql,1,1,'') + 'INTO tmp FROM tbl_school'
PRINT @sql
EXEC(@sql)
SELECT * FROM tmp
DROP TABLE tbl_City,tmp,tbl_School
/*结果
fld1 fld2 fld3
2 2 0
*/
INSERT tbl_city SELECT 1 , '北京'
UNION ALL SELECT 2,'上海'
UNION ALL SELECT 3,'广州'
CREATE TABLE tbl_School(id INT,cityid INT,school VARCHAR(20))
INSERT tbl_School SELECT 1, 1, '北京大学'
UNION ALL SELECT 2 ,1, '清华大学'
UNION ALL SELECT 3 , 2 , '上海交大'
UNION ALL SELECT 4, 2 , '上海电大'
DECLARE @sql VARCHAR(8000)
SET @sql = ''
SELECT @sql = @sql + ',SUM(CASE cityid WHEN '+RTRIM(id)+' THEN 1 ELSE 0 END) [fld'+RTRIM(id)+']' FROM tbl_city
SET @sql='SELECT ' + STUFF(@sql,1,1,'') + 'INTO tmp FROM tbl_school'
PRINT @sql
EXEC(@sql)
SELECT * FROM tmp
DROP TABLE tbl_City,tmp,tbl_School
/*结果
fld1 fld2 fld3
2 2 0
*/
#11
或者left join之后再统计.
#1
select 'fld'+cast(id as varchar) as fldstr into #tmp from tbl_city
或
create #tmp(col1 datatyp,col2 datetype,....)
insert into #tmp(col1,col2,...) select col1,col2,...from tbl_city
或
create #tmp(col1 datatyp,col2 datetype,....)
insert into #tmp(col1,col2,...) select col1,col2,...from tbl_city
#2
需要SELECT临时表还是建立临时表?
#3
DECLARE @X VARCHAR(100)
SET @X=''
select @X=',fld'+cast(id as varchar) as fldstr from tbl_city
SET @X=STUFF(@X,1,1,'')
PRINT @X
这有用,但需要你的实际操作才能用
SET @X=''
select @X=',fld'+cast(id as varchar) as fldstr from tbl_city
SET @X=STUFF(@X,1,1,'')
PRINT @X
这有用,但需要你的实际操作才能用
#4
无语了,蹭点分!
#5
^_^
#6
这有三个过程了
一是先查询出有多少个城市
然后就是以各城市标识为字段名创建临时表
再把各城市的统计数量插入到临时表中(有个关键就是统计数量要与城市对应得上)。
这是用来做统计的,所有统计值插入到一个临时表后,就可以一次性将所有需要(如华北区,或广东的城市)统计结果查询到(asp页面)。
一是先查询出有多少个城市
然后就是以各城市标识为字段名创建临时表
再把各城市的统计数量插入到临时表中(有个关键就是统计数量要与城市对应得上)。
这是用来做统计的,所有统计值插入到一个临时表后,就可以一次性将所有需要(如华北区,或广东的城市)统计结果查询到(asp页面)。
#7
需要用动态SQL语句。
--------------------------------------------------------------------------------
动态sql语句基本语法
1 :普通SQL语句可以用Exec执行
eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N
2:字段名,表名,数据库名之类作为变量时,必须用动态SQL
eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格
当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名
declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错
declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句正确
3. 输出参数
declare @num int,
@sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中?
declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num
--------------------------------------------------------------------------------
动态sql语句基本语法
1 :普通SQL语句可以用Exec执行
eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N
2:字段名,表名,数据库名之类作为变量时,必须用动态SQL
eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格
当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名
declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错
declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句正确
3. 输出参数
declare @num int,
@sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中?
declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num
#8
又是行转列.
CREATE TABLE tbl_city(id INT,City VARCHAR(10))
INSERT tbl_city SELECT 1 , '北京'
UNION ALL SELECT 2,'上海'
DECLARE @sql VARCHAR(8000)
SET @sql = ''
SELECT @sql = @sql + ',(CASE id WHEN '+RTRIM(id)+' THEN 0 ELSE 1 END) [fld'+RTRIM(id)+']' FROM tbl_city
SET @sql='SELECT ' + STUFF(@sql,1,1,'') + 'INTO tmp FROM tbl_city WHERE 1=0'
PRINT @sql
EXEC(@sql)
SELECT * FROM tmp
DROP TABLE tbl_City,tmp
CREATE TABLE tbl_city(id INT,City VARCHAR(10))
INSERT tbl_city SELECT 1 , '北京'
UNION ALL SELECT 2,'上海'
DECLARE @sql VARCHAR(8000)
SET @sql = ''
SELECT @sql = @sql + ',(CASE id WHEN '+RTRIM(id)+' THEN 0 ELSE 1 END) [fld'+RTRIM(id)+']' FROM tbl_city
SET @sql='SELECT ' + STUFF(@sql,1,1,'') + 'INTO tmp FROM tbl_city WHERE 1=0'
PRINT @sql
EXEC(@sql)
SELECT * FROM tmp
DROP TABLE tbl_City,tmp
#9
一楼领会错了,这样试试:
create table #tbl_city (id int,city varchar(100))
create table #tbl_school (id int,cityid int,scholl varchar(100))
insert #tbl_city
select 1,'北京' union all
select 2,'上海'
insert #tbl_school
select 1,1,'北京大学' union all
select 2,1,'清华大学' union all
select 3,2,'上海交大' union all
select 4,2,'上海电大'
declare @sql varchar(2000)
declare @str varchar(1000)
set @str = ''
select @str = @str + ',fld' + cast(id as varchar) + ' = (select count(1) from #tbl_school where cityid = ' + rtrim(id) + ')'
from #tbl_city
set @sql = 'select ' + stuff(@str,1,1,'') + ' into #tmp
select * from #tmp'
exec (@sql)
drop table #tbl_city,#tbl_school
create table #tbl_city (id int,city varchar(100))
create table #tbl_school (id int,cityid int,scholl varchar(100))
insert #tbl_city
select 1,'北京' union all
select 2,'上海'
insert #tbl_school
select 1,1,'北京大学' union all
select 2,1,'清华大学' union all
select 3,2,'上海交大' union all
select 4,2,'上海电大'
declare @sql varchar(2000)
declare @str varchar(1000)
set @str = ''
select @str = @str + ',fld' + cast(id as varchar) + ' = (select count(1) from #tbl_school where cityid = ' + rtrim(id) + ')'
from #tbl_city
set @sql = 'select ' + stuff(@str,1,1,'') + ' into #tmp
select * from #tmp'
exec (@sql)
drop table #tbl_city,#tbl_school
#10
CREATE TABLE tbl_city(id INT,City VARCHAR(10))
INSERT tbl_city SELECT 1 , '北京'
UNION ALL SELECT 2,'上海'
UNION ALL SELECT 3,'广州'
CREATE TABLE tbl_School(id INT,cityid INT,school VARCHAR(20))
INSERT tbl_School SELECT 1, 1, '北京大学'
UNION ALL SELECT 2 ,1, '清华大学'
UNION ALL SELECT 3 , 2 , '上海交大'
UNION ALL SELECT 4, 2 , '上海电大'
DECLARE @sql VARCHAR(8000)
SET @sql = ''
SELECT @sql = @sql + ',SUM(CASE cityid WHEN '+RTRIM(id)+' THEN 1 ELSE 0 END) [fld'+RTRIM(id)+']' FROM tbl_city
SET @sql='SELECT ' + STUFF(@sql,1,1,'') + 'INTO tmp FROM tbl_school'
PRINT @sql
EXEC(@sql)
SELECT * FROM tmp
DROP TABLE tbl_City,tmp,tbl_School
/*结果
fld1 fld2 fld3
2 2 0
*/
INSERT tbl_city SELECT 1 , '北京'
UNION ALL SELECT 2,'上海'
UNION ALL SELECT 3,'广州'
CREATE TABLE tbl_School(id INT,cityid INT,school VARCHAR(20))
INSERT tbl_School SELECT 1, 1, '北京大学'
UNION ALL SELECT 2 ,1, '清华大学'
UNION ALL SELECT 3 , 2 , '上海交大'
UNION ALL SELECT 4, 2 , '上海电大'
DECLARE @sql VARCHAR(8000)
SET @sql = ''
SELECT @sql = @sql + ',SUM(CASE cityid WHEN '+RTRIM(id)+' THEN 1 ELSE 0 END) [fld'+RTRIM(id)+']' FROM tbl_city
SET @sql='SELECT ' + STUFF(@sql,1,1,'') + 'INTO tmp FROM tbl_school'
PRINT @sql
EXEC(@sql)
SELECT * FROM tmp
DROP TABLE tbl_City,tmp,tbl_School
/*结果
fld1 fld2 fld3
2 2 0
*/
#11
或者left join之后再统计.