Sunday, 7 July 2013

How to get multiple tables using stored procedure Dynamically

USE [DuckCreek]
GO
/****** Object:  StoredProcedure [dbo].[Usp_Getreports]    Script Date: 07/08/2013 09:21:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- EXEC Usp_Getreports '2013-06-05 00:00:00.000', '2013-07-05 00:00:00.000', 'wc_'
ALTER procedure [dbo].[Usp_Getreports]
@Startdate date=null,
@Enddate date=null,
@Lob varchar(10)=null
as
begin
    declare @Inboundquery varchar(max)
    declare @Outboundquery varchar(max)
    declare @Failedpolicesquery varchar(max)
    declare @Reporttime varchar(max)
   
        set @Inboundquery = 'select ROW_NUMBER() OVER(ORDER BY TransactionDatatime DESC) AS SNo, * from ';
        set @Inboundquery = @Inboundquery +@Lob+'session s inner join '+@Lob+'data d on s.session_Id=d.session_Id inner join '+@Lob+'policy p on d.data_Id=p.data_Id where DATEADD(D, 0, DATEDIFF(D, 0, s.TransactionDatatime)) ';
        set @Inboundquery = @Inboundquery  + ' BETWEEN ''' + convert(varchar(10),@StartDate,110) + ''' AND ''' + convert(varchar(10),@EndDate,110) + ''''
       
       
        set @Outboundquery = 'select ROW_NUMBER() OVER(ORDER BY Record_Date DESC) AS SNo, Transaction_ID,Record_Date from ';
        set @Outboundquery = @Outboundquery +@Lob+'PROC_Trans_IDs where DATEADD(D, 0, DATEDIFF(D, 0, Record_Date))';
        set @Outboundquery = @Outboundquery + ' BETWEEN ''' + convert(varchar(10),@StartDate,110) + ''' AND ''' + convert(varchar(10),@EndDate,110) + ''''
       
        set @Failedpolicesquery = 'select ROW_NUMBER() OVER(ORDER BY TransactionDatatime DESC) AS SNo,Failedpolicy_Id,Session_Id, Error, TransactionId, TransactionDatatime from ';
        set @Failedpolicesquery = @Failedpolicesquery +@Lob+'FailedPolicies where DATEADD(D, 0, DATEDIFF(D, 0, TransactionDatatime)) ';
        set @Failedpolicesquery = @Failedpolicesquery + ' BETWEEN ''' + convert(varchar(10),@StartDate,110) + ''' AND ''' + convert(varchar(10),@EndDate,110) + ''''
       
        set @Reporttime='select convert(datetime,getdate())';
        exec (@Reporttime)
        print @Inboundquery;
        exec (@Inboundquery)
        exec (@Outboundquery)
        exec (@Failedpolicesquery)
       
       
end


No comments:

Post a Comment