Using the TextHeader and TextBody property to define the view - powershell

I've been tasked to create a Powershell script that will create the following view which works currently within Microsoft SQL Studio. I've made a copy of the working script and would like to to apply it to this Microsoft example. My question is - can you take the known working query below and insert it in the TextBody property below?
# Set the TextHeader and TextBody property to define the view.
$myview.TextHeader = "CREATE VIEW [Sales].[Test_View] AS"
$myview.TextBody ="SELECT h.SalesOrderID, d.OrderQty FROM
Sales.SalesOrderHeader AS h INNER JOIN Sales.SalesOrderDetail AS d ON
h.SalesOrderID = d.SalesOrderID"
CREATE VIEW [dbo].[PROV] AS
SELECT drname.dr_id, drname.create_dt, dr_fname, dr_iname, dr_lname,
drtitle, drsuffix, gender, birthdate, marital, ethnic, drname.email,
org_name, dr_ofname, dr_oiname, dr_olname, nationalid, drname.medicare,
drname.medicaid,
CASE
WHEN EXISTS(SELECT 1 FROM stfstatu act WHERE act.dr_id =
drname.dr_id AND fac_cd NOT IN('SMC','WAR','CCS') AND act.active123 IN
('1','3','N','G','6','S','U','CA','TPRO')) THEN 'Y'
ELSE 'N'
END as Active_Status,
a.sch_type as Addr_Type_sch_type, a.addr, a.addr2, a.city,
a.state, a.zip, a.phone, a.fax, a2.tax_id,
(select top 1 rtrim(d.dr_lname) from drname d join address aa
on d.dr_id = aa.dr_id join address2 aa2 on aa.link = aa2.l_address where
d.verfac = 1 and aa2.tax_id = a2.tax_id and drname.verfac = 0) as
Addr_DrPGp,
lan_tab.txt as lan_tab_txt, dr_lan.lan_cd as dr_lan_cd, CASE
WHEN dr_spec.specorder = 1 THEN dr_spec.specialty ELSE '' END as dr_spec,
CASE WHEN dr_spec.PrimSpec = 1 THEN 'P' ELSE '' END as PrimSpec, school.cd
as school_cd, school.txt as school_txt, educate.fdate as educate_fdate,
educate.sch_type, educ_arr.txt as school_type_txt, educate.degree,
educ_tab.txt
as degree_txt, educate.tdate as educate_tdate, CASE WHEN dr_spec.specorder =
1
THEN spec_tab.txt ELSE '' END as spec_Tab_txt,
CASE WHEN stfstatu.pcp_spec = 'B' then 'Yes' WHEN
stfstatu.pcp_spec = 'P' then 'Yes' ELSE 'No' END as PCP,
case when stfstatu.pcp_spec = 'B' then 'PCP and Specialist'
else case when stfstatu.pcp_spec = 'P' then 'PCP'
else case when stfstatu.pcp_spec = 'S' then
'Specialist'
else ''
END
END
END as PCPSpec, taxonomy.cd as tax_cd
FROM drname
JOIN stfstatu on drname.dr_id = stfstatu.dr_id
LEFT JOIN address1 a on drname.dr_id = a.dr_id AND a.sch_type = 'G' AND
a.link = (SELECT TOP 1 link FROM address1 WHERE sch_type = 'G' AND
address1.dr_id = drname.dr_id ORDER BY LINK DESC)
LEFT JOIN address2 a2 on a2.l_address = a.link
LEFT JOIN dr_lan on drname.dr_id = dr_lan.dr_id
LEFT JOIN lan_tab on lan_tab.cd = dr_lan.lan_cd
LEFT JOIN dr_spec on drname.dr_id = dr_spec.dr_id
LEFT JOIN spec_tab on dr_spec.specialty = spec_tab.cd
LEFT JOIN educate on educate.dr_id = drname.dr_id
LEFT JOIN school on educate.cd = school.cd
LEFT JOIN educ_arr on educ_arr.cd = educate.sch_type
LEFT JOIN facility on facility.fac_cd = educate.fac_cd
LEFT JOIN educ_tab on educate.degree = educ_tab.cd
LEFT JOIN taxonomy on taxonomy.dr_id = drname.dr_id
--Where LTRIM(RTRIM(dr_spec.Fac_Cd)) = ''

did you try using the here string (https://www.safaribooksonline.com/library/view/windows-powershell-cookbook/9780596528492/ch05s03.html]) for TextBody?
# Set the TextHeader and TextBody property to define the view.
$myview.TextHeader = "CREATE VIEW [Sales].[Test_View] AS"
$myview.TextBody = #"
SELECT h.SalesOrderID, d.OrderQty FROM
Sales.SalesOrderHeader AS h INNER JOIN Sales.SalesOrderDetail AS d ON
h.SalesOrderID = d.SalesOrderID
CREATE VIEW [dbo].[PROV] AS
SELECT drname.dr_id, drname.create_dt, dr_fname, dr_iname, dr_lname,
drtitle, drsuffix, gender, birthdate, marital, ethnic, drname.email,
org_name, dr_ofname, dr_oiname, dr_olname, nationalid, drname.medicare,
drname.medicaid,
CASE
WHEN EXISTS(SELECT 1 FROM stfstatu act WHERE act.dr_id =
drname.dr_id AND fac_cd NOT IN('SMC','WAR','CCS') AND act.active123 IN
('1','3','N','G','6','S','U','CA','TPRO')) THEN 'Y'
ELSE 'N'
END as Active_Status,
a.sch_type as Addr_Type_sch_type, a.addr, a.addr2, a.city,
a.state, a.zip, a.phone, a.fax, a2.tax_id,
(select top 1 rtrim(d.dr_lname) from drname d join address aa
on d.dr_id = aa.dr_id join address2 aa2 on aa.link = aa2.l_address where
d.verfac = 1 and aa2.tax_id = a2.tax_id and drname.verfac = 0) as
Addr_DrPGp,
lan_tab.txt as lan_tab_txt, dr_lan.lan_cd as dr_lan_cd, CASE
WHEN dr_spec.specorder = 1 THEN dr_spec.specialty ELSE '' END as dr_spec,
CASE WHEN dr_spec.PrimSpec = 1 THEN 'P' ELSE '' END as PrimSpec, school.cd
as school_cd, school.txt as school_txt, educate.fdate as educate_fdate,
educate.sch_type, educ_arr.txt as school_type_txt, educate.degree,
educ_tab.txt
as degree_txt, educate.tdate as educate_tdate, CASE WHEN dr_spec.specorder =
1
THEN spec_tab.txt ELSE '' END as spec_Tab_txt,
CASE WHEN stfstatu.pcp_spec = 'B' then 'Yes' WHEN
stfstatu.pcp_spec = 'P' then 'Yes' ELSE 'No' END as PCP,
case when stfstatu.pcp_spec = 'B' then 'PCP and Specialist'
else case when stfstatu.pcp_spec = 'P' then 'PCP'
else case when stfstatu.pcp_spec = 'S' then
'Specialist'
else ''
END
END
END as PCPSpec, taxonomy.cd as tax_cd
FROM drname
JOIN stfstatu on drname.dr_id = stfstatu.dr_id
LEFT JOIN address1 a on drname.dr_id = a.dr_id AND a.sch_type = 'G' AND
a.link = (SELECT TOP 1 link FROM address1 WHERE sch_type = 'G' AND
address1.dr_id = drname.dr_id ORDER BY LINK DESC)
LEFT JOIN address2 a2 on a2.l_address = a.link
LEFT JOIN dr_lan on drname.dr_id = dr_lan.dr_id
LEFT JOIN lan_tab on lan_tab.cd = dr_lan.lan_cd
LEFT JOIN dr_spec on drname.dr_id = dr_spec.dr_id
LEFT JOIN spec_tab on dr_spec.specialty = spec_tab.cd
LEFT JOIN educate on educate.dr_id = drname.dr_id
LEFT JOIN school on educate.cd = school.cd
LEFT JOIN educ_arr on educ_arr.cd = educate.sch_type
LEFT JOIN facility on facility.fac_cd = educate.fac_cd
LEFT JOIN educ_tab on educate.degree = educ_tab.cd
LEFT JOIN taxonomy on taxonomy.dr_id = drname.dr_id
--Where LTRIM(RTRIM(dr_spec.Fac_Cd)) = ''
"#

Related

Need to replace case statement in where clause

**The below query is working badly, and I'm planning to replace case stmt in where clause with temp table. Pls suggest to me some option to rewrite this query
Is there a way to simplify this into multiple SQLs
Also explain me how this case stmt works in where clause**
select DISTINCT
ph.order_type_cd,
c.order_num,
ph.created_date,
os.order_status_desc,
ph.facility_name,
PH.vendor_order_num,
l.lic_nm,
(u.last_nm + ', ' + u.first_nm )as requestor_nm,
bl.billing_location_desc,
ph.ship_to_name,
dm.dispatch_method_name_desc as courier,
PH.po_last_updated_dt as last_updated_date,
(u1.last_nm + ', ' + u1.first_nm )as last_updated_by,
ph.right_grp_id,
c.line_id,
a.material_product_id,
a.parent_material_product_id
from avt_po_detail c
inner join avt_po_hdr ph on ph.order_num = c.order_num
left outer join avt_material_product AS a on a.material_product_id=c.material_product_id
INNER JOIN #entity_ids po on po.entity_id = c.order_num --or #select_pos = 0 )
LEFT OUTER JOIN dbo.ert_product AS pc ON pc.product_id = a.product_id
left outer join ert_product_type as pt on pt.product_type_cd = pc.product_type_cd
LEFT OUTER JOIN dbo.ert_product_xref as xref on xref.product_id = a.parent_product_id AND xref.version_id = 1
left outer join avt_order_status AS OS ON OS.order_status_cd = PH.order_status_cd
left outer join avt_licensee l on l.lic_id = ph.lic_id
left outer join avt_po_from pf on pf.material_product_id = a.material_product_id and pf.line_id = c.line_id
left outer join avt_po_audio_detail pad on pad.line_id = c.line_id
left outer join avt_materials_trans_material MTM on mtm.sequence_no = c.video_seq_no
left outer join avt_user u on u.userid = ph.requestor_id
left outer join avt_user u1 on u1.userid = ph.po_last_updated_by
left outer join avt_billing_location bl on bl.billing_location_cd = PH.billing_location_cd
left outer join avt_dispatch_method_name dm on dm.dispatch_method_name_id = PH.dispatch_method_name_id
left outer join avt_right_group arg on arg.right_grp_id = ph.right_grp_id
left outer join #prod product on product.product_id=a.product_id and (product.version_id=c.dist_version_id OR product.version_id = 1)
where (pf.from_desc in ( select from_desc from #from_desc ) or #select_form = 0)
and (case WHEN c.dist_version_id IS NULL THEN c.UCS_prod_no
ELSE
CASE
WHEN isnull(pt.version_control_ind, 'N') = 'Y' THEN xref.rss_prod_no
WHEN
isnull(pt.version_control_ind, 'N') = 'N' AND
xref.rss_prod_no IS NULL AND
xref.rss_film_id IS NULL THEN xref.rss_titleid
WHEN
isnull(pt.version_control_ind, 'N') = 'N' AND
xref.rss_prod_no IS NOT NULL AND
xref.rss_film_id IS NULL THEN substring(pc.glacct, 24, 5) -- ASR 3290687
WHEN isnull(pt.version_control_ind, 'N') = 'N' AND xref.rss_film_id IS NOT NULL THEN isnull(substring(pc.glacct, 24, 5), -- ASR 3290687
(
SELECT substring(dbo.ert_product.glacct, 24, 5) -- ASR 3290687
FROM dbo.ert_product
WHERE dbo.ert_product.product_id = a.parent_product_id
))
END
END in (select prod_no from #prod_no) or #prod_no = 0)

Debugging: LATERAL query in PostgreSQL

SELECT
cu.user_id,
cu.gender,
CASE WHEN cu.looking_for_gender = cu.gender THEN 1 ELSE 0 END AS
sexual_orientation,
os_name,
ROUND((DATE(NOW()) - cu.birthdate)/365.25) AS user_age,
SUM(dsb.likes) AS likes,
SUM(dsb.dislikes) AS dislikes,
SUM(dsb.blocks) AS blocks,
SUM(dsb.matches) AS matches,
SUM(dsb.received_likes) AS received_likes,
SUM(dsb.received_dislikes) AS received_dislikes,
SUM(dsb.received_blocks) AS received_blocks,
CASE WHEN cu.status = 'default' THEN 1 ELSE 0 END AS recall_case,
CASE WHEN cu.status = 'default' THEN extract(epoch from
cu.last_activity - cu.updated_time)/86400 ELSE 0 END AS
recall_retention
FROM ( SELECT stats.core_users cu
LEFT JOIN yay.daily_swipes_by_users dsb ON (dsb.user_id = cu.user_id)
WHERE cu.user_id = '1' GROUP BY 1) e1
LEFT JOIN LATERAL (SELECT cd.os_name FROM stats.core_devices cd WHERE
e1.user_id = cd.user_id ORDER BY cd.updated_time DESC LIMIT 1) e2
ON TRUE;
Current Error Code:
ERROR: syntax error at or near "LEFT"
LINE 18: LEFT JOIN yay.daily_swipes_by_users dsb ON (dsb.user_id = cu...
^
The supplied query would fail in many ways, the following might work I hope, but as you can see this drops a great deal of other columns in the process
SELECT
e1.user_id
, e1.cu
, e2.os_name
FROM (
SELECT stats.core_users cu, cu.user_id
LEFT JOIN yay.daily_swipes_by_users dsb ON (dsb.user_id = cu.user_id)
WHERE cu.user_id = '1'
GROUP BY stats.core_users cu, cu.user_id
) e1
LEFT JOIN LATERAL(
SELECT cd.os_name
FROM stats.core_devices cd
WHERE e1.user_id = cd.user_id
ORDER BY cd.updated_time DESC
LIMIT 1) e2 ON TRUE
;
SELECT
cu.user_id,
cu.gender,
CASE WHEN cu.looking_for_gender = cu.gender THEN 1 ELSE 0 END AS sexual_orientation,
e2.os_name,
ROUND((DATE(NOW()) - cu.birthdate)/365.25) AS user_age,
CASE WHEN cu.status = 'default' THEN 1 ELSE 0 END AS recall_case,
CASE WHEN cu.status = 'default' THEN extract(epoch from cu.last_activity - cu.updated_time)/86400 ELSE 0 END AS recall_retention,
SUM(dsb.likes) AS likes,
SUM(dsb.dislikes) AS dislikes,
SUM(dsb.blocks) AS blocks,
SUM(dsb.matches) AS matches,
SUM(dsb.received_likes) AS received_likes,
SUM(dsb.received_dislikes) AS received_dislikes,
SUM(dsb.received_blocks) AS received_blocks
FROM
stats.core_users cu
LEFT JOIN yay.daily_swipes_by_users dsb ON (dsb.user_id = cu.user_id)
LEFT JOIN LATERAL (SELECT cd.os_name FROM stats.core_devices cd WHERE cu.user_id = cd.user_id ORDER BY cd.updated_time DESC LIMIT 1) e2
ON TRUE
WHERE cu.user_id = '1'
GROUP BY 1,2,3,4,5,6,7
;
This works.

Case Condition to join columns

Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'CASE'
I am trying to join columns based on the column value.
If case conditon 1 is true then i want to join
ON im.ItemNumber = left(tr.itemnumber,len(tr.itemnumber)-4)
if Case condition 2 is true then
ON tr.ItemNumber = im.LegacyItemNumber
else
tr.ItemNumber = im.ItemNumber
SELECT
im.Category as RootCategory,GETDATE() AS LoadDate,tr.*
into transactionreport_stage_testNN
FROM transaction tr
LEFT OUTER JOIN ALLDW_ora.dbo.Location(NOLOCK) loc ON tr.location=loc.location
CASE
when
right(tr.itemnumber,4) = '_old' then
LEFT OUTER JOIN [Inventory](NOLOCK) im
ON im.ItemNumber = left(tr.itemnumber,len(tr.itemnumber)-4)
when
tr.itemnumber <> im.ItemNumber
then
LEFT OUTER JOIN [Inventory](NOLOCK) im
ON tr.ItemNumber = im.LegacyItemNumber
else
LEFT OUTER JOIN [Inventory](NOLOCK) im
ON tr.ItemNumber = im.itemnumber
end
AND im.Location=loc.LocationNo
Schema
You should probably just use AND's and OR's
LEFT OUTER JOIN [Inventory](NOLOCK) im ON im.Location=loc.LocationNo
AND (
(right(tr.itemnumber,4) = '_old' AND im.ItemNumber = left(tr.itemnumber,len(tr.itemnumber)-4))
OR
(tr.itemnumber <> im.ItemNumber AND tr.ItemNumber = im.LegacyItemNumber)
OR
(tr.ItemNumber = im.itemnumber)
)
it may just be as simple as
SELECT im.Category AS RootCategory,
GETDATE() AS LoadDate,
tr.*
INTO #transactionreport_stage_testNN
FROM [transaction] tr
LEFT OUTER JOIN ALLDW_ora.dbo.Location (NOLOCK) loc ON tr.location = loc.location
LEFT OUTER JOIN [Inventory] (NOLOCK) im ON im.Location = loc.LocationNo
AND REPLACE(tr.itemnumber, '_old', '') IN (im.itemnumber, im.legacyitemnumber)

Checking empty column values in case when and ignoring them

I wrote the following query to check duplicates.
select s.*,m.IsMember_Ind,
case when REPLACE(s.[ Owner Mobile],' ','')
in (select cd.Mobile
from Company_Detail cd
inner join MEMBERSHIP m
on cd.Company_ID = m.Company_ID
where m.IsMember_Ind = 1)
then 'Match'
else ' ' end as OwnerMobileMatch,
case when REPLACE(s.[ Contact Number],' ','')
in (select cd.Mobile
from Company_Detail cd
inner join MEMBERSHIP m
on cd.Company_ID = m.Company_ID
where m.IsMember_Ind = 1)
then 'Match' else ' ' end as ContactMobileMatch
from COMPANY c
inner join surreynonmembers s
on c.TTR_ID = s.[Company ID]
left outer join MEMBERSHIP m
on m.Company_ID = c.Company_ID
My problem is, the case statement returns as a match when finds an empty value which I do not need. How can I modify this query to return if there is only a value?
Have you tried this?
GO
SELECT s.*
,m.IsMember_Ind
,CASE
WHEN REPLACE(s.[ Owner Mobile], ' ', '') IN (
SELECT cd.Mobile
FROM Company_Detail cd
INNER JOIN MEMBERSHIP m ON cd.Company_ID = m.Company_ID
WHERE m.IsMember_Ind = 1
AND ISNULL(s.[ Owner Mobile], '') <> ''
)
THEN 'Match'
ELSE ' '
END AS OwnerMobileMatch
,CASE
WHEN REPLACE(s.[ Contact Number], ' ', '') IN (
SELECT cd.Mobile
FROM Company_Detail cd
INNER JOIN MEMBERSHIP m ON cd.Company_ID = m.Company_ID
WHERE m.IsMember_Ind = 1
AND ISNULL(s.[ Contact Number], '')<> ''
)
THEN 'Match'
ELSE ' '
END AS ContactMobileMatch
FROM COMPANY c
INNER JOIN surreynonmembers s ON c.TTR_ID = s.[Company ID]
LEFT JOIN MEMBERSHIP m ON m.Company_ID = c.Company_ID
GO

T-SQL Nested Subquery

I want to place this working code within a SQL Statement, OR do I need to perform a UDF.
The result set is a one line concatenation, and I want it to be place in every one of the overall result set lines.
----
MAIN QUERY
SELECT
H.CONNECTION_ID,
H.SEQUENTIAL_NO,
H.INVOICE_NUMBER,
H.INVOICE_DATE,
H.LAST_INVOICE_NUMBER,
H.LAST_INVOICE_DATE,
CAST(CASE
WHEN H.COLLECT_DEPOSIT = 1 THEN '-'
ELSE CAST(H.PAYMENT_DUE_DATE AS NVARCHAR(20))
END AS SMALLDATETIME) AS PAYMENT_DUE,
H.JOB_NUMBER,
H.CUST_JOB_NUMBER,
HDR.SALES_PERSON,
H.INSIDE_SALES_PERSON,
H.IS_LAST_INVOICE,
CASE
WHEN H.COLLECT_DEPOSIT = 1 THEN 'CASH'
ELSE H.PAYMENT_TERMS_DESCRIPTION
END AS PAYMENT_TERMS,
H.PRINTED,
H.NOTES,
CUR.ID,
CUR.CODE,
CASE CUR.CODE
WHEN 'USD' THEN '001-106624-211'
WHEN 'EUR' THEN '001-106624-101'
WHEN 'GBP' THEN '001-106624-100'
ELSE '001-106624-001'
END AS BANK_ACCT,
CUR.EXCHANGE_RATE,
H.BILL_CONTACT,
H.CUST_ACCOUNT,
H.CUST_NAME,
H.CUST_ADDR1,
H.CUST_ADDR2,
H.CUST_CITY,
H.CUST_STATE,
H.CUST_ZIP,
H.CONTACT_PHONE_NUMBER,
H.CONTACT_PHONE_NUMBER2,
H.ORDERED_BY_CONTACT,
H.SHIP_TO_NAME,
H.SHIP_TO_ADDR1,
H.SHIP_TO_ADDR2,
H.SHIP_TO_CITY,
H.SHIP_TO_STATE,
H.SHIP_TO_ZIP,
H.SITE_PHONE_NUMBER,
H.SITE_PHONE_NUMBER2,
H.OFFICE_NAME,
H.OFFICE_ADDR1,
H.OFFICE_ADDR2,
H.OFFICE_CITY,
H.OFFICE_STATE,
H.OFFICE_ZIP,
H.OFFICE_PHONE_NUMBER,
H.OFFICE_FAX_NUMBER,
H.DELIVERY_TICKET_NUMBER,
H.PO_NUMBER,
H.DUMMY_INVOICE_TEXT,
(SELECT MESSAGE FROM REPORT_MESSAGES WHERE CODE = 'INVOICE') ADVERT_MESSAGE,
(SELECT MAX(DISCOUNT_PERCENTAGE) FROM PRTINVITEM I2 WHERE I2.CONNECTION_ID = H.CONNECTION_ID AND I2.INVOICE_NUMBER = H.INVOICE_NUMBER) AS MAX_DISCOUNT,
I.ITEM,
I.DESCRIPTION,
I.QUANTITY,
I.UNIT_OF_MEASURE,
I.MINIMUM_CHARGE,
I.WEEKLY_CHARGE,
I.MONTHLY_CHARGE,
I.START_OF_BILLING_PERIOD,
I.END_OF_BILLING_PERIOD,
I.DAYS_USED,
I.WEEKS_USED,
I.DISCOUNT_PERCENTAGE,
I.TAX_CODE_FOR_ITEM,
I.INVENTORY_TYPE,
I.BILLING_LOGIC_TYPE,
I.ACTUAL_WEEKLY_CHARGE_USED,
I.DAYS_IN_ACTUAL_WEEKLY_CHARGE,
II.CHARGEABLE_DAYS,
II.CHARGEABLE_WEEKS,
II.CHARGEABLE_MONTHS,
II.FREE_DAYS_THIS_INVOICE,
CNV.TOTAL_NET_VALUE,
CNV.TOTAL_TAX_VALUE,
CNV.TOTAL_GROSS_VALUE,
CNV.TOTAL_GROSS_VALUE_NS,
CNV.NET_LINE_VALUE,
CMP.EMAIL_ADDRESS
FROM (PRTINVHDR H INNER JOIN PRTINVITEM I ON H.CONNECTION_ID = I.CONNECTION_ID AND H.INVOICE_NUMBER = I.INVOICE_NUMBER)
INNER JOIN INVOICEHDR HDR ON I.INVOICE_NUMBER = HDR.INVNO
INNER JOIN CUSTOMERS CST ON H.CUST_ACCOUNT = CST.CUSTNUM
INNER JOIN JOB JOB ON H.JOB_NUMBER = JOB.JOBNUM
INNER JOIN CURRENCY CUR ON HDR.CURRENCY_ID = CUR.ID
INNER JOIN VWCURRENCYCONVERSION CNV ON I.CONNECTION_ID = CNV.CONNECTION_ID AND I.INVC_UCOUNTER = CNV.INVC_UCOUNTER
INNER JOIN COMPANY CMP ON H.OFFICE_CODE = CMP.OFFICE
INNER JOIN INVOICEITEM II ON I.INVOICE_NUMBER = II.INVNO AND I.INVC_UCOUNTER = II.INVC_UCOUNTER
ORDER BY
H.SEQUENTIAL_NO,
I.PRINT_SEQUENCE
ASC
----
COALESCE QUERY
DECLARE
#DTICKET NVARCHAR(20),
#PUMPCATEGORYNAME NVARCHAR(3999)
SET #DTICKET = ''
SET #PUMPCATEGORYNAME = NULL
(SELECT
#DTICKET = DTICKET,
#PUMPCATEGORYNAME = COALESCE(#PUMPCATEGORYNAME + ', ', '' ) + PUMPCATEGORYNAME
FROM (SELECT
BHDR.DTICKET,
SCD.PUMPCATEGORYNAME
FROM PRTTICKHDR PHDR
INNER JOIN BIDHDR BHDR ON PHDR.DELIV_TICKET_NUMBER = BHDR.DTICKET
INNER JOIN PRTTICKITEM PITM ON PHDR.CONNECTION_ID = PITM.CONNECTION_ID AND PHDR.DELIV_TICKET_NUMBER = PITM.DELIV_TICKET_NUMBER
LEFT JOIN SUBCATEGORYDESCRIPTION SCD ON PITM.ITEM = SCD.PUMPCATEGORY
WHERE SCD.PUMPCATEGORYNAME IS NOT NULL)
SUBCATEGORYDESCRIPTION)
SELECT #DTICKET, #PUMPCATEGORYNAME
Not really sure what you are asking for but you can doing something along the lines of
Select col1 + ', ' + col2 + ', ' + col3 etc....