Simple Program

  • I am in the process of learning how to put together simple programs to iterate over a table dataset and report various information from that table. To do this I am using a temporary table. I though have not figured out yet how to program a second pass through the temporary table. Can someone help me with the principles I am missing in this. I am working through a book but haven't gotten there yet. Just a little impatient and want to know the solution before reading through it. Here is my dataset, code and results. My results do not reflect sorted results which I will correct, I would like to know why I cannot get the "TYPE" field populated.

    YearMonthDayHourMinuteSecondMilisecondAskBidAskVolumeBidVolume

    201301020133290951.322771.322641.52.48

    201301020133299551.322771.322641.52.48

    201301020133303151.322771.322661.51.5

    201301020133308591.322771.322641.52.48

    201301020133313251.322771.322661.51.5

    201301020133316251.322771.322661.52.25

    201301020133325061.322771.322661.51.5

    201301020133328551.322771.322661.51.5

    201301020133368651.322781.322641.52.48

    201301020133530351.322781.322662.251.5

    201301020133551051.322781.322662.251.5

    201301020133560961.322781.322662.251.5

    201301020134040221.322751.322662.251.5

    201301020134042161.322741.322631.351.88

    201301020134047541.322751.322631.51.88

    USE

    EURUSD

    GO

    DECLARE @status char(6),

    @Volfloat(5)

    DECLARE @TestTable TABLE (Type char(6), Volume float(4))

    BEGIN

    INSERT INTO @TestTable (Volume)

    SELECT BidVolume

    FROM EURUSD#1

    END

    BEGIN

    SELECT @Vol = Volume FROM @TestTable

    IF @VOL > 0

    INSERT INTO @TestTable (Type)

    VALUES('OK')

    ELSE

    INSERT INTO @TestTable (Type)

    VALUES('NOTOK')

    SELECT Volume, Type

    FROM @TestTable

    END

    Query Results----------------------------------

    VolumeType

    NULLOK

    2.25NULL

    1.88NULL

    1.65NULL

    2.4NULL

    1.65NULL

    1.5NULL

    1.5NULL

    1.65NULL

    1.65NULL

  • I'm guessing here because your requirements aren't quite clear. What you seem to want to do is to set the Type column to either 'OK' or 'NOTOK', based on whether the Volume column is greater than zero or not. But what you are doing with the

    SELECT @Vol = Volume FROM @TestTable

    is getting the Volume of an arbitrary row in @TestTable, and then adding a new row to @TestTable, where you only insert a value for the Type column. What you really intend to do (if I am not mistaken) can be accomplished like this:

    UPDATE @TestTable

    SET Type =

    CASE

    WHEN Volume > 0 THEN 'OK'

    ELSE 'NOTOK'

    END

    This code hasn't been tested, but it should do what I understand it should do.

    BTW, it always helps adding a set of DDL statements that create test tables, plus a set of INSERT statements with test data, so that we (being volunteers) don't have to spend extra time creating those ourselves in order to help you out.

    Regards,

    Jan

    --------------------------------------------------------------------------
    A little knowledge is a dangerous thing (Alexander Pope)
    In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)[/url]

  • basically this chunk of code

    SELECT @Vol = Volume FROM @TestTable

    IF @VOL > 0

    INSERT INTO @TestTable (Type)

    VALUES('OK')

    ELSE

    INSERT INTO @TestTable (Type)

    VALUES('NOTOK')

    will insert one row into @TestTable, which will have NULL in the Volume column (because you insert doesn't specify a value for that column) and either OK or NOTOK in the type column (depending on what the last record the select statement found in the table had in that column).

    If you change that code to something like

    UPDATE @TestTable

    Set Type '=CASE when @Vol > 0 THEN 'OK' ELSE 'NOTOK' END

    -- where 1 = 1 note that this line is commented out

    SO (1) specify which rows you want to change in the UPDATE statement (here it's all rows, which is the default, so you don't need a WHERE clause).

    AND (2) if you are trying to update rows you already inserted (as you are here) use UPDATE, not INSERT (which inserts new rows).

    Tom

  • Hehe, I beat you to it Tom 🙂

    --------------------------------------------------------------------------
    A little knowledge is a dangerous thing (Alexander Pope)
    In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)[/url]

  • Jan Van der Eecken & L'Eomot Inverse'

    Thank you for your time in answering my request and the clear direction.

    In future posts I will follow forum protocol. Everyone's time is valuable.

  • Glad it helped you out 😉

    --------------------------------------------------------------------------
    A little knowledge is a dangerous thing (Alexander Pope)
    In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)[/url]

Viewing 6 posts - 1 through 5 (of 5 total)

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