Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Is it possible to simulate the cross apply function? Expand / Collapse
Author
Message
Posted Monday, March 23, 2009 9:18 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, September 28, 2010 8:59 AM
Points: 21, Visits: 62
My database in SQL 2005 has a compatibility of 80. I cannot change the compatibility level on the database because of older applications. I need to simulate the cross apply and probably the PIVOT in my code. Is it possible?

Here is my code:

SELECT [user], version, paynet_id, payment_comprehensive, payment_summary, business_backround,
trade_summary, trade_detail, public_filings, financial_relationships, ucc_filings, transpo_score,
master_score, legal_name, agri_score, construction_score
FROM
(
SELECT phra_subid,SELECT substring(f.Val, 0, (PATINDEX('%=%', f.Val))) AS Col,substring(f.Val, (PATINDEX('%=%', f.Val)+1), len(f.Val)-(PATINDEX('%=%', f.Val)+0)) AS Val
FROM from dbo.phr_audit a
CROSS APPLY dbo.ParseValues(a.phra_request_querystring,'&') f
where (@subid = -1 or phra_subid = @subid)
and (phra_auditdt between @from and @thru)
and (@reqUrl = 'ALL' or phra_request_url = @reqURL)
and phra_request_url <> '/phr_generate.asp'
)m
PIVOT
(
MAX(Val) FOR Col IN (user, version, paynet_id, payment_comprehensive, payment_summary, business_backround,
trade_summary, trade_detail, public_filings, financial_relationships', ucc_filings, transpo_score,
master_score, legal_name, agri_score, construction_score)
)p
Post #681532
Posted Monday, March 23, 2009 9:20 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
I can't tell what the function does. Is it something that could be turned into a correlated sub-query?

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #681536
Posted Monday, March 23, 2009 9:27 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, September 28, 2010 8:59 AM
Points: 21, Visits: 62
It is taking parameters from an application and getting information to display to an aspx report. The problem is one of the fields (phra_request_querystring) is a string that has to be broken up into several fields to populate the columns within the report, delimited by '&'. The field can vary in content. The column names are included in the string. So it has to also parse the results to get the column name and the value.
Post #681550
Posted Monday, March 23, 2009 9:30 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
I think the only way you're going to get SQL 2000 to do that is with dynamic SQL.

You can parse out a string with a Numbers table, but that won't do dynamic column names directly.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #681556
Posted Monday, March 23, 2009 9:31 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
Here's something you might try. I have no idea if this will work, but it might be worth a try.

Create a separate database that in Compat 90. Write the query in there, using 3-part names so it queries from the Compat 80 database. Then, in the Compat 80 database, write a query that selects from the Compat 90 database.

Might work, might not. Probably worth a try.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #681558
Posted Monday, March 23, 2009 10:22 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, September 28, 2010 8:59 AM
Points: 21, Visits: 62
So are you saying that there is no way of simulating the cross apply logic? I don't really have the option of compatibility 90.
Post #681624
Posted Monday, March 23, 2009 11:41 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
There are ways to simulate cross apply. You have to build a query that does what the function does, but which doesn't have input parameters. Then you include the "parameters" as columns in its output, and join to it on those. That's often more efficient than Cross Apply, but it won't do dynamic columns, which is what you're looking for. Actually, I'm not sure how to get Cross Apply to do dynamic columns, so maybe I'm misunderstanding what you need.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #681685
Posted Monday, March 23, 2009 4:53 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:03 PM
Points: 36,983, Visits: 31,508
I don't believe any of that is necessary in either 2000 or 2005, but I can't tell without some sample data ... please see the link in my signature below before you attempt posting sample data, please.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #681966
Posted Monday, March 23, 2009 5:08 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 4:16 PM
Points: 4,389, Visits: 9,526
Why don't you put the code using the CROSS APPLY in a database that is in 90 compatibility and use three-part naming?

I would also recommend that you read the article in my signature and post the actual problem you are trying to resolve. You will most likely get a much better solution than the one you currently have that will work in both 80 and 90 compatibility mode.


Jeffrey Williams
Problems are opportunites brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs
Post #681975
Posted Monday, March 23, 2009 9:23 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:03 PM
Points: 36,983, Visits: 31,508
vwilliams (3/23/2009)
It is taking parameters from an application and getting information to display to an aspx report. The problem is one of the fields (phra_request_querystring) is a string that has to be broken up into several fields to populate the columns within the report, delimited by '&'. The field can vary in content. The column names are included in the string. So it has to also parse the results to get the column name and the value.


A 2 dimensional "array" like that is a virtual piece of cake... please read the following article... there is no need for CROSS APPLY nor even the overhead of a UDF...

http://www.sqlservercentral.com/articles/T-SQL/63003/


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #682034
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse