Sql存储过程即返回结果集又返回值以及动态创建视图(一)

2014-11-24 12:18:20 · 作者: · 浏览: 0
Sql存储过程即返回结果集又返回值以及动态创建视图
[sql] 
/****** Object:  StoredProcedure [dbo].[GetXiaFa_Record_Tow]    Script Date: 07/10/2013 23:50:33 ******/  
SET ANSI_NULLS ON  
GO  
SET QUOTED_IDENTIFIER ON  
GO  
  
ALTER PROCEDURE [dbo].[GetXiaFa_Record_Tow]  
        @PipeIDs  nvarchar(1000) ,--基础通道ID  
        @DateStart  nvarchar(30),  
        @DateEnd nvarchar(30),  
        @cnt int,               --基础通道ID个数  
        @Status     nvarchar(10),       --发送状态  
        @Destination_no varchar(1000),  --接收号码  
        @Sms_SendContents varchar(max),  --短信内容  
        @user_code varchar(50),          --用户  
        @MessageType nvarchar(10),  --是短信 还是彩信  
        @IsStatus nvarchar(10),     --用户类型 是管理员还是普通用户  普通用户没有回执状态  
        @pageIndex int , --当前页  
        @pageSizes  int, -- 一个页面显示多少条数据  
        @view_name nvarchar(100) output  --返回视图名  
AS  
BEGIN  
     DECLARE @j INT=1,@PipeId nvarchar(100)  --- 分割上传的列  
DECLARE @sql nvarchar(max)=''  
declare @sqlCount nvarchar(max)=''  
WHILE @j<=@cnt   
    BEGIN  
        SET @PipeId= dbo.EDM_FN_SplitString(@PipeIDs,',',@j)  
        IF LEN(@PipeId)>0  
        BEGIN  
            if(@IsStatus='1') --是否让这个用户显示短信发送的真实状态  
            begin  
                 IF LEN(@sql)=0  
                  begin   
                        -- set @sql='select '''',Seq_no as 序列号,Destination_no as 发送号码,Sms_SendContents as 短信内容,Send_Status as 提交状态,Send_Date as 发送时间,Oper_Date as 操作时间,Send_Predate as 预发时间,  case is_Pre when 1 then ''任务发送'' else ''非任务发送'' end as 是否任务,RealStatus as 到达状态,MtDateTime as 状态时间,id,MessageType,isnull(MmcIndex,1) as MmcIndex  from B_Sms_Send_info_'+@PipeId+' where Send_user='''+@user_code+''' and (Oper_Date >=convert(datetime,'''+@DateStart+' 00:00:00'',120) or ''''= '+@DateStart+' ) and (Oper_Date <=convert(datetime,'''+@DateEnd+' 23:59:59'',120) or ''''= '+@DateEnd+' ) and (Destination_no='''+@Destination_no+''' or ''''='''+@Destination_no+''') '  
                         set @sql='select '''' as 序号,Seq_no as 序列号,Destination_no as 发送号码,Sms_SendContents as 短信内容,Send_Status as 提交状态,Send_Date as 发送时间,Oper_Date as 操作时间, RealStatus as 到达状态,MtDateTime as 状态时间,id,MessageType,isnull(MmcIndex,1) as MmcIndex   from B_Sms_Send_info_'+@PipeId+' where Send_user in('+@user_code+') and (Oper_Date >=convert(datetime,'''+@DateStart+' 00:00:00'',120) or ''''= '''+@DateStart+''' ) and (Oper_Date <=convert(datetime,'''+@DateEnd+' 23:59:59'',120) or ''''= '''+@DateEnd+''' ) and (Destination_no='''+@Destination_no+''' or ''''='''+@Destination_no+''') and Send_Status!=-11 '  
                         set @sqlCount='select COUNT(*) as Counts from B_Sms_Send_Info_'+@PipeId+' where Send_user in('+@user_code+') and (Oper_Date >=convert(datetime,'''+@DateStart+' 00:00:00'',120) or ''''= '''+@DateStart+''' ) and (Oper_Date <=convert(datetime,'''+@DateEnd+' 23:59:59'',120) or ''''= '''+@DateEnd+''' ) and (Destination_no='''+@Destination_no+''' or ''''='''+@Destination_no+''')and Send_Status!=-11  '  
                         if(@Status='0')  
                         begin  
                            set @sql+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) '  
                            set  @sqlCount+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) '  
                         end  
                         if(@Status='1')  
                         begin  
                            set @sql+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and Send_Status=1  '  
                            set @sqlCount+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and Send_Status=1  '  
                         end  
                         if(@Status='-1')  
                         begin  
                            set @sql+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and Send_Status=-1  '  
                            set @sqlCount+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and Send_Status=-1  '  
                         end  
                         if(@Status='DELIVRD')  
                         begin  
                            set @sql+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and RealStatus = ''DELIVRD'' '  
                            set @sqlCount+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and RealStatus = ''DELIVRD'' '  
                         end  
                         if(@Status='2')  
                         begin  
                            set @sql+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and (RealStatus <> ''DELIVRD'' and RealStatus <> '' and RealStatus is not null)  '  
                            set @sqlCount+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and (RealStatus <> ''DELIVRD'' and RealStatus <> '' and RealStatus is not null)  '  
                         end  
                          if(@Status='NULL')  
                         begin  
                            set @sql+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and RealStatus is null '  
                            set @sqlCount+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and RealStatus is null '  
                         end  
                         --set @sql+='  ) as IDWithRowNumber where IDRank>
'+ convert(nvarchar(10),@pageSizes )+'*'+convert(nvarchar(10),@pageIndex ) +' and IDRank<='+ convert(nvarchar(10),@pageSizes )+'*('+convert(nvarchar(10),@pageIndex ) +'+1)' end ELSE begin -- set @sql+=' union all select '''',Seq_no as 序列号,Destination_no as 发送号码,Sms_SendContents as 短信内容,Send_Status as 提交状态,Send_Date as 发送时间,Oper_Date as 操作时间,Send_Predate as 预发时间, case is_Pre when 1 then ''任务发送'' else ''非任务发送'' end as 是否任务,RealStatus as 到达状态,MtDateTime as 状态时间,id,MessageType,isnull(MmcIndex,1) as MmcIndex from B_Sms_Send_info_'+@PipeId+' where Send_user='''+@user_code+''' and (Oper_Date >=convert(datetime,'''+@DateStart+' 00:00:00'',120) or ''''= '+@DateStart+' ) and (Oper_Date <=convert(datetime,'''+@DateEnd+' 23:59:59'',120) or ''''= '+@DateEnd+' ) and (Destination_no='''+@Destination_no+''' or ''''='''+@Destination_no+''')' set @sql+=' union all select '''' as 序号,Seq_no as 序列号,Destination_no as 发送号码,Sms_SendContents as 短信内容,Send_Status as 提交状态,Send_Date as 发送时间,Oper_Date as 操作时间,RealStatus as 到达状态,MtDateTime as 状态时间,id,MessageType,isnull(MmcIndex,1) as MmcIndex from B_Sms_Send_info_'+@PipeId+' where Send_user in('+@user_code+') and (Oper_Date >=convert(datetime,'''+@DateStart+' 00:00:00'',120) or ''''= '''+@DateStart+''' ) and (Oper_Date <=convert(datetime,'''+@DateEnd+' 23:59:59'',120) or ''''= '''+@DateEnd+''' ) and (Destination_no='''+@Destination_no+''' or ''''='''+@Destination_no+''')and Send_Status!=-11 ' set @sqlCount+=' union all select COUNT(*) as Counts from B_Sms_Send_Info_'+@PipeId+' where Send_user in('+@user_code+') and (Oper_Date >=convert(datetime,'''+@DateStart+' 00:00:00'',120) or ''''= '''+@DateStart+''' ) and (Oper_Date <=convert(datetime,'''+@DateEnd+' 23:59:59'',120) or ''''= '''+@DateEnd+''' ) and (Destination_no='''+@Destination_no+''' or ''''='''+@Destination_no+''') and Send_Status!=-11 ' if(@Status='0') begin set @sql+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) ' set @sqlCount+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) ' end if(@Status='1') begin set @sql+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and Send_Status=1 ' set @sqlCount+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and Send_Status=1 ' end if(@Status='-1') begin set @sql+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and Send_Status=-1 ' set @sqlCount+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and Send_Status=-1 ' end if(@Status='DELIVRD') begin set @sql+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and RealStatus = ''DELIVRD'' ' set @sqlCount+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and RealStatus = ''DELIVRD'' ' end if(@Status='2') begin set @sql+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and (RealStatus <> ''DELIVRD'' and RealStatus <> '' and RealStatus is not null) ' set @sqlCount+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and (RealStatus <> ''DELIVRD'' and RealStatus <> '' and RealStatus is not null) ' end if(@Status='NULL') begin set @sql+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and RealStatus is null ' set @sqlCount+=' and MessageType ='+@MessageType+' and (Sms_SendContents like ''%'+@Sms_SendContents+'%''or ''''='''+@Sms_SendContents+''' ) and RealStatus is null '