Share via

Require modification to a dynamic query

Debadas Nayak 21 Reputation points
2021-01-04T16:21:32.897+00:00

Hi,

I have this query running at specific hours of the day to provide us details about exceptions observed with call recording. Now our requirement is to report us only those exceptions which are associated with a call and not with voicemails or emails etc.. The column that holds the value as "call" is "MediaType" in a view "vwReserved". Now I want to join this view with the table with alias "d" on vwReserved.iInteractionID=d.iInteractionID with a condition where vwReserved.MediaType='call'. Could you please let me know how can I add this in to the existing query. I did try this myself but getting an error invalid column name 'call'.

declare @tablename1 varchar(20);
declare @tablename2 varchar(20);
declare @tablename3 varchar(20);
declare @tablename4 varchar(20);
declare @tablename5 varchar(20);
declare @number varchar(4)
set @number=(select MAX(vcSetNumber) from tblInteractionCatalog where GETDATE() between [dtMinStartTime] and [dtMaxStartTime])
set @tablename1='tblexception' + @number
set @tablename2='tblrecording' + @number
set @tablename3='tblinteraction' + @number
set @tablename4='tblparticipant' + @number
set @tablename5='tblcallinteraction' + @number
declare @alenzi nvarchar(max) = N'Select distinct a.iinteractionid,d.dtInteractionLocalStartTime,d.dtInteractionLocalstopTime,b.ilogger as LOGGER,
IC.nvcResourceHost as IC_SERVER, a.iExceptionTypeID as EXceptions,a.vcExceptionDetail as Exp_Detail,
par.nvcFormattedName, c.nvcStation as Station_ID, c.iSwitchID as Switch_ID, e.tiCallDirectionTypeID
from ' + @tablename1 + ' as a inner join ' + @tablename2 + ' as b on a.iinteractionid=b.iinteractionid
inner join ' + @tablename3 + ' as d on a.iInteractionID = d.iInteractionID
inner join ' + @tablename4 + ' as c on a.iinteractionid=c.iInteractionID
inner join nice_admin..tblUser as PAR on PAR.iUserId=c.iUserID
inner join nice_admin..tblSystemResource as IC on IC.iResourceId=d.siICID
inner join ' + @tablename5 + ' as e on e.iInteractionID = d.iInteractionID
where iexceptiontypeid=12 and d.dtInteractionLocalStartTime >= DATEADD (minute,-30, Getdate()) and e.tiCallDirectionTypeID >0
and c.iSwitchID >0 and b.tiRecordingRecordedTypeID in (0,2)
order by d.dtInteractionLocalStartTime desc'
print @alenzi
execute sp_executeSQL @alenzi ;

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

0 comments No comments

Answer accepted by question author

Tom Phillips 17,786 Reputation points
2021-01-04T16:34:41.263+00:00
declare @tablename1 varchar(20);
declare @tablename2 varchar(20);
declare @tablename3 varchar(20);
declare @tablename4 varchar(20);
declare @tablename5 varchar(20);
declare @number varchar(4)
set @number=(select MAX(vcSetNumber) from tblInteractionCatalog where GETDATE() between [dtMinStartTime] and [dtMaxStartTime])
set @tablename1='tblexception' + @number
set @tablename2='tblrecording' + @number
set @tablename3='tblinteraction' + @number
set @tablename4='tblparticipant' + @number
set @tablename5='tblcallinteraction' + @number
declare @sql nvarchar(max) = N'Select distinct a.iinteractionid,d.dtInteractionLocalStartTime,d.dtInteractionLocalstopTime,b.ilogger as LOGGER,
IC.nvcResourceHost as IC_SERVER, a.iExceptionTypeID as EXceptions,a.vcExceptionDetail as Exp_Detail,
par.nvcFormattedName, c.nvcStation as Station_ID, c.iSwitchID as Switch_ID, e.tiCallDirectionTypeID
from ' + @tablename1 + ' as a inner join ' + @tablename2 + ' as b on a.iinteractionid=b.iinteractionid
inner join ' + @tablename3 + ' as d on a.iInteractionID = d.iInteractionID
inner join vwReserved as r ON r.iInteractionID=d.iInteractionID AND r.MediaType=''call''
inner join ' + @tablename4 + ' as c on a.iinteractionid=c.iInteractionID
inner join nice_admin..tblUser as PAR on PAR.iUserId=c.iUserID
inner join nice_admin..tblSystemResource as IC on IC.iResourceId=d.siICID
inner join ' + @tablename5 + ' as e on e.iInteractionID = d.iInteractionID
where iexceptiontypeid=12 and d.dtInteractionLocalStartTime >= DATEADD (minute,-30, Getdate()) and e.tiCallDirectionTypeID >0
and c.iSwitchID >0 and b.tiRecordingRecordedTypeID in (0,2)
order by d.dtInteractionLocalStartTime desc'
print @sql
execute sp_executeSQL @sql;

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. EchoLiu-MSFT 14,626 Reputation points
    2021-01-07T09:23:37.617+00:00

    Hi @Debadas Nayak ,

    I'm glad you got help in the community. If you have any questions, you are welcome to come to the community for help.

    Regards
    Echo

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.