USE [MyDataBase]
GO
/****** Object: StoredProcedure [dbo].[GetVipDetails] Script Date: 07/05/2012 09:39:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Proc [dbo].[GetVipDetails]
@pageindex int=1 ,
@pagesize int=10,
@VipAccount nvarchar(20)=null, --VIP账户
@VipCompanyUserID bigint=null, --VIP用户的公司用户ID
@ProxyCompanyID bigint=null, --渠道商公司ID/云平台公司ID
@ProxyManagerID bigint=null, --业务经理ID
@ProxyCompanyUserID bigint=null --业务员ID
as
declare @SqlStr varchar(4000) --要插入数据到临时表执行的SQL
declare @sqlstr2 varchar(2000) -- 要插入数据到临时表的条件
declare @SqlStr3 varchar(4000) --pageindex为1时不带条件的sql
declare @SqlStr4 varchar(2000) --条件组合的sql
declare @SqlStr5 varchar(4000) --pageindex > 1时要执行的sql
declare @ResultSql varchar(4000) --最终要执行的sql
declare @OutCountSql varchar(1000) --输出总记录的sql
begin
--创建临时数据表
create table #TempVipDetails
(
ID bigint primary key ,
VIPAccount nvarchar(20),
CompanyUserID bigint,
UserID bigint,
CompanyID bigint,
CompanyName nvarchar(250),
[Status] bit,
IsAssignedChannel bit,
IsAssignedManager bit,
IsAssignedUser bit,
ProxyCompanyID bigint,
ProxyManagerID bigint,
AssignManagerDateTime datetime,
ProxyCompanyUserID bigint,
AssignUserDateTime datetime,
UserName nvarchar(20),
RealName nvarchar(20),
Sex int,
OICQ nchar(20),
Email nvarchar(50),
MobileNumber1 nvarchar(15),
Brthday datetime,
HomeAddress nvarchar(200),
OfficeAddress nvarchar(200),
OfficeNumber nvarchar(20),
HomePhoneNumber nvarchar(20),
ProxyCompanyName nvarchar(250),
ProxyManagerName nvarchar(20),
ProxyUserName nvarchar(20),
IsUsed bit
)
begin
set @sqlstr2=' 1 = 1 '
if @VipAccount is not null
begin
set @sqlstr2+=' and VIPAccount = '''+@VipAccount+''''
end
else if @VipCompanyUserID>0
begin
set @sqlstr2+=' and VipCompanyUserID ='+STR(@VipCompanyUserID)
end
set
@SqlStr='insert into #TempVipDetails
(
ID,
VIPAccount,
CompanyUserID,
CompanyID,
[Status],
IsAssignedChannel,
IsAssignedManager,
IsAssignedUser,
IsUsed
)
select
ID,
VIPAccount,
VipCompanyUserID,
CompanyID,
[Status],
IsAssignedChannel,
IsAssignedManager,
IsAssignedUser,
IsUsed
from SSIP_VIPAccount
where '+@sqlstr2
end
print @SqlStr;
exec(@SqlStr);--执行SQL 添加vip数据操作
--更新vip与渠道商运营商关系数据
update a set
a.ProxyCompanyID=b.ProxyCompanyID,
a.ProxyManagerID=b.ProxyManagerID,
a.AssignManagerDateTime=b.AssignManagerDateTime,
a.ProxyCompanyUserID=b.ProxyCompanyUserID,
a.AssignUserDateTime=b.AssignUserDateTime
from #TempVipDetails as a,[ssip_vipusersproxy] as b
where a.CompanyUserID=b.CompanyUserID
--更新 用户公司基本数据操作
update a set
a.CompanyName=b.CompanyName
from #TempVipDetails as a,[SSIP-SSO].[dbo].[SSO_Company] as b
where a.CompanyID=b.CompanyID
--更新用户基本数据
update a set
a.UserID=b.UserID,
a.UserName=b.UserName,
a.RealName=b.RealName,
a.Sex=b.Sex,
a.OICQ=b.OICQ,
a.Email=b.Email,
a.MobileNumber1=b.MobileNumber1,
a.Brthday=b.Brthday,
a.HomeAddress=b.HomeAddress,
a.OfficeAddress=b.OfficeAddress,
a.OfficeNumber=b.OfficeNumber,
a.HomePhoneNumber=b.HomePhoneNumber
from #TempVipDetails as a,[SSIP-SSO].[dbo].[SSO_CompanyUsers] as b
where a.CompanyUserID=b.CompanyUserID
--更新渠道商公司名称
update a set
a.ProxyCompanyName=b.CompanyName
from #TempVipDetails as a , [SSIP-SSO].[dbo].[SSO_Company] as b
where a.ProxyCompanyID=b.CompanyID
--更新业务经理名称
update a set
a.ProxyManagerName=b.RealName
from #TempVipDetails as a,[SSIP-SSO].[dbo].[SSO_Users] as b
where a.ProxyManagerID=b.UserID
--更新业务员姓名
update a set
a.ProxyUserName=b.RealName
from #TempVipDetails as a,[SSIP-SSO].[dbo].[SSO_Users] as b
where a.ProxyCompanyUserID=b.UserID
set
@SqlStr3='select top '+STR(@pagesize)+' * from #TempVipDetails where 1 = 1 '
set
@SqlStr4=''
if @ProxyCompanyID>0
begin
set @SqlStr4+=' and ProxyCompanyID = '+STR(@ProxyCompanyID)
end
else if @ProxyManagerID>0
begin
set @SqlStr4+=' and ProxyManagerID = '+STR(@ProxyManagerID)
end
else if @ProxyCompanyUserID>0
begin
set @SqlStr4+=' and ProxyCompanyUserID = '+STR(@ProxyCompanyUserID)
end
set
@SqlStr5=' select top ('+STR(@pagesize)+') * from (
select ROW_NUMBER() over(order by ID) as RowNum,*
from #TempVipDetails
where 1 = 1 '+@SqlStr4+'
) as VipDetailsInfo
where VipDetailsInfo.RowNum >'+str((@pageindex-1)*@pagesize)+'
and VipDetailsInfo.RowNum <= '+STR(@pageindex*@pagesize)
set
@OutCountSql ='select COUNT(1) from #TempVipDetails where 1 = 1 '+@SqlStr4
--查询结果
if @pageindex<=1
begin
set @ResultSql=@SqlStr3+@SqlStr4
exec (@ResultSql) --执行sql语句
exec (@OutCountSql) --查询所有总数
end
else if @pageindex>1
begin
set @ResultSql=@SqlStr5
exec (@ResultSql) --执行sql语句
exec (@OutCountSql) --查询所有总数
end
--删除临时表
drop table #TempVipDetails
end