0

I need help with declaring a variable in SQL Server. This variable is supposed to be an int that is calculated with the values from each row for the columns of dblelectrodelength and dblelectrodespacing. How would I go about doing this?

Declare @T0 int = ((dblelectrodelength - 1) +  0 * (dblelectrodespacing + dblelectrodelength) + (dblelectrodelength / 2)) 
3
  • Use a subquery DECLARE... = (SELECT col + col) etc. Keep in mind, variables only hold a single value. It seems like you may be intending to calculate this across multiple rows, which would only work in a loop with this approach (there are better approaches if that is the case, but we need more info). Commented Mar 27, 2018 at 21:05
  • 1
    I know this SQL and all, but my recollection is that 0*anything=0. :) Commented Mar 27, 2018 at 21:19
  • Integer Variables hold single values (scalars). You appear to want to put a whole set of values in to a variable. Do you mean that you want a table with an integer column (possibly a table variable)? The best suggestion I can make is to provide a bit more information; what are you going to do with this? Show us from beginning to end what you want; some sample data, the processing you want to do, and the results that you want at the end. This, so far, is too abstract, with an inherent misunderstanding/inconsistency... Commented Mar 28, 2018 at 5:44

1 Answer 1

1

A variable holds a single value, you need multiple values (one for each row). You can then use a table variable:

Declare @T0 table (value int);
insert into @T0 (value)
select ( (dblelectrodelength-1) + (dblelectrodelength/2) )
from myTable;

Note: Removed the unnecessary + 0*(...) part.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.