SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Simple Program


Simple Program

Author
Message
mcertini
mcertini
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 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
Jan Van der Eecken
Jan Van der Eecken
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2452 Visits: 6494
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)
Tom Thomson
Tom Thomson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14402 Visits: 12213
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

Jan Van der Eecken
Jan Van der Eecken
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2452 Visits: 6494
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)
mcertini
mcertini
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 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.
Jan Van der Eecken
Jan Van der Eecken
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2452 Visits: 6494
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search