Is it possible to simulate the cross apply function?

  • 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 , 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

  • 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

  • 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.

  • 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

  • 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

  • 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.

  • 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

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Along with some other fields from my table, the resulting row will include information derived from parsing a string field. A sample of the string field that needs to be parsed looks like this:

    version=0200&Paynet_id=48580744&payment_comprehensive=1&payment_summary=0&business_background=0&trade_summary=0&trade_detail=0&public_filings=0&financial_relationships=0&ucc_filings=0&transpo_score=0&office_score=0&user=cjwall%40acra.com&password=

    It is delimited by '&'. The words on the left of the '=' sign are the columns, the value is to the right of the '=' sign. This field can vary, so I am making static columns and populating them if they happen to appear in the string.

  • Cool... would you mind posting your function... we may be able to make an improvement here and there.

    --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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Hello,

    Because of the problems with the database compatibility (not having the option of change to 90 or creating other databases), I tried to accomplish this without a cross apply. I am using a cursor instead. But for some reason, I am still only getting one row of data. Can you see why? Here is my code:

    DECLARE @iReturnCode int,

    @iNextRowId int,

    @iCurrentRowId int,

    @iLoopControl int,

    @phra_request_querystring nvarchar(4000)

    DECLARE QueryCursor CURSOR FOR

    SELECT subid,

    queryString

    FROM #parseString -- THIS IS MY INPUT TABLE

    SELECT @iLoopControl = 1

    SELECT @iNextRowId = MIN(iRowId)

    FROM #parseString

    IF ISNULL(@iNextRowId,0) = 0

    BEGIN

    SELECT 'No data in found in table!'

    RETURN

    END

    SELECT @iCurrentRowId = iRowId,

    @subid = subid,

    @phra_request_querystring = queryString

    FROM #parseString

    WHERE iRowId = @iNextRowId

    WHILE @iLoopControl = 1

    BEGIN

    --===== Simulate a passed parameter

    DECLARE @Parameter VARCHAR(8000),@Value varchar(100)

    SET @Parameter = @phra_request_querystring

    --===== Suppress the auto-display of rowcounts to keep them from being

    -- mistaken as part of the result set.

    SET NOCOUNT ON

    --Drop table #Element

    --===== Add start and end commas to the Parameter and change all "group"

    -- delimiters to a comma so we can handle all the elements the same way.

    SET @Parameter = ','+REPLACE(@Parameter,'&',',') +','

    --===== Join the Tally table to the string at the character level and

    -- when we find a comma, insert what's between that comma and

    -- the next comma into the Elements table. CTE does the split...

    ;WITH

    cteSplit AS

    (

    SELECT ROW_NUMBER() OVER (ORDER BY N)-1 AS RowNumber,

    SUBSTRING(@Parameter,N+1,CHARINDEX(',',@Parameter,N+1)-N-1) AS Element

    FROM dbo.Tally

    WHERE N < LEN(@Parameter)

    AND SUBSTRING(@Parameter,N,1) = ',' --Notice how we find the comma

    )

    --==== ... and this puts the data back together in a fixed table format

    -- using classic Cross-Tab code. It also converts columns that are

    -- supposed to be numeric

    SELECT substring(Element, 0, (PATINDEX('%=%', Element))) ElementTitle,

    substring(Element, (PATINDEX('%=%', Element)+1), len(Element)-(PATINDEX('%=%', Element)+0))ElementValue

    --into #Element

    FROM cteSplit

    --GROUP BY RowNumber, ELement

    SELECT @iNextRowId = NULL

    -- get the next iRowId

    SELECT @iNextRowId = MIN(iRowId)

    FROM #parseString

    WHERE iRowId > @iCurrentRowId

    -- did we get a valid next row id?

    IF ISNULL(@iNextRowId,0) = 0

    BEGIN

    BREAK

    END

    -- get the next row.

    SELECT @iCurrentRowId = iRowId,

    @subid = subid,

    @phra_request_querystring = queryString

    FROM #parseString

    WHERE iRowId = @iNextRowId

    END

    RETURN

  • tempdb is always going to run under the native compatibility mode. Add a Use tempdb statement before you call the CROSS APPLY and change the context back when done.

  • Christopher Kutsch (9/12/2014)


    tempdb is always going to run under the native compatibility mode. Add a Use tempdb statement before you call the CROSS APPLY and change the context back when done.

    Note that this is a 5 year old thread.

    Your suggestion might be questionable.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 15 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic. Login to reply