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

Simple Program Expand / Collapse
Author
Message
Posted Saturday, June 15, 2013 12:47 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 24, 2013 12:22 PM
Points: 24, Visits: 105
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.

Year Month Day Hour Minute Second MilisecondAsk Bid AskVolumeBidVolume
2013 01 02 01 33 29 095 1.32277 1.32264 1.5 2.48
2013 01 02 01 33 29 955 1.32277 1.32264 1.5 2.48
2013 01 02 01 33 30 315 1.32277 1.32266 1.5 1.5
2013 01 02 01 33 30 859 1.32277 1.32264 1.5 2.48
2013 01 02 01 33 31 325 1.32277 1.32266 1.5 1.5
2013 01 02 01 33 31 625 1.32277 1.32266 1.5 2.25
2013 01 02 01 33 32 506 1.32277 1.32266 1.5 1.5
2013 01 02 01 33 32 855 1.32277 1.32266 1.5 1.5
2013 01 02 01 33 36 865 1.32278 1.32264 1.5 2.48
2013 01 02 01 33 53 035 1.32278 1.32266 2.25 1.5
2013 01 02 01 33 55 105 1.32278 1.32266 2.25 1.5
2013 01 02 01 33 56 096 1.32278 1.32266 2.25 1.5
2013 01 02 01 34 04 022 1.32275 1.32266 2.25 1.5
2013 01 02 01 34 04 216 1.32274 1.32263 1.35 1.88
2013 01 02 01 34 04 754 1.32275 1.32263 1.5 1.88

USE
EURUSD
GO

DECLARE @Status char(6),
@Vol float(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----------------------------------

Volume Type
NULL OK
2.25 NULL
1.88 NULL
1.65 NULL
2.4 NULL
1.65 NULL
1.5 NULL
1.5 NULL
1.65 NULL
1.65 NULL



Post #1463826
Posted Saturday, June 15, 2013 3:59 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, November 6, 2014 5:01 AM
Points: 2,116, Visits: 6,441
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)
Post #1463906
Posted Saturday, June 15, 2013 4:01 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 2:38 AM
Points: 7,850, Visits: 9,600
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
Post #1463907
Posted Saturday, June 15, 2013 4:03 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, November 6, 2014 5:01 AM
Points: 2,116, Visits: 6,441
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)
Post #1463908
Posted Saturday, June 15, 2013 9:48 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 24, 2013 12:22 PM
Points: 24, Visits: 105
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.
Post #1463924
Posted Sunday, June 16, 2013 1:00 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, November 6, 2014 5:01 AM
Points: 2,116, Visits: 6,441
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)
Post #1463930
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse