Using FOR XML to return child queries - sql-server

I am doing the subquery below as a parameter in a SELECT statement in SQL Server 2008. Is there any disadvantage of doing this to get a child query of the parent and parse it out on the server when its returned. Seems to easy.
(
SELECT ag.Name, ag.[Description], ega.Gender, ag.[Order], ag.[Type]
FROM GrassrootsHoops.EventAgeGrade ega
INNER JOIN GrassrootsHoops.AgeGrade ag ON ega.AgeGradeId = ag.Id
WHERE ega.EventId = ev.Id FOR XML PATH('AgeGrade'), ROOT('AgeGrades')
) AS AgeGrades

Related

SQL Alternative for FOR XML PATH

For some reason my query :
SELECT
MP_SQLQRY_ProjectArchive.ProjectShort,
MP_SQLQRY_ProjectArchive.ProjectName ,
MP_SQLQRY_ProjectArchive.ProjectManagerName ,
ProjectOwnerName,
ProjStateName,
ProjectKategoryShort,
ProjectTypeShort,
MP_SQLQRY_ProjectArchive.ProjectPriority,
DepartmentShort,
ProjectArchivedDate,
CONVERT(NVARCHAR(MAX),ERPAccountCodes.ERPAccountCodes) As ERPAccountCodes
FROM
MP_SQLQRY_ProjectArchive
INNER JOIN
SQLQRY_VALID_PROJECT_ORDER_INFO ON MP_SQLQRY_ProjectArchive.ProjectID = SQLQRY_VALID_PROJECT_ORDER_INFO.ProjectID
INNER JOIN
COM_tbl_MAD_Project ON MP_SQLQRY_ProjectArchive.ProjectID = COM_tbl_MAD_Project.ProjectID
LEFT JOIN
(SELECT
ProjectID,
CONVERT(Nvarchar(max), STUFF((SELECT ';' + [SAPNumber] AS [data()]
FROM [COM_tbl_MAD_ProjectERPAccountCode] t2
WHERE t2.[ProjectID] = t1.[ProjectID]
ORDER BY [SAPNumber]
FOR XML PATH('')), 1, 1, '')) AS ERPAccountCodes
FROM
[COM_tbl_MAD_ProjectERPAccountCode] t1
GROUP BY
[ProjectID], [SAPNumber]) AS ERPAccountCodes ON ERPAccountCodes.ProjectID = COM_tbl_MAD_Project.ProjectID
will not return the values for the ERPAccountCodes column. In SQL Server, it works fine and I get my values, which are seperated with a ; as I want.
But when I run it as extern connection with ADODB in access this single column is empty.
I suppose it is a problem with access or ADODB but I have no clue what to do about it
Is there an alternative to FOR XML PATH or is there a different way to do it?
For clarification, the values return some blank values. Not NULL, just these weird blank symbols when i debugged in VBA in access, altough I tried converting it into a NVarchar which theoretically should work just fine.
Left is an Excel created from the MS Access SQL call. Right is from SQL Server Management Studio:

ODBC call failed. The multipart identifier "(tablename)" could not be bound. (#4104)

This query doesnt work when i run this query with tables from sql server as linked back end. But works just fine when tables are from linked ms access back end.
I think it has something to do with the join clause.
SELECT qry_Product_Warehouse.Warehouse_ID, qry_Product_Warehouse.Product_ID,
CDbl(Nz(IIf(IsNull([tbl_Product_Quantity].[Stock_Recount_Date]),
Sum([qry_Product_Transaction].[qty]),
[tbl_Product_Quantity].[Stock_Recount_Quantity]+Sum(IIf([qry_Product_Transaction].[Date]>[tbl_product_quantity].[Stock_Recount_Date],[qry_Product_Transaction].[Qty],0))),0)) AS Current_Qty
FROM (qry_Product_Warehouse
LEFT JOIN qry_Product_Transaction ON (qry_Product_Warehouse.Warehouse_ID = qry_Product_Transaction.Location)
AND (qry_Product_Warehouse.Product_ID = qry_Product_Transaction.Product))
LEFT JOIN tbl_Product_Quantity ON (qry_Product_Warehouse.Product_ID = tbl_Product_Quantity.Product)
AND (qry_Product_Warehouse.Warehouse_ID = tbl_Product_Quantity.Warehouse)
GROUP BY qry_Product_Warehouse.Warehouse_ID, qry_Product_Warehouse.Product_ID, tbl_Product_Quantity.Stock_Recount_Quantity, tbl_Product_Quantity.Stock_Recount_Date;

Convert IIF statement from access to T-SQL SQL Server(Pass-Through)

I have an update query that I created it in Access 2013. I have a table with over 1.5 million records, I am trying to run an update query but it takes forever sometimes especially if I am updating thousands of rows. So I figured out that I would use pass through option in access but when I try to do that I get a syntax error in my query as the syntax is different in access than it is in SQL Server. How could I convert my query to sql server so my queries won't take forever to run.
Here is my original access query that I want to convert it to T-SQL.
UPDATE CLAIM
INNER JOIN 06 ON [06].ID = Claim.ID
SET reason_code_01= IIF(reason_code_01 is null,”06”,reason_code_01), reason_code_02= IIF(reason_code_01 <> null,”06”,reason_code_02), reason_code_03= IIF(reason_code_01 <> null,”06”,reason_code_03), reason_code_04= IIF(reason_code_01 <> null,”06”,reason_code_04),overpaid_deduc= IIF(overpaid_deduc is null,[06].[DED AMT],overpaid_deduc), overpaid_deduc2= IIF(overpaid_deduc <>null,[06].[DED AMT],overpaid_deduc2),overpaid_deduc3= IIF(overpaid_deduc <> null,[06].[DED AMT],overpaid_deduc3),overpaid_pay1= IIF(overpaid_pay1 is null,[06].[PAY 1],overpaid_pay1),overpaid_pay2= IIF(overpaid_pay1 <> null,[06].[PAY 1],overpaid_pay2),overpaid_pay3= IIF(overpaid_pay1 <> null,[06].[PAY 1],overpaid_pay3)
WHERE ((([06].sort_order)=1));
Your syntax is off a bit for updates with joins. And you need to use a case statement instead of iif. Here is a condensed version which should help:
UPDATE c
SET reason_code_01=
case when reason_code_01 is null
then '06'
else reason_code_01
end,
...
overpaid_pay3=
case when overpaid_pay1 is not null
then [06].[PAY 1]
else overpaid_pay3
end
FROM CLAIM c
INNER JOIN 06 ON [06].ID = c.ID
WHERE [06].sort_order=1

SQL Server - Getting XML from result set within one tag

I am trying without much success to build a XML out of my result set.
My result set should look like this :
<CSVRoot>
<CsvColumn>MDCY_Code</CsvColumn>
<CsvColumn>MDCY_Description</CsvColumn>
</CSVRoot>
This is what my query returns :
<CSVRoot>
<CSVHeader>
<CsvColumn>MDCY_Code</CsvColumn>
</CSVHeader>
<CSVHeader>
<CsvColumn>MDCY_Description</CsvColumn>
</CSVHeader>
</CSVRoot>
I know that I can use flowr on this formed XML and remake the XML, but that's additional processing. Is there any way to do this without further processing ( such as using a flowr to form a new XML ) in one query?
This is what I've used:
select
utma.CsvColumn as [ColumnName]
from
Methods m
inner join
UITestMap utm on m.UITestMapID = utm.ID
inner join
SubMethods sm on m.Id = sm.HeaderID
inner join
UITestMapActions utma on sm.UITestMapActionID = utma.ID
for xml path('CSVHeader'),root('CSVRoot')
Edit 2 :
This isn't working, I get the same output.Utma has one column that I need in my query and that is CSVColumn.
Edit 3:
Now I'm getting this :
<CsvHeader>
<ColumnName>MDCY_Cod</ColumnName>
<ColumnName>MDCY_Descriere</ColumnName>
</CsvHeader>
Which is the correct answer.
I wasn't sure how I wanted to process the result sets, actually I will need to use flowr to create a new xml out of this output. ( which is what I needed in the first place)
Try this:
select
utma.CsvColumn
from
Methods m
for xml path(''),root('CSVRoot')

View from DB2 to SQL Server 2005

I'm attempting to move a view between DB2 and SQL Server.
CREATE VIEW msu.bad_bus_cnty_st_mstr
AS
SELECT id,
bus_cnty_cntry_cd,
bus_st,
bus_zip
FROM summit.mstr
WHERE ( bus_cnty_cntry_cd, bus_st ) IN (SELECT cnty_cntry_cd,
st
FROM uhelp.cnty_cntry_cd
WHERE
cnty_cntry_descr LIKE '%invalid%');
The view works in DB2, but doesn't work with SQL Server because of the WHERE clause. Can I have a recommendation on how to rewrite this view to work with SQL Server?
It usually helps to define what "doesn't work" means (e.g. what error did you get) and also to specify the version of SQL Server you are using.
Unfortunately SQL Server doesn't support IN() with more than one clause. However you can re-write your view this way:
ALTER VIEW msu.bad_bus_cnty_st_mstr
AS
SELECT id,
bus_cnty_cntry_cd,
bus_st,
bus_zip
FROM summit.mstr AS mstr
WHERE EXISTS
(
SELECT 1
FROM uhelp.cnty_cntry_cd
WHERE cnty_cntry_descr LIKE '%invalid%'
AND cnty_cntry_cd = mstr.bus_cnty_cntry_cd
AND st = mstr.bus_st
);
one way
CREATE VIEW msu.bad_bus_cnty_st_mstr
AS
SELECT id,
bus_cnty_cntry_cd,
bus_st,
bus_zip
FROM summit.mstr m
WHERE EXISTS( SELECT 1 FROM uhelp.cnty_cntry_cd c
WHERE c.cnty_cntry_descr LIKE '%invalid%'
AND c.bus_cnty_cntry_cd = m.bus_cnty_cntry_cd
AND c.st = m.bus_st)

Resources