Saving a decimal query result as a variable- Must declare the scalar variable (INNER JOINS, 2 DECIMAL PLACES, PRECISION AND SCALE SET) - sql

Calculating the avg:
SELECT
round(AVG(P.LiczbaUczniow/E.WypełnioneEtaty),2)
FROM [dbo].[UczniowieWojs] P
INNER JOIN EtatyWypełnioneWoj E ON E.WOJ=P.idTerytWojewodztwo
RESULT: 9.38
DECLARING THE VARIABLE:
DECLARE #ŚrednioUczniówNaNauczycielaPL decimal(3,2);
TRYING TO SAVE THE VARIABLE:
SELECT
#ŚrednioUczniówNaNauczycielaPL=round(AVG(P.LiczbaUczniow/E.WypełnioneEtaty),2)
FROM [dbo].[UczniowieWojs] P
INNER JOIN EtatyWypełnioneWoj E ON E.WOJ=P.idTerytWojewodztwo
Msg 137, Level 15, State 1, Line 305 Must declare the scalar variable
"#ŚrednioUczniówNaNauczycielaPL".
What I've tried to resolve it:
I've read the documentation of decimal datatype.
Google
Similar topics on stackoverflow - they all seem to refer to scale and precision which I think I got right. Yet, it's still wrong.
I've been working on it and sincerely cannot find the solution. I'd appreciate pointing me towards the answer.
Thanks

#Larnu, #stickybit, #AlwaysLearning pointed out that a variable must be in the same badge. And I was running the Declare and Select commands separately, which made them separate things. That was the reason for the error.
If I had used a GO command in between of these statements that would also have produced the erroneous result.
Semicolon turned out to be fine because it isn't a batch separator.
To sum it up - no 'go' between the statements and run them altogether.

Related

Error 148 Dimension different - The symbol is referenced with more/less

I am not able to sort out the Error 148 in GAMS Studio 39.3 for my problem.
148 Dimension different - The symbol is referenced with more/less indices as declared
following is the details:
Sets
EV 'EV unit'/ev1*ev1/
Asev1 'set ev index for values of sch' /1*2/
taev 'set of arrival'/taev1*taev1/
tdev 'set of departure'/tdev1*tdev1/
t ’time periods’ /t1*t2/
w ’scenarios’ /w1*w2/;
SCALAR
Pev ’EV power traded in the Energy market’/7.8/;
PARAMETER
Asev 'alphasch(ev) values of alphasch'
/1 0
2 1/;
PARAMETER
weigth(w) ’weight of scenarios’
/w1 0.05
w2 0.05/;
POSITIVE VARIABLES
As(ev,t,w) 'EV scheduling in time period t';
EQUATION
Pev1 'power output of EV'
Pev1(t,w).. Pev=e=sum(ev,Pev(ev,t,w)*Asev('ev,t,w'));
I am not sure where i am making mistake however i am getting Error 148.
Error 148 Dimension different - The symbol is referenced with more/less
**** indices as declared
Note: I got $148 error under w)*
Pev1(t,w).. Pev=e=sum(ev,Pev(ev,t,w)*Asev('ev,t,w'));
First, you probably don't want the quotes at the end, which makes ev,t,w one specific element. So, you should change it to this:
Pev1(t,w).. Pev=e=sum(ev,Pev(ev,t,w)*Asev(ev,t,w));
But still, this does not work. You defined Asev as 1-dimensional parameter with the elements 1 and 2 above. That does not fit your usage here. So, what do you actually intend? Either the definition of Asev must be wrong or your usage. Or did you actually want to use As here instead, like this?
Pev1(t,w).. Pev=e=sum(ev,Pev(ev,t,w)*As(ev,t,w));
This would resolve the problem for the last term, but give an error before, since also the use of Pev (with 3 indices here) does not match your definition as Scalar above.
Thanks for your feedback!
As or Asev is just the declaration.
Firstly, I removed the quotes at the end, which makes (ev,t,w)
and here it is
Pev1(t,w).. Pev=e=sum(ev,Pev(ev,t,w)*As(ev,t,w));
The problem for the last term is still existing.
I didn't get it you saying " the use of Pev (with 3 indices here) does not match your definition as Scalar above."
I think the Pev (with 3 indices here) match with the scalar above

OpenSQL uses additives that can only be used with a fixed point arithmetic flag?

I keep having an error message for the selection below. What I have done is creating a global structure, then declaring a structure and a table in the program as TYPE table, as it is seen below:
DATA: gt_add_data_08 TYPE TABLE OF zsd_s_z5_9910_9900_08,
gs_add_data_08 TYPE zsd_s_z5_9910_9900_08.
The selection that I have problem is below:
SELECT gt_add_data_08~bi_desc1,
gt_add_data_08~bi_desc2,
gt_add_data_08~herkl,
gt_add_data_08~herkl_t,
gt_add_data_08~tempb,
gt_add_data_08~tbtxt,
vbdpl~lfimg,
vbdpl~vrkme,
vbdpl-charg
INTO TABLE gs_add_data_08
FROM gt_add_data_08
INNER JOIN vbdpl ON
vbdpl~posnr = gt_add_data_08~posnr,
vbdpl~vbeln = gt_add_data_08~vbeln
WHERE vbdpl~spras = 'EN'.
The selection is supposed to join two tables, thus gt_add_data_08 and vbdpl into gs_add_data_08, with the condition that spras must be in English. The error that is show to me is:
This Open SQL statement uses additives that can only be used with a fixed point arithmetic flag enabled (e.g. CASE Expression, Host variables in expressions, ...)
May anyone know where the problem may be that is showing to me this error?
Thank you all in advance!

SQL Server Spatial Select Where STContains

I'm working on a spatial database SQL Server and am having a hard time querying a row where the geography contains a given lat/long.
I'm able to get this query to work:
DECLARE #polygon Geography;
select #polygon = (
select
geog4269
from census_tracts
WHERE namelsad10 = 'Census Tract 9801.02'
);
set #polygon = #polygon.ReorientObject();
select #polygon.STContains(
geography::Point(18.4102591, -66.0732014, 4269)
);
However, I want to be able to select the row that contains a given lat/long with something like the following:
select
*
from census_tracts
WHERE geog4269.ReorientObject().STContains(
geography::Point(18.4102591, -66.0732014, 4269)
) = 1
I'm getting a .NET Framework exception when I run that saying to use MakeValid to avoid it, but adding .MakeValid() doesn't fix the issue.
This is the exception message:
Msg 6522, Level 16, State 1, Line 1
A .NET Framework error occurred during execution of user-defined routine or aggregate "geography":
System.ArgumentException: 24144: This operation cannot be completed because the instance is not valid. Use MakeValid to convert the instance to a valid instance. Note that MakeValid may cause the points of a geometry instance to shift slightly.
System.ArgumentException:
at Microsoft.SqlServer.Types.SqlGeography.ThrowIfInvalid()
at Microsoft.SqlServer.Types.SqlGeography.ReorientObject()
.
When I use the following query:
select
*
from census_tracts
WHERE geog4269.MakeValid().ReorientObject().STContains(
geography::Point(18.4102591, -66.0732014, 4269)
) = 1
The geographies don't get reoriented (every geography says it contains all points).
Has anyone run into something similar before or can point out where I'm going wrong? Thanks for the help!
My guess is for some geographies MakeValid also fixes the orientation of the polygons. If the polygons is invalid, MakeValid has to make a guess about what was the intended shape, and fix it using some heuristics. The results may vary depending on what exactly was wrong with the data - and sometime garbage in, garbage out applies.
I would avoid using both MakeValid() and ReorientObject() in the query. This is both error prone, and slow (as it prevents spatial index usage).
Instead, fix the actual data by updating the geographies to be the intended ones.
Quick and dirty way is to invert only those that need inverting, something like
update census_tracts
set geog4269 =
IF (geog4269.MakeValid().STArea() < 1e14, -- is it small?
geog4269.MakeValid(),
geog4269.MakeValid().ReorientObject());

trouble with the division of two floats C++/CLI

I'm having problems with dividing two float values that appears to be a simple process.
http://imgur.com/EZz5Q1Z
the floats l and larg have the values of 20 and 10, which should result in a n1 value of 2. That's not the case though.
Thanks in advance.
You put your breakpoint on the line containing the division, which means the debugger is showing the value of the variable before the division takes place. Step to the next instruction to see the correct value.

How to search a image/varbinary field for records that start with a binary pattern

I am trying to find all images that do not start with the magic number ff d8 ff e0 (the signature for jpg) According to the MSDN I should be able to use patindex on my data. However
SELECT TOP 1000 [cpclid]
FROM [cp]
where patindex('FFD8FFE0%', cpphoto) = 0 -- cpphoto is a column type of image
gives me the error
Msg 8116, Level 16, State 1, Line 1
Argument data type image is invalid for argument 2 of patindex function.
What would be the correct way to find records that do not match the magic number of ff d8 ff e0?
UPDATE:
Here is a link to test any suggestions you have.
I Ross's solution worked in the end with some tweaking on what the query.
SELECT [cpclid]
FROM [cp]
where convert(varchar(max), cast(cpphoto as varbinary(max))) not like convert(varchar(max), 0xFFD8FFE0 ) + '%'
I found a even better solution, see my answer.
I found a much simpler solution that runs a lot faster.
SELECT [cpclid]
FROM [cp]
where cast(cpphoto as varbinary(4)) <> 0xFFD8FFE0
Why are you still using the IMAGE data type? It has been deprecated in favor of VARBINARY(MAX)... if you convert your column to VARBINARY(MAX) I think you'll find it a lot easier to work with.
EDIT
In SQL Server 2008 you can use a much easier convert:
SELECT CONVERT(VARCHAR(MAX), CONVERT(VARBINARY(MAX), cpphoto), 2) FROM cpphoto;
In fact this worked just fine on your StackExchange query (I suspect the back end is not using SQL Server 2005).
But I'm glad my answer was so useless to you. Noted to self.
Use where cpphoto not like 'FFD8FFE0%' in your where clause.
cast cpphoto as a varchar(max) if it is not a string already.