视图,存储实现行列转换

 

账号表:Accounts

       accountid ,acountName,accountType,accountDesc,BeginAmount,Amount,editedBy,editedDateTime

会计明目表:AccountCode

       accountCode,accountCodeName,accountType,,editedBy,editedDateTime

流水记录表:AccountTally

logid,accountid,accountcode,amount,accountDate,Appored,VouchNo,…

 

 

ContractedBlock.gif ExpandedBlockStart.gif 行列转换 存储,视图
--视图:
ALTER      VIEW [dbo].[uvw_AccountsCollRpt]
AS
SELECT m.accountcodename ,c.accountname, t.amount ,t.accountid,t.accountcode,t.accountdate,m.accounttype

 
from T_AccountTally t inner join M_accountCode m on t.accountcode= m.accountcode 

 
inner join M_accounts c on c.accountid = t.accountid
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
select * from [uvw_AccountsCollRpt

 

--存储:
alter procedure   [dbo
].[usp_AccountsCollRpt]
(
@accountType tinyint)
AS
 
declare @sql varchar(8000)
set @sql = 'select accountname as 帐号名,accountid as 帐号编号'
select @sql = @sql + ' , sum(case accountcodename when ''' + accountcodename + ''' then amount else 0 end) [' + accountcodename + ']'
from (select distinct accountcodename from [uvw_AccountsCollRpt] where accounttype=@accountTypeas a
set @sql = @sql + ' from [uvw_AccountsCollRpt] group by accountname,accountid'
exec(@sql)
RETURN

 

ContractedBlock.gif ExpandedBlockStart.gif 加条件参数的存储
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER  procedure   [dbo].[usp_AccountsCollRpt]
@accountid nvarchar(300--必需是nvarchar 
 -- exec  [usp_AccountsCollRpt]  '''1101001'',''1101027'',''1102053'''  
AS
declare @sql varchar(8000)
set @sql = 'select accountname as 帐号名,accountid as 帐号编号 '
select @sql = @sql + ' , sum(case accountcodename when ''' + accountcodename + ''' then amount else 0 end) [' + accountcodename + ']'
from (select distinct accountcodename from [uvw_AccountsCollRpt] where accounttype=1as a
set @sql = @sql + ' from [uvw_AccountsCollRpt] where  accountid in ( ' +@accountid+' )  group by accountname,accountid'
exec(@sql)

 
declare @sql1 varchar(8000)
set @sql1 = 'select accountname as 帐号名,accountid as 帐号编号 '
select @sql1 = @sql1 + ' , sum(case accountcodename when ''' + accountcodename + ''' then amount else 0 end) [' + accountcodename + ']'
from (select distinct accountcodename from [uvw_AccountsCollRpt] where accounttype=0as a
set @sql1 = @sql1 + ' from [uvw_AccountsCollRpt] where  accountid in ('+ @accountid+') group by accountname,accountid'
exec(@sql1)


RETURN
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO

转载于:https://www.cnblogs.com/chirs888888/archive/2009/08/14/1546319.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值