SQL Stuff For XML Path with Multiple Fields - sql

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

Related

SQL add conversion of date to existing query

I'm would like to convert the date using this function. I'm using SQL server 2014
CONVERT(varchar, sc.StartDate,103) + '" + " " + "' + '" + " - " + "' + CONVERT(varchar, sc.EndDate,103) SIPDate
so it will display in dd/mm/yyyy - dd/mm/yyyy. How do i add it into the query? Thanks.
The query is:
SELECT CONCAT(sc.StartDate, sc.ENDDate) SIPDate,
COUNT (sj.LOComment) WeekReviewed,
COUNT(sjd.WeekNo) TotalWeek,
SUM(sjj.TotalDaysRecord) TotalDaysRecord,
COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
SELECT sj.WeekNo,
CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord
FROM StudentJournal sj
) AS sjj
ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
ON sc.AdminNo = sjd.AdminNo
GROUP BY CONCAT(sc.StartDate, sc.ENDDate)
You can just SELECT and GROUP BY this formatted date expression, i.e.:
SELECT CONVERT(varchar, sc.StartDate, 103) + " - " + CONVERT(varchar, sc.EndDate, 103) AS SIPDate,
COUNT (sj.LOComment) WeekReviewed,
COUNT(sjd.WeekNo) TotalWeek,
SUM(sjj.TotalDaysRecord) TotalDaysRecord,
COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
SELECT sj.WeekNo,
CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord
FROM StudentJournal sj
) AS sjj
ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
ON sc.AdminNo = sjd.AdminNo
GROUP BY CONVERT(varchar, sc.StartDate, 103) + " - " + CONVERT(varchar, sc.EndDate, 103)
Try this. since your SQL SERVER is 2014 you can use format to format the date
SELECT CONCAT(FORMAT(sc.StartDate,'dd/MM/yyy'),+ ' - ' +FORMAT(sc.ENDDate,'dd/MM/yyy')) SIPDate,
COUNT (sj.LOComment) WeekReviewed,
COUNT(sjd.WeekNo) TotalWeek,
SUM(sjj.TotalDaysRecord) TotalDaysRecord,
COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
SELECT sj.WeekNo,
CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord
FROM StudentJournal sj
) AS sjj
ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
ON sc.AdminNo = sjd.AdminNo
GROUP BY CONCAT(FORMAT(sc.StartDate,'dd/MM/yyy'),+ ' - ' +FORMAT(sc.ENDDate,'dd/MM/yyy'))

How to get rid of these co-related sub queries from select statement

Here I am dealing with only one table, It has an attribute called CmpltStscd where for each value I need a different column in the output as an aggregate function.
Is there a way to get rid of these subqueries ?
Trying a lot
Select
Mg.RsrcId
, Count(Mg.ActID) Num_of_Goals
, (SELECT COUNT(MC.ActID) FROM TM.MatrixGoal MC where MC.CmpltStsCd = 2 AND MG.RsrcID = MC.RsrcID AND MC.ActiveFlg = 1 AND MC.DelFlg = 0 AND MC.CorporateGoalFlg <> 1 AND MC.StsCd in (3,4)) as Complete
, (SELECT COUNT(MI.ActID) FROM TM.MatrixGoal MI where MI.CmpltStsCd = 4 AND MG.RsrcID = MI.RsrcID AND MI.ActiveFlg = 1 AND MI.DelFlg = 0 AND MI.CorporateGoalFlg <> 1 AND MI.StsCd in (3,4)) as Issues
, (SELECT COUNT(MO.ActID) FROM TM.MatrixGoal MO where MO.CmpltStsCd = 1 AND MG.RsrcID = MO.RsrcID AND MO.ActiveFlg = 1 AND MO.DelFlg = 0 AND MO.CorporateGoalFlg <> 1 AND MO.StsCd in (3,4)) as OnTrack
From
TM.MatrixGoal AS Mg
Where MG.ActiveFlg = 1
AND MG.DelFlg = 0
AND MG.CorporateGoalFlg <> 1
AND MG.StsCd in (3,4)
Group By RsrcId
You need a pivot/cross tab query
SELECT Mg.RsrcId,
COUNT(Mg.ActID) Num_of_Goals,
SUM(CASE
WHEN CmpltStsCd = 2 THEN 1
ELSE 0
END) AS Complete,
SUM(CASE
WHEN CmpltStsCd = 4 THEN 1
ELSE 0
END) AS Issues,
SUM(CASE
WHEN CmpltStsCd = 1 THEN 1
ELSE 0
END) AS OnTrack
FROM TM.MatrixGoal AS Mg
WHERE MG.ActiveFlg = 1
AND MG.DelFlg = 0
AND MG.CorporateGoalFlg <> 1
AND MG.StsCd IN ( 3, 4 )
GROUP BY RsrcId
I think you can just use conditional aggregation:
Select Mg.RsrcId, Count(Mg.ActID) Num_of_Goals,
SUM(CASE WHEN mg.CmpltStsCd = 2 THEN 1 ELSE 0 END) as Complete,
SUM(CASE WHEN mg.CmpltStsCd = 4 THEN 1 ELSE 0 END) as Issues
SUM(CASE WHEN mg.CmpltStsCd = 1 THEN 1 ELSE 0 END) as OnTrack
From TM.MatrixGoal Mg
Where MG.ActiveFlg = 1 AND MG.DelFlg = 0 AND MG.CorporateGoalFlg <> 1 AND
MG.StsCd in (3,4)
Group By RsrcId;

How to count non-null/non-blank values in SQL

I have data like the following:
And what I want is to count the PONo, PartNo, and TrinityID fields with a value in them, and output data like this:
How can I do this counting in SQL?
select
Job_number, Item_code,
case when RTRIM(PONo) = '' or PONo is null then 0 else 1 end +
case when RTRIM(PartNo) = '' or PartNo is null then 0 else 1 end +
case when RTRIM(TrinityID) = '' or TrinityID is null then 0 else 1 end
as [Count]
from YourTable
Try this:
select Job_Number = t.Job_Number ,
Item_Code = t.Item_Code ,
"Count" = sum( case ltrim(rtrim(coalesce( PONo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( PartNo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( TrinityID , '' ))) when '' then 0 else 1 end
)
from dbo.my_table t
group by t.Job_Number , t.Item_Code
If you want to exclude data where all the tested fields are null or empty, add a having clause:
having sum( case ltrim(rtrim(coalesce( PONo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( PartNo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( TrinityID , '' ))) when '' then 0 else 1 end
) > 0
Easy!

SQL geting Top X number of rows

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.

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