Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. SET NOCOUNT ON;
  2.  
  3. -- Declare @parameters and @command variables
  4. DECLARE @command nvarchar(MAX)
  5. DECLARE @parameters nvarchar(MAX)
  6.  
  7. -- Set @parameters variable
  8. SET @parameters = N'@RequestCode nvarchar(255), @ARC nvarchar(255), @vitastor nvarchar(255), @dateFrom Datetime, @dateTo Datetime, @Svice nvarchar(255), @Distributor nvarchar(255), @Seller nvarchar(255), @Flow nvarchar(255)'
  9.  
  10.  
  11. -- Set @command variable
  12. SET @command = N'SELECT * FROM
  13. (select *,
  14. ROW_NUMBER() OVER (ORDER BY ' + @sortExpression + ') AS RowNum
  15. from
  16. (select r.FileID,r.FilePath,r.Vitadst,r.Seller,r.vitaserv,r.Flusso,r.FlagErr,r.DescErr,r.Data,r.vitastor,max(e.FlagErr) as ErroreOutcome,count(*)as NumeroEsiti, max(r.Type) as SviceType
  17. from
  18. ( select distinct rf.FileID,rf.FilePath
  19. ,rf.Vitadst,av.Descrizione as Seller
  20. ,rf.vitaserv,rf.Flusso,rf.FlagErr,rf.DescErr,rf.Data,rf.vitastor, S.Type
  21.  
  22. from dbo.colmerich rf WITH (nolock)
  23. JOIN dbo.fildrich ef WITH (nolock)
  24. on (rf.FileID = ef.vitafilID) AND (@ARC is null OR @ARC = ef.ARC) AND (@RequestCode is null OR @RequestCode = ef.caseNumber) AND (rf.richturc is null OR (rf.richturc=''GAS''))
  25. left join dbo.hubgas_Svices S on (rf.vitaserv collate SQL_Latin1_General_CP1_CI_AS LIKE ''%'' + S.IDSvice + ''%'')
  26. left join AnagraficaVenditori av with(nolock) on (rf.vitasell = av.vitatbl)
  27. where (@Seller is null OR @Seller = av.vitatbl)
  28. )
  29. r join
  30. dbo.fildrich e WITH (nolock) on (r.FileID = e.vitafilID)
  31. group by r.FileID,r.FilePath,r.Vitadst,r.Seller,r.vitaserv,r.Flusso,r.FlagErr,r.DescErr,r.Data,r.vitastor
  32.  
  33. UNION
  34.  
  35. select rf.FileID, rf.FilePath,rf.Vitadst,av.Descrizione as Seller,rf.vitaserv,rf.Flusso,rf.FlagErr,rf.DescErr,rf.Data,rf.vitastor,NULL as ErroreOutcome,0 as NumeroEsiti, s.Type as SviceType
  36. from dbo.colmerich rf WITH (nolock)
  37. left join dbo.hubgas_Svices S WITH (nolock)
  38. on (rf.vitaserv collate SQL_Latin1_General_CP1_CI_AS LIKE ''%'' + S.IDSvice + ''%'')
  39. left join AnagraficaVenditori av with(nolock) on (rf.vitasell = av.vitatbl)
  40. where vitastor != 1
  41. AND Visionato = 0
  42. AND (@ARC is null OR rf.FileID = ''-1'')
  43. AND (@RequestCode is null OR rf.FileID = ''-1'')
  44. AND (rf.richturc is null OR (rf.richturc=''GAS''))
  45. AND (@Seller is null OR @Seller = av.vitatbl)
  46. ) res
  47. where
  48. (@Distributor is null OR @Distributor = res.Vitadst)
  49. AND (@vitastor is null OR @vitastor = vitastor)
  50. AND (@Svice is null OR @Svice = vitaserv)
  51. AND (SviceType is null OR SviceType like ''CUU%'')
  52. AND (@Flow is null OR @Flow = Flusso)
  53. AND (@dateFrom is null OR (Data >= @dateFrom))
  54. AND (@dateTo is null OR (Data <= @dateTo))
  55. ) rows
  56. WHERE RowNum BETWEEN ' + @lowerBound + ' AND ' + @upperBound
  57.  
  58. DECLARE @tab AS TABLE (FileID NVARCHAR(255) null, FilePath NVARCHAR(255) null ,Vitadst NVARCHAR(255), Seller NVARCHAR(255),vitaserv NVARCHAR(255) null , Flusso NVARCHAR(255) null, FlagErr NVARCHAR(255),DescErr NVARCHAR(255) null, Data Datetime null , vitastor NVARCHAR(255) null, ErroreOutcome NVARCHAR(255) null , NumeroEsiti int, SviceType NVARCHAR(255) null, RowNum int )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement