--一条SQL写不出来,只能用动态的
declare @s varchar(8000)
set @s=''
select @s=@s+' when '''+A+''' then(select max(A) from ['+A+'])'
from [table] group by A
exec('select B,C=case A'+@s+' end from [table]')
go
解决方案 »
- @符号代表的意思
- 问个基础问题~~~~
- 独立局域网(不连internet)中的ip地址是如何给出的,是任意的吗?独立局域网中的IP地址是否与internet的IP地址无关,并且可以重复任意选择?
- 这个问题如何解决?
- 这样的一个SQL怎样来实现?
- 求一查询语句
- SqlDumpExceptionHandler: 进程 51 发生了严重的异常 c0000005 EXCEPTION_ACCESS_VIOLATION。SQL Server 将终止该进程。
- 关于在ACCESS中使用触发器
- 实在太难的 SQL!!!
- 关于《书店管理系统》的数据库,欢迎大家一起讨论………………………………
- 〓★★★〓 存储过程分页: 对于非唯一值字段排序 有没有好的解决方法?
- 初学数据库-有哪位大哥能告诉我什么是存储过程啊
create table [table](A sysname,B varchar(10))
insert [table] select 'table_1','a'
union all select 'table_2','b'create table table_1(A int)
insert table_1 select 1
union all select 2create table table_2(A int)
insert table_2 select 3
union all select 4
go--一条SQL写不出来,只能用动态的
declare @s varchar(8000)
set @s=''
select @s=@s+' when '''+A+''' then(select max(A) from ['+A+'])'
from [table] group by A
exec('select B,C=case A'+@s+' end from [table]')
go--删除测试
drop table [table],table_1,table_2/*--测试结果B C
---------- -----------
a 2
b 4
--*/
declare @ss varchar(8000)
set @ss = ''
select @ss = 'select ' + b + ',(select max(a) from ' + a + ') union all ' from [table]
set @ss = right(@ss,len(@ss) - 11)
--注意是否可能超过8000字符
exec(@ss)
^_^!