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

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

Related

Avoid calling a scalar function multiple times in a MERGE and UPDATE statement

I want to call function that return scalar multiple times.
How can I do that in a MERGE statement?
My code:
MERGE [dbo].MyTable m
USING (select * from Table2 edbl
where IsRowError = 0) edbTable
on edbTable.Filed1 = m.Filed1
WHEN MATCHED THEN UPDATE SET
m.Filed2 = case when dbo.func1(edbTable.Filed7) = 1 then 1
when dbo.func1(edbTable.Filed7) = 0 then 1
else 0 end,
m.Filed3 = case when dbo.func1(edbTable.Filed7) = 0 then 0 end,
m.Filed4 = case when dbo.func1(edbTable.Filed7) = 0 then 3 end,
m.Filed5 = case when dbo.func1(edbTable.Filed7) = 0 then 9 else 5 end,
m.Filed6 = getdate();
I want also to do it in UPDATE statement:
UPDATE md
set Filed1 = case when dbo.func1(edbl.Filed7) = 0 then 0 end,
Filed5 = case when dbo.func1(edbTable.Filed7)=0 then 9 else 5 end,
Filed6 = getdate()
from Table2 edbl
join Table3 m on edbl.Filed2= m.Filed2
join Table4 md on m.Filed3=md.Filed3
join Table5 mb on md.Filed4 = mb.Filed4
where IsRowError = 0
You can do it in the subquery:
MERGE [dbo].MyTable m
USING (select edbl.*, dbo.func1(edbTable.Filed7) as func1
from Table2 edbl
where IsRowError = 0
) edbTable
on edbTable.Filed1 = m.Filed1
WHEN MATCHED THEN UPDATE SET
m.Filed2 = (case when func1 = 1 then 1
when func1 = 0 then 1
else 0
end),
m.Filed3 = case when func1 = 0 then 0 end,
m.Filed4 = case when func1 = 0 then 3 end,
m.Filed5 = case when func1 = 0 then 9 else 5 end,
m.Filed6 = getdate();
However, it is not clear that this is a big win. If most rows do not match, this might not be a big improvement.
You could do it once inside USING part:
MERGE [dbo].MyTable m
USING (select *, dbo.func1(Filed7) AS f from Table2 edbl
where IsRowError = 0) edbTable
on edbTable.Filed1 = m.Filed1
WHEN MATCHED THEN UPDATE SET
m.Filed2 = case when f = 1 then 1
when f = 0 then 1
else 0 end,
m.Filed3 = case when f = 0 then 0 end,
m.Filed4 = case when f = 0 then 3 end,
m.Filed5 = case when f = 0 then 9 else 5 end,
m.Filed6 = getdate();
You can use APPLY with UPDATE statement :
UPDATE md
SET Filed1 = CASE WHEN fun = 0 THEN 0 END,
Filed5 = CASE WHEN fun = 0 THEN 9 ELSE 5 END,
Filed6 = getdate()
FROM Table2 edbl JOIN
Table3 m
ON edbl.Filed2 = m.Filed2 JOIN
Table4 md
ON m.Filed3 = md.Filed3 JOIN
Table5 mb
ON md.Filed4 = mb.Filed4 CROSS APPLY
( VALUES (dbo.func1(edbl.Filed7))
) edbl1(fun)
where IsRowError = 0;

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

How to count up a single character in multiple fields in SQL

HI i have a table 'TableCustomers' and within this table are many fields titled 'Name1' 'Name2''Name3'.... 'Name40' some of these fields just have the letter 'x' i want to know how many 'x' are there in all 40 fields
One possible solution is something like;
SELECT
CASE WHEN [Name] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name1] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name2] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name3] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name4] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name5] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name6] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name7] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name8] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name9] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name10] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name11] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name12] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name13] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name14] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name15] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name16] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name17] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name18] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name19] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name20] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name21] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name22] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name23] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name24] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name25] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name26] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name27] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name28] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name29] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name30] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name31] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name32] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name33] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name34] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name35] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name36] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name37] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name38] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name39] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name40] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name41] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name42] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name43] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name44] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name45] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name46] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name47] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name48] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name49] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name50] = 'x' THEN 1 ELSE 0 END
FROM [TableCustomers]
I generated this using an application called Nimble Text (http://nimbletext.com/) though you could use dynamic SQL along with the sys.columns view to generate this statement within SQL server, if the columns of the table change. Let me know if you would like an example of this...
You may try something like this:
select len(name00 + name01 + .. + name40) - len(replace(name00 + name01 + .. + name40, 'x', ''))
from yourTable;

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;

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.