SQL geting Top X number of rows - sql

i want to get only the top x number of the query. where the x will be send by parameter how can i do this?
I know i can take it as string and exec it later but i find it very cumbersome.
This the query i have and #ArticleNo is the parameter i want to take as x.
Create proc [dbo].[GW2_Report_SlowFastMovingArticle]
#ArtKey bigint = null,
#ArtCatKey int= null,
#ArtGroupKey int= null,
#ArtTypeKey int= null,
#MaterialKey int= null,
#ColorKey int= null,
#VendorTypeKey int = null,
#VendorKey bigint = null,
#FromDate datetime = null,
#ToDate datetime = null,
#MovingType int = 0,
#PerformanceType int = 0,
#ArticleNo int = 10,
#OutletKey int = null
AS
BEGIN
SELECT
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode,
dbo.ArtWithVendor.ArtName,
Sum(isnull(dbo.Sal_POSDet.Qty,0))as Pair,
Sum(isnull(dbo.Sal_POSDet.Total,0)) as TurnOver
into #temp FROM
dbo.Sal_POS INNER JOIN
dbo.Sal_POSDet ON
dbo.Sal_POS.SalesKey = dbo.Sal_POSDet.SalesKey INNER JOIN
dbo.ArtWithVendor ON
dbo.Sal_POSDet.ArtKey = dbo.ArtWithVendor.ArtKey
WHERE
Sal_POS.IsHold=0 and
Sal_POS.SalesDate between #FromDate and #ToDate and
CASE WHEN #ArtKey is null THEN 1 WHEN ArtWithVendor.ArtKey =#ArtKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtCatKey is null THEN 1 WHEN ArtWithVendor.ArtCatKey =#ArtCatKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtGroupKey is null THEN 1 WHEN ArtWithVendor.ArtGroupKey = #ArtGroupKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtTypeKey is null THEN 1 WHEN ArtWithVendor.ArtTypeKey = #ArtTypeKey THEN 1 ELSE 0 END = 1
and CASE WHEN #MaterialKey is null THEN 1 WHEN ArtWithVendor.MaterialKey = #MaterialKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ColorKey is null THEN 1 WHEN ArtWithVendor.ColorKey = #ColorKey THEN 1 ELSE 0 END = 1
and CASE WHEN #VendorKey is null THEN 1 WHEN ArtWithVendor.VendorKey = #VendorKey THEN 1 ELSE 0 END = 1
and CASE WHEN #VendorTypeKey is null THEN 1 WHEN ArtWithVendor.VendorTypeKey = #VendorTypeKey THEN 1 ELSE 0 END = 1
and CASE WHEN #OutLetKey is null THEN 1 WHEN Sal_POS.OutLetKey = #OutLetKey THEN 1 ELSE 0 END = 1
Group by
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode,
dbo.ArtWithVendor.ArtName
if(#PerformanceType=0 and #MovingType=0)
begin
select * from #temp
order by Pair asc
end
if(#PerformanceType=0 and #MovingType=1)
begin
select * from #temp
order by Pair desc
end
if(#PerformanceType=1 and #MovingType=0)
begin
select * from #temp
order by turnover asc
end
if(#PerformanceType=1 and #MovingType=1)
begin
select * from #temp
order by turnover desc
end
END

Try this...
select TOP(#ArticleNo) *
from #temp

Use:
SELECT TOP(#ArticleNo)
Therefore:
SELECT TOP(#ArticleNo)
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode,
dbo.ArtWithVendor.ArtName,
Sum(isnull(dbo.Sal_POSDet.Qty,0))as Pair,
Sum(isnull(dbo.Sal_POSDet.Total,0)) as TurnOver
into #temp FROM
dbo.Sal_POS INNER JOIN
dbo.Sal_POSDet ON
dbo.Sal_POS.SalesKey = dbo.Sal_POSDet.SalesKey INNER JOIN
dbo.ArtWithVendor ON
dbo.Sal_POSDet.ArtKey = dbo.ArtWithVendor.ArtKey
WHERE
Sal_POS.IsHold=0 and
Sal_POS.SalesDate between #FromDate and #ToDate and
CASE WHEN #ArtKey is null THEN 1 WHEN ArtWithVendor.ArtKey =#ArtKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtCatKey is null THEN 1 WHEN ArtWithVendor.ArtCatKey =#ArtCatKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtGroupKey is null THEN 1 WHEN ArtWithVendor.ArtGroupKey = #ArtGroupKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtTypeKey is null THEN 1 WHEN ArtWithVendor.ArtTypeKey = #ArtTypeKey THEN 1 ELSE 0 END = 1
and CASE WHEN #MaterialKey is null THEN 1 WHEN ArtWithVendor.MaterialKey = #MaterialKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ColorKey is null THEN 1 WHEN ArtWithVendor.ColorKey = #ColorKey THEN 1 ELSE 0 END = 1
and CASE WHEN #VendorKey is null THEN 1 WHEN ArtWithVendor.VendorKey = #VendorKey THEN 1 ELSE 0 END = 1
and CASE WHEN #VendorTypeKey is null THEN 1 WHEN ArtWithVendor.VendorTypeKey = #VendorTypeKey THEN 1 ELSE 0 END = 1
and CASE WHEN #OutLetKey is null THEN 1 WHEN Sal_POS.OutLetKey = #OutLetKey THEN 1 ELSE 0 END = 1
Group by
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode,
dbo.ArtWithVendor.ArtName
Alternatively, add the following before your SELECT query:
IF #ArticleNo IS NOT NULL
BEGIN
SET ROWCOUNT #ArticleNo
END
Then after your SELECT query you need to reset the ROWCOUNT by doing:
IF #ArticleNo IS NOT NULL
BEGIN
SET ROWCOUNT 0
END
Therefore, overall it will be something like:
IF #ArticleNo IS NOT NULL
BEGIN
SET ROWCOUNT #ArticleNo
END
SELECT
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode,
dbo.ArtWithVendor.ArtName,
Sum(isnull(dbo.Sal_POSDet.Qty,0))as Pair,
Sum(isnull(dbo.Sal_POSDet.Total,0)) as TurnOver
into #temp FROM
dbo.Sal_POS INNER JOIN
dbo.Sal_POSDet ON
dbo.Sal_POS.SalesKey = dbo.Sal_POSDet.SalesKey INNER JOIN
dbo.ArtWithVendor ON
dbo.Sal_POSDet.ArtKey = dbo.ArtWithVendor.ArtKey
WHERE
Sal_POS.IsHold=0 and
Sal_POS.SalesDate between #FromDate and #ToDate and
CASE WHEN #ArtKey is null THEN 1 WHEN ArtWithVendor.ArtKey =#ArtKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtCatKey is null THEN 1 WHEN ArtWithVendor.ArtCatKey =#ArtCatKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtGroupKey is null THEN 1 WHEN ArtWithVendor.ArtGroupKey = #ArtGroupKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtTypeKey is null THEN 1 WHEN ArtWithVendor.ArtTypeKey = #ArtTypeKey THEN 1 ELSE 0 END = 1
and CASE WHEN #MaterialKey is null THEN 1 WHEN ArtWithVendor.MaterialKey = #MaterialKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ColorKey is null THEN 1 WHEN ArtWithVendor.ColorKey = #ColorKey THEN 1 ELSE 0 END = 1
and CASE WHEN #VendorKey is null THEN 1 WHEN ArtWithVendor.VendorKey = #VendorKey THEN 1 ELSE 0 END = 1
and CASE WHEN #VendorTypeKey is null THEN 1 WHEN ArtWithVendor.VendorTypeKey = #VendorTypeKey THEN 1 ELSE 0 END = 1
and CASE WHEN #OutLetKey is null THEN 1 WHEN Sal_POS.OutLetKey = #OutLetKey THEN 1 ELSE 0 END = 1
Group by
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode,
dbo.ArtWithVendor.ArtName
IF #ArticleNo IS NOT NULL
BEGIN
SET ROWCOUNT 0
END
However using ROWCOUNT is not ideal as it will mess with your sub-query results.

Related

Not able to search the data in stored procedure

I need to search by Suburb however it does not return anything even though there's a data. Here is the stored procedure implementation and I have added it in the where statement. Please help me what cause the issue. Thank you.
WHERE
((#DriverId = '00000000-0000-0000-0000-000000000000') OR
(#DriverId IS NULL AND tl.T_DriverId IS NULL) OR
(#DriverId IS NOT NULL AND tl.T_DriverId = #DriverId)) AND
((#SearchBySuburb IS NOT NULL AND #Suburb = 0 AND pa.A_Suburb = #SearchBySuburb) AND
(#SearchBySuburb IS NOT NULL AND #Suburb = 1 AND da.A_Suburb = #SearchBySuburb)) AND
((#HideCompleted = 1 AND #StatusId = 0 AND tl.T_Status != 8) OR
(#HideCompleted = 1 AND #StatusId = 0 AND tl.T_Status IS NULL) OR
(#HideCompleted = 1 AND #StatusId > 0 AND tl.T_Status = #StatusId) OR
(#HideCompleted = 0 AND #StatusId = 0) OR
(#HideCompleted = 0 AND #StatusId > 0 AND tl.T_Status = #StatusId)) AND
(#OrganizationId IS NULL OR o.O_ID = #OrganizationId) AND
((#StartDate IS NULL AND tl.T_PlannedDeliveryDate IS NULL) OR (#EndDate IS NULL AND tl.T_PlannedDeliveryDate IS NULL) OR (tl.T_PlannedDeliveryDate BETWEEN CAST(#StartDate AS DATE) AND CAST(#EndDate AS DATE)))
The only time you use #SearchBySuburb is in the following fragment
((#SearchBySuburb IS NOT NULL AND #Suburb = 0 AND pa.A_Suburb = #SearchBySuburb) AND
(#SearchBySuburb IS NOT NULL AND #Suburb = 1 AND da.A_Suburb = #SearchBySuburb))
Given they're all AND, that part cannot ever evaluate as true because you're checking if #Suburb = 0 AND #Suburb = 1.
I suggest being very explicit with brackets and checking your ANDs and ORs.
Update following OP's comment about it still not working
I've re-formatted your WHERE into conceptual groups separated by AND clauses- see below. The groups are broadly about the driver, suburb, status, organisation, and start/end dates.
My suggestion is to
a) Start with none of them (which should therefore show all rows)
b) Add the groups (separated by ANDs) to the WHERE clause one at a time until your desired rows disappear
When your desired rows disappear, it tells you there's a problem with the last added filters to the WHERE clause.
WHERE
(
(#DriverId = '00000000-0000-0000-0000-000000000000')
OR (#DriverId IS NULL AND tl.T_DriverId IS NULL)
OR (#DriverId IS NOT NULL AND tl.T_DriverId = #DriverId)
)
AND
(
(#SearchBySuburb IS NOT NULL AND #Suburb = 0 AND pa.A_Suburb = #SearchBySuburb)
OR (#SearchBySuburb IS NOT NULL AND #Suburb = 1 AND da.A_Suburb = #SearchBySuburb)
)
AND
(
(#HideCompleted = 1 AND #StatusId = 0 AND tl.T_Status != 8)
OR (#HideCompleted = 1 AND #StatusId = 0 AND tl.T_Status IS NULL)
OR (#HideCompleted = 1 AND #StatusId > 0 AND tl.T_Status = #StatusId)
OR (#HideCompleted = 0 AND #StatusId = 0)
OR (#HideCompleted = 0 AND #StatusId > 0 AND tl.T_Status = #StatusId)
)
AND
(#OrganizationId IS NULL OR o.O_ID = #OrganizationId)
AND
(
(#StartDate IS NULL AND tl.T_PlannedDeliveryDate IS NULL)
OR (#EndDate IS NULL AND tl.T_PlannedDeliveryDate IS NULL)
OR (tl.T_PlannedDeliveryDate BETWEEN CAST(#StartDate AS DATE) AND CAST(#EndDate AS DATE))
)

optimize complex stored procedure

I have a very complex stored procedure and i am not that strong in SQL, please any one who has a strong SQL expertise and can help me optimize or at least can give me a hint, the procedure selects the inbox messages from database for specific user and it contains many other parameters such as paging and categories and also language input and many other details:
ALTER PROCEDURE [dbo].[Inbox]
#ConfidentialityIds NVARCHAR(100) = '0,1,2,3',
#UrgencyIds NVARCHAR(100) = '0,1,2,3',
#CorrespondenceCategoryIds NVARCHAR(100) = '1,2,3,5,6,7',
#CorrespondenceExtendedCategoryIds NVARCHAR(MAX),
#IsPrivate bit = 0,
#IsSaved bit = 0,
#Importantance VARCHAR(10) = '0, 1',
#UserId uniqueidentifier,
#SelectedPage int = 1,
#PageSize int = 25,
#Now bigint,
#IsImpersonated bit,
#ActualLoggedInUserId uniqueidentifier,
#IsEnglish bit,
#OnlyArchived bit = null,
#replaceNameWithEntName bit,
#entityIds nvarchar(MAX),
#OnlyLate bit = 0,
#Statuses NVARCHAR(100) ='1,2,3,4,5',
#ApplyPageFilter bit = 1,
#IsBundled int,
#showArchivedInCCInbox bit = 0,
#appModuleId INT = 0
WITH RECOMPILE
As
BEGIN
SET transaction isolation level read uncommitted
DECLARE #lFirstRec INT, #lLastRec INT, #lTotalRows INT
SET #lFirstRec = ( #SelectedPage - 1 ) * #PageSize
SET #lLastRec = ( #SelectedPage * #PageSize + 1 )
SET #lTotalRows = #lFirstRec - #lLastRec + 1
DECLARE #isPersonalDelegationAvailable BIT = 0
IF EXISTS(
SELECT 1
FROM DelegatedToEntityUsers d
WHERE d.DelegatedToUserId = #ActualLoggedInUserId
AND d.DelegatedFromUserId = #UserId
AND
(
d.DelegatedToEntityId IS NULL OR d.DelegationType = 1
))
BEGIN
SET #isPersonalDelegationAvailable = 1
END
DECLARE #tblEntities TABLE(Value INT)
INSERT INTO #tblEntities
SELECT Value FROM SplitCommaUniqueValues(#entityIds)
Select * from (
Select (ROW_NUMBER() OVER (ORDER BY K.ActionDate DESC, K.CorrespondenceDate DESC)) AS RowIndex,
Count(*) over () AS TotalCount,*
from (
select (ROW_NUMBER() OVER (PARTITION BY CorrespondenceID Order by CorrespondenceID DESC)) AS CorrId, *
from (
SELECT Distinct * FROM
(
SELECT
CONVERT(VARCHAR(100), R.CorrespondenceActionRecipientID) Id,
A.CorrespondenceActionID ActionId,
(CASE WHEN R.RecipientType IS NULL THEN NULL ELSE CAST(RecipientType AS INT) END) RecipientType,
A.ActionTypeId,
(CASE when #IsEnglish = 1 then ATypes.FriendlyTextEN else ATypes.FriendlyTextAR end) ActionTypeName,
A.ActionCreatedByUserID SentById,
CASE
when #replaceNameWithEntName = 0 then ISNULL(ActionCreatedByUser.ShortName, ActionCreatedByUser.EmployeeName)
when #replaceNameWithEntName = 1 and SentByEntity.EntityID not in (SELECT Value FROM #tblEntities) then case when #IsEnglish = 1 then SentByEntity.EntityNameEn else SentByEntity.EntityNameAR end
when #replaceNameWithEntName = 1 and SentByEntity.EntityID in (SELECT Value FROM #tblEntities) then ISNULL(ActionCreatedByUser.ShortName, ActionCreatedByUser.EmployeeName)
END SentByFrom,
A.WithDelegateFromUserID OnBehalfOfId,
ISNULL(WithDelegateFromUser.ShortName, WithDelegateFromUser.EmployeeName) OnBehalfOfName,
R.UserId SentToId,
CASE
when #replaceNameWithEntName = 0 then ISNULL(SentToUser.ShortName, SentToUser.EmployeeName)
when #replaceNameWithEntName = 1 and SentToEntity.EntityID not in (SELECT Value FROM #tblEntities) then case when #IsEnglish = 1 then SentToEntity.EntityNameEn else SentToEntity.EntityNameAR end
when #replaceNameWithEntName = 1 and SentToEntity.EntityID in (SELECT Value FROM #tblEntities) then isnull(ISNULL(SentToUser.ShortName, SentToUser.EmployeeName), case when #IsEnglish = 1 then SentToEntity.EntityNameEn else SentToEntity.EntityNameAR end)
END SentToName,
R.EntityID SentToEntityId,
SentToEntity.EntityNameAR SentToEntityNameAR,
SentToEntity.EntityNameEN SentToEntityNameEN,
R.Seen,
(CASE WHEN MR.CorrespondenceActionRecipientID IS NOT NULL THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END) IsReadByUser,
CAST(ISNULL(FollowedByUser.IsFollowedByUser, 0) AS BIT) IsFollowedByUser,
(
CASE WHEN EXISTS(SELECT 1 FROM CorrespondenceFollowups
WHERE CorrespondenceID = C.CorrespondenceID
AND UserId = #UserId AND AssignedByUserId IS NOT NULL) THEN CAST(1 AS BIT)
ELSE CAST(0 AS BIT) END
) IsAssignedFollowToUser,
(
CASE WHEN EXISTS(SELECT 1 FROM CorrespondenceFollowups
WHERE CorrespondenceID = C.CorrespondenceID
AND AssignedByUserId = #UserId) THEN CAST(1 AS BIT)
ELSE CAST(0 AS BIT) END
) IsAssignedFollowByUser,
A.CorrespondenceID,
A.ActionDate,
A.ActionDeadlineDate,
C.CompletionDate,
C.CorrespondenceDeadlineDate CorrDeadlineDate,
C.OwnerUserID CorrOwnerId,
C.ChildNo ChildNo,
OwnerUser.EmployeeName CorrOwnerName,
C.Important,
C.UrgencyId,
C.LetterCorrespondenceId,
(CASE when #IsEnglish = 1 then U.TextEN else U.TextAR end) UrgencyText,
C.ConfidentialityId,
(CASE when #IsEnglish = 1 then Confid.TextEN else Confid.TextAR end) ConfidentialityText,
C.CorrespondenceCategoryId CategoryId,
C.CorrespondenceClassificationID CorrespondenceClassificationID,
(CASE when #IsEnglish = 1 then CCat.TextEN else CCat.TextAR end) CategoryName,
CCat.ColorClass ColorClass,
C.Subject,
c.CorrespondenceFormula,
C.InternalSenderEntityId,
C.InternalRecipientEntityId,
C.[Sequence] [Sequence],
C.Year,
C.CompletionStatus Status,
Null Number,
(CASE WHEN (
(
(A.ActionDeadlineDate IS NOT NULL AND A.ActionDeadlineDate < #Now)
OR
(C.CorrespondenceDeadlineDate IS NOT NULL AND C.CorrespondenceDeadlineDate < #Now)
)
AND
C.[Closed-Archived] = 0) THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END) IsLate,
CAST(0 AS BIT) HasAnyEvents,
C.CreatedByUserId,
CreatedByUser.EmployeeName CreatedByUserName,
CAST(C.CorrespondenceDate AS VARCHAR(50)) CorrDate,
C.CorrespondenceDate,
C.[Closed-Archived] ClosedArchived,
C.IsSaved SavedActionTaken,
((CASE WHEN C.Important = 1 THEN CASE WHEN #IsEnglish = 1 THEN N'Important' ELSE N'هامة' END ELSE '' END) + (CASE WHEN C.Important = 1 AND C.UrgencyID <> 0 THEN CASE WHEN #IsEnglish = 1 THEN N' and ' ELSE N' و ' END ELSE '' END) + (CASE WHEN C.UrgencyID <> 0 THEN (CASE when #IsEnglish = 1 then U.TextEN else U.TextAR end) ELSE '' END)) ImportantUrgencyText,
CAST(C.IsAutoSaved AS BIT) IsAutoSaved,
-- *******************************************************************
rem.ReminderDate as CorrespondenceReminderDateTime,
remAction.ReminderDate as ActionReminderDateTime
FROM (
SELECT * FROM Correspondences CC
WHERE CC.IsDeleted = 0
AND CC.LetterCorrespondenceId IS NULL
AND (CC.ConfidentialityId IN (SELECT [Value] FROM SplitCommaUniqueValues(#ConfidentialityIds)))
AND
( #IsPrivate = 0 OR
(
#IsPrivate = 1 AND CC.IsForPresident = 1
)
)
AND
(
(CC.IsSaved = case when #IsSaved =1 then 1 else CC.IsSaved end)
)
AND (#appModuleId = 0 OR CC.AppModuleId = #appModuleId)
AND (CC.UrgencyID IN (SELECT value FROM SplitCommaUniqueValues(#UrgencyIds)))
AND (CC.Important IN (SELECT value FROM SplitCommaUniqueValues(#Importantance)))
AND CC.CompletionStatus IN(SELECT value FROM SplitComma(#Statuses))
) C
LEFT JOIN CorrespondenceExtendedDetails CorrExt
ON CorrExt.CorrespondenceID = C.CorrespondenceID
LEFT JOIN CorrespondenceActions A
ON C.CorrespondenceID = A.CorrespondenceID
AND A.IsDeleted = 0
AND A.ActionTypeID != 7 -- إنهاء التذكير
LEFT JOIN CorrespondenceActionRecipients R
ON A.CorrespondenceActionID = R.CorrespondenceActionID
AND (
R.AsCopy = 1
)
JOIN Urgencies U
ON C.UrgencyID = U.ID
JOIN ActionTypes ATypes
ON A.ActionTypeID = ATypes.ID
JOIN CorrespondenceCategories CCat
ON C.CorrespondenceCategoryId = CCat.ID
JOIN Confidentialities Confid
ON C.ConfidentialityID = Confid.ID
LEFT JOIN Users ActionCreatedByUser
ON A.ActionCreatedByUserID = ActionCreatedByUser.UserId
LEFT JOIN Users WithDelegateFromUser
ON A.WithDelegateFromUserID = WithDelegateFromUser.UserID
LEFT JOIN Entities SentByEntity
ON A.ActionCreatedByUserEntityId = SentByEntity.EntityID
LEFT JOIN Users SentToUser
ON R.UserId = SentToUser.UserId
LEFT JOIN Entities SentToEntity
ON R.EntityID = SentToEntity.EntityID
LEFT JOIN Users OwnerUser
ON C.OwnerUserID = OwnerUser.UserId
LEFT JOIN Users CreatedByUser
ON C.CreatedByUserID = CreatedByUser.UserId
LEFT JOIN MarkAsReads MR
ON R.CorrespondenceActionRecipientID = MR.CorrespondenceActionRecipientID
AND MR.UserId = #UserId
OUTER APPLY
(
SELECT TOP 1 1 IsFollowedByUser
FROM CorrespondenceFollowups CF
WHERE CF.CorrespondenceId = C.CorrespondenceID
AND CF.UserId = #UserId
AND CF.AssignedByUserId IS NULL
) FollowedByUser
-- *******************************************************************
LEFT JOIN LinkedCorrespondenceSchedules CS on CS.CorrespondenceID = C.CorrespondenceID
LEFT JOIN Reminder rem on rem.ReferenceId=C.CorrespondenceID AND rem.ReferenceType = 1 AND rem.CreatedByUserId=#UserId AND (rem.[Status] = 1 OR rem.[Status] = 2)
Left JOIN Reminder remAction on remAction.ReferenceId = A.CorrespondenceActionID AND remAction.ReferenceType = 2 AND remAction.CreatedByUserId=#UserId AND (remAction.[Status] = 1 OR remAction.[Status] = 2)
-- *******************************************************************
WHERE (
(C.CorrespondenceCategoryId IN (SELECT Value FROM SplitComma(#CorrespondenceCategoryIds)) AND CorrExt.CorrespondenceExtendedCategoryId IS NULL)
OR (CorrExt.CorrespondenceExtendedCategoryId IN (SELECT Value FROM SplitComma(#CorrespondenceExtendedCategoryIds)))
)
AND
(
-- Personal
(
(
(#IsImpersonated = 0 AND R.UserId = #UserId)
OR
(#IsImpersonated = 1 AND R.UserId = #UserId AND #isPersonalDelegationAvailable = 1)
)
AND R.RecipientType = 1 AND R.UserId Is Not null
)
OR
-- Me as Manager (Impersonation mode handled from code by sending the proper Entity Ids.
(
(R.EntityID IN (SELECT Value FROM #tblEntities) AND R.EntityID Is Not null AND R.RecipientType != 1)
)
)
AND C.[Closed-Archived] = Case When #showArchivedInCCInbox = 1 Then 0 Else C.[Closed-Archived] End
AND R.UserTokeAction = 0
AND NOT EXISTS (
SELECT 1 FROM Correspondences CC
INNER JOIN CorrespondenceLinks CL
ON CC.CorrespondenceID = CL.LinkedCorrespondenceID AND CC.CorrespondenceID = C.CorrespondenceID AND CL.LinkTypeID = #IsBundled
)
) T
WHERE T.LetterCorrespondenceId is null
) as ttt
) K where CorrId = 1
)N where (#ApplyPageFilter = 0 OR ( RowIndex > #lFirstRec AND RowIndex < #lLastRec))
Order by
ActionDate DESC, CorrespondenceDate
option(recompile)
END

SQL Stuff For XML Path with Multiple Fields

I am using SQL Server 2008. I am attempting to write a select statement using STUFF and FOR XML PATH to create a comma separated string in one field. There is only ONE record but 31 fields.
I'm pulling the data from 1 table which has a smallint field for each day of the month - 0 means no for that day, 1 means yes. (I did not design this table and unfortunately I am not allowed to change it.) Like so:
z1st z2nd z3rd z4th z5th z6th z7th z8th z9th z10th
------ ------ ------ ------ ------ ------ ------ ------ ------ ------
0 1 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 1
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 1
And so on, all the way through z31st.
My statement is passing integer variables #order_snbr and #admin_nbr, which will always return a single row.
DECLARE #strSelectedDayList AS VARCHAR(MAX)
SELECT #strSelectedDayList = STUFF((SELECT ', ' + CASE WHEN td1.z1st = 1 THEN '1st' END
, CASE WHEN td1.z2nd = 1 THEN '2nd' END
, CASE WHEN td1.z3rd = 1 THEN '3rd' END
, CASE WHEN td1.z4th = 1 THEN '4th' END
, CASE WHEN td1.z5th = 1 THEN '5th' END
, CASE WHEN td1.z6th = 1 THEN '6th' END
, CASE WHEN td1.z7th = 1 THEN '7th' END
, CASE WHEN td1.z8th = 1 THEN '8th' END
, CASE WHEN td1.z9th = 1 THEN '9th' END
, CASE WHEN td1.z10th = 1 THEN '10th' END
, CASE WHEN td1.z11th = 1 THEN '11th' END
, CASE WHEN td1.z12th = 1 THEN '12th' END
, CASE WHEN td1.z13th = 1 THEN '13th' END
, CASE WHEN td1.z14th = 1 THEN '14th' END
, CASE WHEN td1.z15th = 1 THEN '15th' END
, CASE WHEN td1.z16th = 1 THEN '16th' END
, CASE WHEN td1.z17th = 1 THEN '17th' END
, CASE WHEN td1.z18th = 1 THEN '18th' END
, CASE WHEN td1.z19th = 1 THEN '19th' END
, CASE WHEN td1.z20th = 1 THEN '20th' END
, CASE WHEN td1.z21st = 1 THEN '21st' END
, CASE WHEN td1.z22nd = 1 THEN '22nd' END
, CASE WHEN td1.z23rd = 1 THEN '23rd' END
, CASE WHEN td1.z24th = 1 THEN '24th' END
, CASE WHEN td1.z25th = 1 THEN '25th' END
, CASE WHEN td1.z26th = 1 THEN '26th' END
, CASE WHEN td1.z27th = 1 THEN '27th' END
, CASE WHEN td1.z28th = 1 THEN '28th' END
, CASE WHEN td1.z29th = 1 THEN '29th' END
, CASE WHEN td1.z30th = 1 THEN '30th' END
, CASE WHEN td1.z31st = 1 THEN '31st' END
FROM TableDays td1 WITH(NOLOCK)
WHERE td1.order_snbr = td2.order_snbr
AND td1.admin_nbr = td2.admin_nbr
FOR XML PATH('')), 1, 1, '')
FROM TableDays td2 WITH(NOLOCK)
WHERE td2.order_snbr = #order_snbr
AND td2.admin_nbr = #admin_nbr
PRINT #strSelectedDayList
This produces a result like so:
1st14th28th
What I would like is this:
1st, 14th, 28th
Any help would be much appreciated! And if there's a better way to do this (without STUFF or FOR XML PATH) I'm happy to switch.
Perhaps a another approach
Example
Declare #YourTable Table ([z1st] varchar(50),[z2nd] varchar(50),[z3rd] varchar(50),[z4th] varchar(50),[z5th] varchar(50),[z6th] varchar(50),[z7th] varchar(50),[z8th] varchar(50),[z9th] varchar(50))
Insert Into #YourTable Values
(0,1,0,0,0,1,0,0,0)
,(0,0,0,0,1,0,0,1,0)
,(0,0,1,0,0,0,0,0,0)
,(1,1,1,1,0,0,0,0,0)
,(0,1,0,0,0,0,1,0,1)
Select A.*
,C.*
From #YourTable A
Cross Apply (Select XMLData = cast((select A.* for XML Raw) as xml) ) B
Cross Apply (
Select S = Stuff((Select ', ' +replace(Item,'z','')
From (
Select Item = attr.value('local-name(.)','varchar(100)')
From B.XMLData.nodes('/row') as A(r)
Cross Apply A.r.nodes('./#*') AS B(attr)
Where attr.value('local-name(.)','varchar(100)') like 'z[0-9]%'
and attr.value('.','varchar(max)') =1
) C1
For XML Path ('')),1,2,'')
) C
Returns

Matching multiple conditions in T-SQL stored procedure

I'm forced to work with a table with the following structure (ScheduledEmails) and am looking to find the most efficient way to deal with multiple variables being set to 1 (true)
The script below will only return the correct value if only one is selected.
Could anybody recommend a way to modify the bottom query to accomodate multiple conditions.
e.g. where #IsMonday and #IsTuesday is true etc.?
I have tried multiple combinations of AND/OR but none seem to yield the desired result.
DECLARE #IsMonday bit
DECLARE #IsTuesday bit
DECLARE #IsWednesday bit
DECLARE #IsThursday bit
DECLARE #IsFriday bit
DECLARE #IsSaturday bit
DECLARE #IsSunday bit
DECLARE #Is0000 bit
DECLARE #Is0100 bit
DECLARE #Is0200 bit
DECLARE #Is0300 bit
DECLARE #Is0400 bit
DECLARE #Is0500 bit
DECLARE #Is0600 bit
DECLARE #Is0700 bit
DECLARE #Is0800 bit
DECLARE #Is0900 bit
DECLARE #Is1000 bit
DECLARE #Is1100 bit
DECLARE #Is1200 bit
DECLARE #Is1300 bit
DECLARE #Is1400 bit
DECLARE #Is1500 bit
DECLARE #Is1600 bit
DECLARE #Is1700 bit
DECLARE #Is1800 bit
DECLARE #Is1900 bit
DECLARE #Is2000 bit
DECLARE #Is2100 bit
DECLARE #Is2200 bit
DECLARE #Is2300 bit
DECLARE #dayOfWeek VARCHAR(20)
DECLARE #hourOfDay int
SET #dayOfWeek = datename(dw,GETDATE()) -- Monday to Sunday
SET #hourOfDay = datepart(hh, GETDATE()) -- Single digit 0 - 23
SET #IsMonday = CASE WHEN #dayOfWeek = 'Monday' THEN 1 ELSE 0 END
SET #IsTuesday = CASE WHEN #dayOfWeek = 'Tuesday' THEN 1 ELSE 0 END
SET #IsWednesday = CASE WHEN #dayOfWeek = 'Wednesday' THEN 1 ELSE 0 END
SET #IsThursday = CASE WHEN #dayOfWeek = 'Thursday' THEN 1 ELSE 0 END
SET #IsFriday = CASE WHEN #dayOfWeek = 'Friday' THEN 1 ELSE 0 END
SET #IsSaturday = CASE WHEN #dayOfWeek = 'Saturday' THEN 1 ELSE 0 END
SET #IsSunday = CASE WHEN #dayOfWeek = 'Sunday' THEN 1 ELSE 0 END
SET #Is0000 = CASE WHEN #hourOfDay = 0 THEN 1 ELSE 0 END
SET #Is0100 = CASE WHEN #hourOfDay = 1 THEN 1 ELSE 0 END
SET #Is0200 = CASE WHEN #hourOfDay = 2 THEN 1 ELSE 0 END
SET #Is0300 = CASE WHEN #hourOfDay = 3 THEN 1 ELSE 0 END
SET #Is0400 = CASE WHEN #hourOfDay = 4 THEN 1 ELSE 0 END
SET #Is0500 = CASE WHEN #hourOfDay = 5 THEN 1 ELSE 0 END
SET #Is0600 = CASE WHEN #hourOfDay = 6 THEN 1 ELSE 0 END
SET #Is0700 = CASE WHEN #hourOfDay = 7 THEN 1 ELSE 0 END
SET #Is0800 = CASE WHEN #hourOfDay = 8 THEN 1 ELSE 0 END
SET #Is0900 = CASE WHEN #hourOfDay = 9 THEN 1 ELSE 0 END
SET #Is1000 = CASE WHEN #hourOfDay = 10 THEN 1 ELSE 0 END
SET #Is1100 = CASE WHEN #hourOfDay = 11 THEN 1 ELSE 0 END
SET #Is1200 = CASE WHEN #hourOfDay = 12 THEN 1 ELSE 0 END
SET #Is1300 = CASE WHEN #hourOfDay = 13 THEN 1 ELSE 0 END
SET #Is1400 = CASE WHEN #hourOfDay = 14 THEN 1 ELSE 0 END
SET #Is1500 = CASE WHEN #hourOfDay = 15 THEN 1 ELSE 0 END
SET #Is1600 = CASE WHEN #hourOfDay = 16 THEN 1 ELSE 0 END
SET #Is1700 = CASE WHEN #hourOfDay = 17 THEN 1 ELSE 0 END
SET #Is1800 = CASE WHEN #hourOfDay = 18 THEN 1 ELSE 0 END
SET #Is1900 = CASE WHEN #hourOfDay = 19 THEN 1 ELSE 0 END
SET #Is2000 = CASE WHEN #hourOfDay = 20 THEN 1 ELSE 0 END
SET #Is2100 = CASE WHEN #hourOfDay = 21 THEN 1 ELSE 0 END
SET #Is2200 = CASE WHEN #hourOfDay = 22 THEN 1 ELSE 0 END
SET #Is2300 = CASE WHEN #hourOfDay = 23 THEN 1 ELSE 0 END
INSERT INTO ScheduledEmailQueue (ScheduledEmailId, Created)
SELECT Id, GETDATE() FROM ScheduledEmails WHERE
(SendMonday = #IsMonday AND
SendTuesday = #IsTuesday AND
SendWednesday = #IsWednesday AND
SendThursday= #IsThursday AND
SendFriday = #IsFriday AND
SendSaturday = #IsSaturday AND
SendSunday = #IsSunday AND
Send0000 = #Is0000 AND
Send0100 = #Is0100 AND
Send0200 = #Is0200 AND
Send0300 = #Is0300 AND
Send0400 = #Is0400 AND
Send0500 = #Is0500 AND
Send0600 = #Is0600 AND
Send0700 = #Is0700 AND
Send0800 = #Is0800 AND
Send0900 = #Is0900 AND
Send1000 = #Is1000 AND
Send1100 = #Is1100 AND
Send1200 = #Is1200 AND
Send1300 = #Is1300 AND
Send1400 = #Is1400 AND
Send1500 = #Is1500 AND
Send1600 = #Is1600 AND
Send1700 = #Is1700 AND
Send1800 = #Is1800 AND
Send1900 = #Is1900 AND
Send2000 = #Is2000 AND
Send2100 = #Is2100 AND
Send2200 = #Is2200 AND
Send2300 = #Is2300)
SELECT Id, GETDATE() FROM ScheduledEmails WHERE
SendMonday= #IsMonday AND
SendTuesday = #IsTuesday AND
SendWednesday = #IsWednesday AND
SendThursday= #IsThursday AND
SendFriday = #IsFriday AND
SendSaturday = #IsSaturday AND
SendSunday = #IsSunday AND
Send0000 = #Is0000 AND
Send0100 = #Is0100 AND
Send0200 = #Is0200 AND
Send0300 = #Is0300 AND
Send0400 = #Is0400 AND
Send0500 = #Is0500 AND
Send0600 = #Is0600 AND
Send0700 = #Is0700 AND
Send0800 = #Is0800 AND
Send0900 = #Is0900 AND
Send1000 = #Is1000 AND
Send1100 = #Is1100 AND
Send1200 = #Is1200 AND
Send1300 = #Is1300 AND
Send1400 = #Is1400 AND
Send1500 = #Is1500 AND
Send1600 = #Is1600 AND
Send1700 = #Is1700 AND
Send1800 = #Is1800 AND
Send1900 = #Is1900 AND
Send2000 = #Is2000 AND
Send2100 = #Is2100 AND
Send2200 = #Is2200 AND
Send2300 = #Is2300
If I have understood your requirement correctly, you just want to select the records for current day of week and hour from ScheduledEmails table, and insert them into ScheduledEmailQueue table. If that is indeed the case, you can create a dynamic sql query and execute it. If I have understood your requirement incorrectly, then I apologise in advance :)
DECLARE #dayOfWeek VARCHAR(20)
DECLARE #hourOfDay INT
DECLARE #hourOfDayColumnName VARCHAR(20)
DECLARE #dayOfWeekColumnName VARCHAR(20)
DECLARE #sql VARCHAR(MAX)
SET #dayOfWeek = datename(dw,GETDATE()) -- Monday to Sunday
SET #hourOfDay = datepart(hh, GETDATE()) -- Single digit 0 - 23
SET #hourOfDayColumnName = 'Send' + CASE WHEN LEN(#hourOfDay) = 1 THEN '0' + CONVERT(VARCHAR, #hourOfDay) ELSE CONVERT(VARCHAR, #hourOfDay) END + '00'
SET #dayOfWeekColumnName = 'Send' + #dayOfWeek
SET #sql = 'INSERT INTO ScheduledEmailQueue (ScheduledEmailId, Created)
SELECT Id, GETDATE() FROM ScheduledEmails WHERE ' + #dayOfWeekColumnName + ' = 1 AND ' + #hourOfDayColumnName + ' = 1'
PRINT #hourOfDayColumnName
PRINT #dayOfWeekColumnName
PRINT #sql
EXEC (#sql)
Sorry, I misread your question, but I am still not clear about your requirements. If you use DayOfWeek and Hour, they will always return you singular values (e.g. Monday and 22). If you want to select values based on multiple criteria, then you will have to do a simple hardcoded query like:
SELECT Id, GETDATE() FROM ScheduledEmails WHERE
SendMonday= 1 AND
SendTuesday = 1 AND
Send0000 = 1 AND
Send0100 = 1
If you do not know the values for required days and hours beforehand, then you can just pass them as parameters to a stored proc and use them for filtering.

Using a Case statement within the values section of an Insert statement

Please forgive my ignorance and poor SQL programming skills but I am normally a basic SQL developer.
I need to create a trigger off the insertion of data in one table to insert different data into another table.
Within this trigger I need to insert certain data into the new table based upon values within the newly inserted data from the original table. I am totally confused on this. i thought I would be creative and use a case statement within teh Values section but it is not working.
Can anyone please help me on this? (below is the code for the trigger that I have as of now)
INSERT INTO dbo.WebOnlineUserPeopleDashboard
(
ONLINE_USERACCOUNT_ID,
ONLINE_ROOMS_DIRECTORY,
ONLINE_ROOMS_LIST,
ONLINE_ROOMS_PLACEMENT,
ONLINE_ROOMS_MANAGEMENT,
ONLINE_MAILINGLIST_DIRECTORY,
ONLINE_MAILINGLIST_LIST,
ONLINE_MAILINGLIST_MEMBERS,
ONLINE_MAILINGLIST_MANAGER,
ONLINE_PEOPLESEARCH_DIRECTORY
)
VALUES
IF (SELECT ONLINE_PEOPLE_FULL_ACCESS FROM INSERTED) = 1
BEGIN
SELECT
ONLINE_USERACCOUNT_ID,
1,
1,
1,
1,
1,
1,
1,
1,
1
FROM INSERTED
END
ELSE IF (SELECT ONLINE_PEOPLE_FULL_ACCESS FROM INSERTED) = 0
BEGIN
SELECT
ONLINE_USERACCOUNT_ID,
0,
0,
0,
0,
0,
0,
0,
0,
0
FROM INSERTED
END
ELSE
BEGIN
SELECT
ONLINE_USERACCOUNT_ID,
CASE --DIRECTORY
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_FULL_ACCESS = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_FULL_ACCESS = 0
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 1
THEN 1
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 0
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 0 AND ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 0 AND ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 0
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_ROOMS_MANAGEMENT_FULL_ACCESS = 1
THEN 1
WHEN ONLINE_PEOPLE_ROOMS_MANAGEMENT_FULL_ACCESS = 0
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_MAILING_LISTS_FULL_ACCESS = 1 OR ONLINE_PEOPLE_MAILING_LISTS_VIEW = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_MAILING_LISTS_FULL_ACCESS = 0
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_MAILING_LISTS_VIEW = 1
THEN 1
WHEN ONLINE_PEOPLE_MAILING_LISTS_VIEW = 0
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 0 AND ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 0 AND ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 0
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_MAILING_LISTS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_MAILING_LISTS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_DELETE = 1
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_PEOPLE_SEARCH = 1
THEN 1
WHEN ONLINE_PEOPLE_PEOPLE_SEARCH = 0
THEN 0
END
FROM INSERTED
END
END
this will handle all rows if multiple rows are affected by the trigger:
INSERT INTO dbo.WebOnlineUserPeopleDashboard
(
ONLINE_USERACCOUNT_ID,
ONLINE_ROOMS_DIRECTORY,
ONLINE_ROOMS_LIST,
ONLINE_ROOMS_PLACEMENT,
ONLINE_ROOMS_MANAGEMENT,
ONLINE_MAILINGLIST_DIRECTORY,
ONLINE_MAILINGLIST_LIST,
ONLINE_MAILINGLIST_MEMBERS,
ONLINE_MAILINGLIST_MANAGER,
ONLINE_PEOPLESEARCH_DIRECTORY
)
SELECT
ONLINE_USERACCOUNT_ID,
1,
1,
1,
1,
1,
1,
1,
1,
1
FROM INSERTED
WHERE ONLINE_PEOPLE_FULL_ACCESS=1
UNION
SELECT
ONLINE_USERACCOUNT_ID,
0,
0,
0,
0,
0,
0,
0,
0,
0
FROM INSERTED
WHERE ONLINE_PEOPLE_FULL_ACCESS=0
UNION
SELECT
ONLINE_USERACCOUNT_ID,
CASE --DIRECTORY
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_FULL_ACCESS = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_FULL_ACCESS = 0
THEN 0
--ELSE ??? what is the default
END,
CASE
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 1
THEN 1
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 0
THEN 0
--ELSE ??? what is the default
END,
CASE
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 0 AND ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 0 AND ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 0
THEN 0
--ELSE ??? what is the default
END,
CASE
WHEN ONLINE_PEOPLE_ROOMS_MANAGEMENT_FULL_ACCESS = 1
THEN 1
WHEN ONLINE_PEOPLE_ROOMS_MANAGEMENT_FULL_ACCESS = 0
THEN 0
--ELSE ??? what is the default
END,
CASE
WHEN ONLINE_PEOPLE_MAILING_LISTS_FULL_ACCESS = 1 OR ONLINE_PEOPLE_MAILING_LISTS_VIEW = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_MAILING_LISTS_FULL_ACCESS = 0
THEN 0
--ELSE ??? what is the default
END,
CASE
WHEN ONLINE_PEOPLE_MAILING_LISTS_VIEW = 1
THEN 1
WHEN ONLINE_PEOPLE_MAILING_LISTS_VIEW = 0
THEN 0
--ELSE ??? what is the default
END,
CASE
WHEN ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 0 AND ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 0 AND ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 0
THEN 0
--ELSE ??? what is the default
END,
CASE
WHEN ONLINE_PEOPLE_MAILING_LISTS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_MAILING_LISTS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_DELETE = 1
THEN 0
--ELSE ??? what is the default
END,
CASE
WHEN ONLINE_PEOPLE_PEOPLE_SEARCH = 1
THEN 1
WHEN ONLINE_PEOPLE_PEOPLE_SEARCH = 0
THEN 0
--ELSE ??? what is the default
END
FROM INSERTED
WHERE ONLINE_PEOPLE_FULL_ACCESS NOT IN (0,1)
Something like this?
IF (SELECT ONLINE_PEOPLE_FULL_ACCESS FROM INSERTED) = 1
INSERT INTO dbo.WebOnlineUserPeopleDashboard
(
ONLINE_USERACCOUNT_ID,
ONLINE_ROOMS_DIRECTORY,
ONLINE_ROOMS_LIST,
ONLINE_ROOMS_PLACEMENT,
ONLINE_ROOMS_MANAGEMENT,
ONLINE_MAILINGLIST_DIRECTORY,
ONLINE_MAILINGLIST_LIST,
ONLINE_MAILINGLIST_MEMBERS,
ONLINE_MAILINGLIST_MANAGER,
ONLINE_PEOPLESEARCH_DIRECTORY
)
VALUES
SELECT
ONLINE_USERACCOUNT_ID,
1,
1,
1,
1,
1,
1,
1,
1,
1
FROM INSERTED
ELSE IF (SELECT ONLINE_PEOPLE_FULL_ACCESS FROM INSERTED) = 0
INSERT INTO dbo.WebOnlineUserPeopleDashboard
(
ONLINE_USERACCOUNT_ID,
ONLINE_ROOMS_DIRECTORY,
ONLINE_ROOMS_LIST,
ONLINE_ROOMS_PLACEMENT,
ONLINE_ROOMS_MANAGEMENT,
ONLINE_MAILINGLIST_DIRECTORY,
ONLINE_MAILINGLIST_LIST,
ONLINE_MAILINGLIST_MEMBERS,
ONLINE_MAILINGLIST_MANAGER,
ONLINE_PEOPLESEARCH_DIRECTORY
)
VALUES
SELECT
ONLINE_USERACCOUNT_ID,
0,
0,
0,
0,
0,
0,
0,
0,
0
FROM INSERTED
ELSE
INSERT INTO dbo.WebOnlineUserPeopleDashboard
(
ONLINE_USERACCOUNT_ID,
ONLINE_ROOMS_DIRECTORY,
ONLINE_ROOMS_LIST,
ONLINE_ROOMS_PLACEMENT,
ONLINE_ROOMS_MANAGEMENT,
ONLINE_MAILINGLIST_DIRECTORY,
ONLINE_MAILINGLIST_LIST,
ONLINE_MAILINGLIST_MEMBERS,
ONLINE_MAILINGLIST_MANAGER,
ONLINE_PEOPLESEARCH_DIRECTORY
)
SELECT
ONLINE_USERACCOUNT_ID,
CASE --DIRECTORY
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_FULL_ACCESS = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_FULL_ACCESS = 0
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 1
THEN 1
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 0
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 0 AND ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 0 AND ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 0
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_ROOMS_MANAGEMENT_FULL_ACCESS = 1
THEN 1
WHEN ONLINE_PEOPLE_ROOMS_MANAGEMENT_FULL_ACCESS = 0
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_MAILING_LISTS_FULL_ACCESS = 1 OR ONLINE_PEOPLE_MAILING_LISTS_VIEW = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_MAILING_LISTS_FULL_ACCESS = 0
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_MAILING_LISTS_VIEW = 1
THEN 1
WHEN ONLINE_PEOPLE_MAILING_LISTS_VIEW = 0
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 0 AND ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 0 AND ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 0
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_MAILING_LISTS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_DELETE = 1
THEN 1
WHEN ONLINE_PEOPLE_MAILING_LISTS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_DELETE = 1
THEN 0
END,
CASE
WHEN ONLINE_PEOPLE_PEOPLE_SEARCH = 1
THEN 1
WHEN ONLINE_PEOPLE_PEOPLE_SEARCH = 0
THEN 0
END
FROM INSERTED