"数据”--“导入外部数据”按提示操作
请采纳。
② SQL行列转置
declare @sql varchar(8000)
declare @date varchar(20)
declare @bmbh varchar(20)
declare @ckbh varchar(20)
set @date='20110101'
set @bmbh='500103'
set @ckbh='0601'set @sql = 'select max(lsbmzd_bmmc) as bmmc,kcrkd2_wlbh as wlbh,max(lswlzd_wlmc) as wlmc,max(lswlzd_ggxh) as ggxh ,sum(kcrkd2_sssl) as sum'
select @sql = @sql + ', max(case kcrkd1_kcywrq when ''' + kcrkd1_kcywrq + ''' then kcrkd2_sssl else 0 end) [' + kcrkd1_kcywrq + ']'
from (select distinct kcrkd1_kcywrq from kcrkd1
where kcrkd1_kcywrq>= substring(convert(varchar(100),dateadd(mm,-1,Convert(DateTime,@date)),112),1,6)+'26'
and kcrkd1_kcywrq<=substring(@date,1,6)+'25') as a
set @sql = @sql + ' from kcrkd1,kcrkd2,lswlzd,lsbmzd
where kcrkd1_lsbh=kcrkd2_lsbh and kcrkd1_pjlx=''j'' and kcrkd1_ckbh='''+@ckbh+''' and kcrkd1_bmbh=lsbmzd_bmbh and kcrkd2_wlbh=lswlzd_wlbh and kcrkd1_bmbh='''+@bmbh+'''
group by kcrkd2_wlbh'
exec(@sql)
③ SQL怎样把一个表的数据插入到另一个表里
复制表结构及数据到新表select * into 目标表名 from 源表名
将数据库A中某表的的某列字段,更新到数据库B中某表的某列字段:(use master 数据库)
update a
set a.name=b.name
from temp1.dbo.tableA a,temp2.dbo.tableA b
where a.id=b.id
④ 如何用SQL把下面两个表完全倒过来,行变列,列变行完全转置!
静态脚本:
select '收入' as 项目
, case when 项目='一厂本月' then 收入 else null end as 一厂本月
, case when 项目='一厂本年' then 收入 else null end as 一厂本年
, case when 项目='二厂本月' then 收入 else null end as 二厂本月
, case when 项目='二厂本年' then 收入 else null end as 二厂本年
, case when 项目='三厂本月' then 收入 else null end as 三厂本月
, case when 项目='三厂本年' then 收入 else null end as 三厂本年
from 表名
union all
select '成本' as 项目
, case when 项目='一厂本月' then 成本 else null end as 一厂本月
, case when 项目='一厂本年' then 成本 else null end as 一厂本年
, case when 项目='二厂本月' then 成本 else null end as 二厂本月
, case when 项目='二厂本年' then 成本 else null end as 二厂本年
, case when 项目='三厂本月' then 成本 else null end as 三厂本月
, case when 项目='三厂本年' then 成本 else null end as 三厂本年
from 表名
union all
select '其他费用' as 项目
, case when 项目='一厂本月' then 其他费用 else null end as 一厂本月
, case when 项目='一厂本年' then 其他费用 else null end as 一厂本年
, case when 项目='二厂本月' then 其他费用 else null end as 二厂本月
, case when 项目='二厂本年' then 其他费用 else null end as 二厂本年
, case when 项目='三厂本月' then 其他费用 else null end as 三厂本月
, case when 项目='三厂本年' then 其他费用 else null end as 三厂本年
from 表名
union all
select '毛利' as 项目
, case when 项目='一厂本月' then 毛利 else null end as 一厂本月
, case when 项目='一厂本年' then 毛利 else null end as 一厂本年
, case when 项目='二厂本月' then 毛利 else null end as 二厂本月
, case when 项目='二厂本年' then 毛利 else null end as 二厂本年
, case when 项目='三厂本月' then 毛利 else null end as 三厂本月
, case when 项目='三厂本年' then 毛利 else null end as 三厂本年
from 表名
改动态脚本(只改项目,即改原表行不定,列数目固定):
declare @sql nvarchar(max)
set @sql=''
set @sql=@sql+'
select ''收入'' as 项目
'
select @sql=@sql+', case when 项目='''+项目+''' then 收入 else null end as '+项目
from 表名
set @sql=@sql+'
from 表名
'
set @sql=@sql+'union all
select ''成本'' as 项目
'
select @sql=@sql+', case when 项目='''+项目+''' then 成本 else null end as '+项目
from 表名
set @sql=@sql+'
from 表名
'
set @sql=@sql+'union all
select ''其他费用'' as 项目
'
select @sql=@sql+', case when 项目='''+项目+''' then 其他费用 else null end as '+项目
from 表名
set @sql=@sql+'
from 表名
'
set @sql=@sql+'union all
select ''毛利'' as 项目
'
select @sql=@sql+', case when 项目='''+项目+''' then 毛利 else null end as '+项目
from 表名
set @sql=@sql+'
from 表名
'
exec sp_executesql @sql
改动态脚本(改原表行和列数目不固定,两层动态脚本,能实现但基本难读):
declare @sql nvarchar(max)
set @sql='declare @sql_in nvarchar(max)
set @sql_in='' '' '
set @sql=@sql+'select @sql_in=@sql_in+''union all
select ''''''+name+'''''' as 项目
'
select @sql=@sql+', case when 项目='''''+项目+''''' then ''+name+'' else null end as '+项目
from 表名
set @sql=@sql+'
from 表名
''
from syscolumns where id=(select id from sysobjects where name=''表名'')
order by colorder
set @sql_in=stuff(@sql_in,1,10,'''')
exec sp_executesql @sql_in
'
exec sp_executesql @sql
⑤ SQL中如何将查询后的结果集转成Excel表格
在查询分析器生成的
结果集
里点鼠标右键
有个“将结果另存为”,选csv格式就可以了
csv用EXCEL可以打开,如果觉得不合适,可以在EXCEL里选择另存为xsl
直接用查询分析器转EXCEL,不说其难度,光是在服务器上开启sp_shell就足够危险