Home Forums SQL Server 2008 SQL Server 2008 - General Stored procedure execution from within a data retrieval select statements. RE: Stored procedure execution from within a data retrieval select statements.

  • bkatoch (10/30/2012)


    The end result of this task is to track inventory movements by quarter. I realize my approach to this task is not very efficient but it's the only way I could think of at this time. Old habits sometimes die hard.

    If you could point me in the right direction, that would be most helpful.

    Thanks again.

    So we're talking apples to apples, and since you seem like a nice enough guy to keep around, when we request DDL and data we're looking for directly runnable data, not just a formatted set, like so:

    CREATE TABLE #GPM

    (PPMID VARCHAR(2) NOT NULL,--Record ID

    PCMPNY NUMERIC( 2, 0) NOT NULL,-- Company Number

    PYear Numeric( 4, 0) NOT NULL,-- Fiscal Year

    Period NUMERIC( 2,0) NOT NULL,-- Fiscal Period

    PStart NUMERIC(8,0) NOT NULL,-- Period Start Date

    PEnd NUMERIC(8,0) NOT NULL-- Period End Date

    )

    INSERT INTO #GPM

    SELECT'PM', 20, 2012, 1, 20120102, 20120129 UNION ALL

    SELECT'PM', 20, 2012, 2, 20120130, 20120326

    Now, to move past that. SQL Server has the DATETIME (and DATE) datatypes, and you'll find they're usually more effective for this than Numerics are. However, that said, you don't need an extra function for your dates, all you need is another column in your GPM table indicating the quarter. It'll repeat for the necessary rows the same way Fiscal Year would.

    From there, you're looking at joining the dates from the table into the GPM table. Now, usually a range isn't the best of choices for direct joins like this but I'm not sure how much data you have coming off the primary table, and this table seems small enough that doing a between join shouldn't hurt you much. I'd avoid the function entirely and adjust the ranged calendar table you have here. I'd personally also switch PStart and PEnd into DATETIMEs.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA