"數據」--「導入外部數據」按提示操作
請採納。
② 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就足夠危險