Advertisement
Guest User

GetAccountFilterData

a guest
Oct 7th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.67 KB | None | 0 0
  1.  
  2. CREATE PROCEDURE [dbo].[GetAccountFilterData]
  3.     @accountId nvarchar(100),
  4.     @country nvarchar(100),
  5.     @testType varchar(5),
  6.     @associatedAccounts nvarchar(max)
  7. AS
  8. BEGIN
  9. SET NOCOUNT ON;
  10.  
  11. declare @associatedAccountsTable table (id int, data nvarchar(100))
  12.  
  13. insert into @associatedAccountsTable
  14. select * from [dbo].[Split](@associatedAccounts, ',')
  15.  
  16. if @testType = 'static string'
  17. begin
  18.  
  19. select * from (
  20. select ri.ReaderSerialNumber as Value,
  21. case
  22.     when ri.readertitle is null or len(ri.readertitle) = 0 then ri.ReaderSerialNumber
  23.     else ri.readertitle
  24. end as Name
  25. from ReaderInfo ri
  26. inner join Asset ass on ri.ReaderSerialNumber = ass.SerialNumber
  27. where ass.AccountNumber = @accountId
  28. union
  29. select ri.ReaderSerialNumber as Value,
  30. case
  31.     when ri.readertitle is null or len(ri.readertitle) = 0 then ri.ReaderSerialNumber
  32.     else ri.readertitle
  33. end as Name
  34. from ReaderInfo ri
  35. inner join MastitisTest mt on ri.ReaderSerialNumber = mt.ReaderSerialNumber
  36. inner join Account a on a.AccountNumber = mt.AccountNumber
  37. inner join CustomerLocation cl on mt.CustomerLocationID = cl.ID
  38. left join Account ca on ca.AccountNumber = cl.ConsumingAccountNumber
  39. where (a.AccountNumber = @accountId and (@country is null or a.Country = @country)) or
  40. (cl.ConsumingAccountNumber in (select data from @associatedAccountsTable) and (@country is null or ca.Country = @country)))
  41. as Readers
  42.  
  43. -- i believe this is the problem query
  44. select distinct [Batches].ID, [Batches].Notes, [Batches].StartTime, [Batches].CompleteTime, [Batches].ReaderSerialNumber as ReaderSN from TestBatch [Batches]
  45. inner join MastitisTest mt on [Batches].ID = mt.BatchID
  46. inner join Account a on a.AccountNumber = mt.AccountNumber
  47. left join CustomerLocation cl on mt.CustomerLocationID = cl.ID
  48. left join Account ca on ca.AccountNumber = cl.ConsumingAccountNumber
  49. where (a.AccountNumber = @accountId and (@country is null or a.Country = @country)) or
  50. (cl.ConsumingAccountNumber in (select data from @associatedAccountsTable) and (@country is null or ca.Country = @country))
  51.  
  52. select distinct Locations.ID, Locations.Name, Locations.ReaderSerialNumber from CustomerLocation Locations
  53. inner join MastitisTest mt on Locations.ID = mt.CustomerLocationID
  54. inner join Account a on a.AccountNumber = mt.AccountNumber
  55. left join Account ca on ca.AccountNumber = Locations.ConsumingAccountNumber
  56. where Locations.ID <> '00000000-0000-0000-0000-000000000000' and ((a.AccountNumber = @accountId and (@country is null or a.Country = @country)) or
  57. (Locations.ConsumingAccountNumber in (select data from @associatedAccountsTable) and (@country is null or ca.Country = @country)))
  58.  
  59. end
  60. else
  61. begin
  62. -- this else can be ignored
  63. select 1
  64. end
  65. END
  66.  
  67.  
  68. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement