dbTalk Databases Forums  

Set variable to value in column

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Set variable to value in column in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mcolson
 
Posts: n/a

Default Set variable to value in column - 07-26-2007 , 03:08 PM






Is it possible to set a variable in a stored procedure equal to a
value from a column when that column's respective ID is equal to
max(id)-1


ID A B
1 24 24
2 53 29
3 76 47
4 32 32

What I am trying to do is update A in the last column to be equal to B
from the 4th row, plus A from the 3rd row. If I could set a variable
equal to A in row 3 (in this case 76) I could easily add the variable
to the existing value.

I've tried something like this before. I'm sure some of you will say
right away, "of coarse that doesn't work". I'm new to sql.

Set A = A + select( B from mytable where ID = (select (max(ID)-1) from
mytable))
where ID = (select (max(ID)) from mytable)

Thanks

Matt


Reply With Quote
  #2  
Old   
Roy Harvey
 
Posts: n/a

Default Re: Set variable to value in column - 07-26-2007 , 03:46 PM






I think you have columns and rows confused in your first paragraph. I
also can't quite figure out what you mean by variables, as A is both
(apparently) a column in the table and something you are trying to
SET.

This query might give you something to play with.

SELECT X.A +
(SELECT X
FROM YourTable as Y
WHERE Y.ID =
(select max(ID) FROM YourTable as Z
where Z.ID < A.ID))
FROM YourTable as X
WHERE A.ID =
(select max(ID) FROM YourTable)

Roy Harvey
Beacon Falls, CT

On Thu, 26 Jul 2007 20:08:43 -0000, mcolson <mcolson1590 (AT) gmail (DOT) com>
wrote:

Quote:
Is it possible to set a variable in a stored procedure equal to a
value from a column when that column's respective ID is equal to
max(id)-1


ID A B
1 24 24
2 53 29
3 76 47
4 32 32

What I am trying to do is update A in the last column to be equal to B
from the 4th row, plus A from the 3rd row. If I could set a variable
equal to A in row 3 (in this case 76) I could easily add the variable
to the existing value.

I've tried something like this before. I'm sure some of you will say
right away, "of coarse that doesn't work". I'm new to sql.

Set A = A + select( B from mytable where ID = (select (max(ID)-1) from
mytable))
where ID = (select (max(ID)) from mytable)

Thanks

Matt

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.