动态Sql获取动态表数据
来源:互联网 发布:网络言情小说合集 编辑:程序博客网 时间:2024/05/29 10:05
转自 http://www.cnblogs.com/zhuisuo/archive/2010/11/23/1885623.html
if object_id('country') is not null
drop table country
Go
Create Table country( id int null, name varchar(50) null)
if object_id('Product') is not null
drop table Product
go
create table Product( id int null, countryid int null, name varchar(50) null)
insert Into country values(1,'中国');
insert Into country values(2,'美国');
insert Into country values(3,'英国');
insert Into country values(4,'法国');
insert Into country values(5,'日本');
Insert into Product values(1,1,'产品1');
Insert into Product values(2,2,'产品2');
Insert into Product values(3,3,'产品1');
Insert into Product values(4,7,'产品3');
go
declare @id varchar(100),@name varchar(100)
declare @sql varchar(max)
set @sql='select name'
declare name_zuisuo cursor for
select id,name from country
open name_zuisuo
fetch name_zuisuo into @id,@name
while @@fetch_status=0
begin
set @sql=@sql+',max(case when countryid='+char(39)+@id+char(39)
+' then '+char(39)+'yes'+char(39)+'else '+char(39)+'no'+char(39)+' end) as '
+char(39)+@name+char(39)+''
fetch name_zuisuo into @id,@name
end
close name_zuisuo
deallocate name_zuisuo
set @sql=@sql+' from product group by name'
print @sql
exec (@sql)
--
--select * from product
--select name,
--max(case when countryid='1' then 'yes'else 'no' end) as '中国'
--,max(case when countryid='2' then 'yes'else 'no' end) as '美国'
--,max(case when countryid='3' then 'yes'else 'no' end) as '英国'
--,max(case when countryid='4' then 'yes'else 'no' end) as '法国'
--,max(case when countryid='5' then 'yes'else 'no' end) as '日本'
--from product group by name
--------------------------------------------------
上面的可能不是最好的,但是相信在自己的ERP工作会用到。、
感谢作者
- 动态Sql获取动态表数据
- listview动态获取数据
- ECharts动态获取数据
- SQL 数据动态汇总
- T-SQL删除动态表中的数据
- 获取动态SQL执行结果
- 获取动态sql执行结果
- 某一列动态获取数据
- highcharts如何动态获取数据
- HighChart扩展-动态获取数据
- echarts ajax 动态获取数据
- Android动态获取传感器数据
- bootstrapTable动态渲染数据获取
- 动态视图中的获取数据
- echarts 动态获取数据demo
- 异步刷新动态获取数据
- 动态SQL 处理BLOB数据
- JS动态添加表行,并验证数据,获取数据。
- SQL语句 UNION 和 UNION ALL 使用(二)
- vim map
- 如何消除JBuilderX默认的提示音
- smartform输出格式设置
- 自定义ListView item的例子
- 动态Sql获取动态表数据
- 深入浅出VC++串口编程之基于Win32 API
- PHP 脚本加密 工具(软件)
- 泛型List导出Excel
- Windows 7最全的硬盘安装方法
- [zt] Perl的简单语法(与C语言语法的异同)
- vim usage
- extjs表单的初始化前设只读和之后设只读
- VC打开工程时 弹出 Visual SourceSafe Login的解决办法