Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AdamWest
Constraint Violating Yak Guru
360 Posts |
Posted - 2011-07-24 : 11:51:21
|
| I have to make a change to this sp. Instead of passing all rows on the department level, I have codes a new table "account.UserDepartments'UserID (PK,FK, int, not null)DepartmentID (PK, FK, int, not null)CustomerID (PK, FK, int, not null)And we want to include the rows by user department, for examplesay the customer has 4 departments> Medical, Dietary, Housekeeping, and Nutrition. THe user has in that table a row for Medical and Nutrition, then the SP should return only those rows for Medical and nutrition departments.but the only caveat is, if the customer has only ONE department, not 4 for example, then we do want to process the 1 department. as it would be very tedious to enter rights to a one department customer.Here you have the sp, On our DB, the highest level is department, then category, then item. I have to integrate this new table to this sp: while there are other sp's i think this is the main one, getting the transactions rows in the first place.USE [SpendDashb]GO/****** Object: StoredProcedure [dbo].[GetTransactions] Script Date: 07/24/2011 11:48:44 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- drop procedure GetTransactionsALTER PROCEDURE [dbo].[GetTransactions] @CustomerId INT = NULL AS BEGIN INSERT INTO App_Log(Info, Start, EndDate)VALUES('GetTransactions', getDate(), null )Declare @logId intSET @logId = @@IDENTITYSET NOCOUNT ON; SELECT LTRIM(RTRIM(ISNULL(d.DepartmentName, ''))) AS DepartmentName ,d.DepartmentId AS DepartmentId ,ISNULL(t.Cost, 0) AS Expense ,ISNULL(t.Quantity , 0)AS Quantity ,LTRIM(RTRIM(ISNULL(c.CustomerName, ''))) AS CUSTNAME ,LTRIM(RTRIM(ISNULL(i.ItemName, ''))) AS Description ,t.DateCreated AS DateCreated ,LTRIM(RTRIM(ISNULL(ct.CategoryLongName, ''))) AS CategoryName ,LTRIM(RTRIM(ISNULL(ct.CategoryName, ''))) AS USCATVAL --,'00000' AS Budget ,LTRIM(RTRIM(ISNULL(t.InvoiceNumber,''))) as InvoiceNumFROM Transactions tLEFT JOIN Departments d ON t.DepartmentId = d.DepartmentIdLEFT JOIN Customers c ON t.CustomerId = c.CustomerIdLEFT JOIN Categories ct ON t.CategoryId = ct.CategoryIdLEFT JOIN Items i ON t.ItemId = i.ItemIdWHERE DateCreated >= DATEADD(YYYY, - 2, GETDATE()) AND DateCreated <= GETDATE()AND t.CustomerId = @CustomerIdORDER BY DateCreated DESC UPDATE App_LogSET EndDate = GETDATE()where ID = @logIdEND |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-24 : 12:51:40
|
quote: Originally posted by AdamWest I have to make a change to this sp. Instead of passing all rows on the department level, I have codes a new table "account.UserDepartments'UserID (PK,FK, int, not null)DepartmentID (PK, FK, int, not null)CustomerID (PK, FK, int, not null)And we want to include the rows by user department, for examplesay the customer has 4 departments> Medical, Dietary, Housekeeping, and Nutrition. THe user has in that table a row for Medical and Nutrition, then the SP should return only those rows for Medical and nutrition departments.but the only caveat is, if the customer has only ONE department, not 4 for example, then we do want to process the 1 department. as it would be very tedious to enter rights to a one department customer.Here you have the sp, On our DB, the highest level is department, then category, then item. I have to integrate this new table to this sp: while there are other sp's i think this is the main one, getting the transactions rows in the first place.USE [SpendDashb]GO/****** Object: StoredProcedure [dbo].[GetTransactions] Script Date: 07/24/2011 11:48:44 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- drop procedure GetTransactionsALTER PROCEDURE [dbo].[GetTransactions] @CustomerId INT = NULL AS BEGIN INSERT INTO App_Log(Info, Start, EndDate)VALUES('GetTransactions', getDate(), null )Declare @logId intSET @logId = @@IDENTITYSET NOCOUNT ON; SELECT LTRIM(RTRIM(ISNULL(d.DepartmentName, ''))) AS DepartmentName ,d.DepartmentId AS DepartmentId ,ISNULL(t.Cost, 0) AS Expense ,ISNULL(t.Quantity , 0)AS Quantity ,LTRIM(RTRIM(ISNULL(c.CustomerName, ''))) AS CUSTNAME ,LTRIM(RTRIM(ISNULL(i.ItemName, ''))) AS Description ,t.DateCreated AS DateCreated ,LTRIM(RTRIM(ISNULL(ct.CategoryLongName, ''))) AS CategoryName ,LTRIM(RTRIM(ISNULL(ct.CategoryName, ''))) AS USCATVAL --,'00000' AS Budget ,LTRIM(RTRIM(ISNULL(t.InvoiceNumber,''))) as InvoiceNumFROM Transactions tLEFT JOIN Departments d ON t.DepartmentId = d.DepartmentIdLEFT JOIN Customers c ON t.CustomerId = c.CustomerIdLEFT JOIN Categories ct ON t.CategoryId = ct.CategoryIdLEFT JOIN account.UserDepartments ud ON ud.DepartmentID = d.DepartmentId AND ud.CustomerId = c.CustomerIdLEFT JOIN Items i ON t.ItemId = i.ItemIdWHERE DateCreated >= DATEADD(YYYY, - 2, GETDATE()) AND DateCreated <= GETDATE()AND t.CustomerId = @CustomerIdORDER BY DateCreated DESC UPDATE App_LogSET EndDate = GETDATE()where ID = @logIdEND
Try the edited version above.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
AdamWest
Constraint Violating Yak Guru
360 Posts |
Posted - 2011-07-24 : 14:39:46
|
| OK this is good,but one thing, I need to code that if the customeronly had one department the we want to not check the user-department table. because one department occurs for many of the customers. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-25 : 00:39:38
|
maybe thisUSE [SpendDashb]GO/****** Object: StoredProcedure [dbo].[GetTransactions] Script Date: 07/24/2011 11:48:44 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- drop procedure GetTransactionsALTER PROCEDURE [dbo].[GetTransactions] @CustomerId INT = NULL AS BEGIN INSERT INTO App_Log(Info, Start, EndDate)VALUES('GetTransactions', getDate(), null )Declare @logId intSET @logId = @@IDENTITYSET NOCOUNT ON; SELECT LTRIM(RTRIM(ISNULL(d.DepartmentName, ''))) AS DepartmentName,d.DepartmentId AS DepartmentId,ISNULL(t.Cost, 0) AS Expense,ISNULL(t.Quantity , 0)AS Quantity,LTRIM(RTRIM(ISNULL(c.CustomerName, ''))) AS CUSTNAME,LTRIM(RTRIM(ISNULL(i.ItemName, ''))) AS Description,t.DateCreated AS DateCreated,LTRIM(RTRIM(ISNULL(ct.CategoryLongName, ''))) AS CategoryName,LTRIM(RTRIM(ISNULL(ct.CategoryName, ''))) AS USCATVAL--,'00000' AS Budget,LTRIM(RTRIM(ISNULL(t.InvoiceNumber,''))) as InvoiceNumFROM Transactions tLEFT JOIN Departments d ON t.DepartmentId = d.DepartmentIdLEFT JOIN Customers c ON t.CustomerId = c.CustomerIdLEFT JOIN Categories ct ON t.CategoryId = ct.CategoryIdLEFT JOIN account.UserDepartments ud ON ud.DepartmentID = d.DepartmentId AND ud.CustomerId = c.CustomerIdLEFT JOIN (SELECT UserID,COUNT(DepartmentID) AS Dept_Cnt FROM account.UserDepartments GROUP BY UserID)ud1 ON ud1.UserID=ud.UserIDLEFT JOIN Items i ON t.ItemId = i.ItemIdWHERE DateCreated >= DATEADD(YYYY, - 2, GETDATE()) AND DateCreated <= GETDATE()AND t.CustomerId = @CustomerIdAND (ud1.Dept_Cnt <=1 OR ud.UserID IS NOT NULL)ORDER BY DateCreated DESC UPDATE App_LogSET EndDate = GETDATE()where ID = @logIdEND ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|