Update using Sum -Error - sql

Keep on getting error
you tried to execute a query that does not include the specified expression
My SQL looks as follows:
UPDATE TblField LEFT JOIN TblTempStats ON TblField.DomainCatID = TblTempStats.DomainCatID
SET TblTempStats.EmptyFields = Sum(IIf([fieldname] Is Null,1,0));
Any ideas as to why?

You should use a domain aggregate for this in my opinion, it avoids the error:
UPDATE TblTempStats
SET TblTempStats.EmptyFields =
DSum(
"IIf([fieldname] Is Null,1,0)",
"TblField",
IIf(
TblTempStats.DomainCatID Is Null,
"TblField.DomainCatID Is Null",
"TblField.DomainCatID = " & TblTempStats.DomainCatID
)
)

Related

The multi-part identifier "-" could not be bound

I have this SQL:
SELECT
dbo.ARTRNTBL.Item_code, dbo.ARTRNTBL.Item_name, dbo.FAFPHTBL.rate_value,
dbo.FAFPHTBL.quantity, dbo.FAFPHTBL.total, dbo.FAFPHTBL.TIN, dbo.EGRDGTBL.return_type
FROM
dbo.FAFPHTBL
INNER JOIN
dbo.ARTRNTBL
INNER JOIN
dbo.EGRDGTBL ON dbo.FAFPHTBL.Item_code = dbo.ARTRNTBL.Item_code
ON dbo.ARTRNTBL.Item_code = dbo.EGRDGTBL.Item_code
WHERE
dbo.FAFPHTBL.invoice_no = '" & TextBox39.Text & "'"
I keep getting this error:
The multi-part identifier "dbo.FAFPHTBL.Item_code" could not be bound.
How can I fix this?
The problem is the way the join expressions are nested. At the time dbo.FAFPHTBL.Item_code is used, it's part of this expression:
dbo.ARTRNTBL INNER JOIN dbo.EGRDGTBL ON dbo.FAFPHTBL.Item_code = dbo.ARTRNTBL.Item_code
Notice the dbo.FAFPHTBL object is not included with this expression.
You probably want this instead:
SELECT dbo.ARTRNTBL.Item_code, dbo.ARTRNTBL.Item_name, dbo.FAFPHTBL.rate_value,
dbo.FAFPHTBL.quantity, dbo.FAFPHTBL.total, dbo.FAFPHTBL.TIN, dbo.EGRDGTBL.return_type
FROM dbo.FAFPHTBL
INNER JOIN dbo.ARTRNTBL ON dbo.FAFPHTBL.Item_code = dbo.ARTRNTBL.Item_code
INNER JOIN dbo.EGRDGTBL ON dbo.ARTRNTBL.Item_code = dbo.EGRDGTBL.Item_code
WHERE dbo.FAFPHTBL.invoice_no = #InvoiceNumber
While I'm here:
DO NOT USE STRING CONCATENATION LIKE THAT TO INCLUDE A TEXTBOX IN THE QUERY!
And yes, I know I'm shouting, because it's that important. If you're not using parameterized queries, you're practically begging for problems with your app, some of them pretty serious security issues. If you set me down in front of your app, I could easily delete all your data or elevate my permission to admin level.

SQL Update statement not working - SQL Server

I am trying to run the below sql statement (SQL Server), however getting the error
"FROM clause in UPDATE and DELETE statements cannot contain subquery sources or joins."
update fp
set fp.totalcapacity = hc.totalcapacity,
fp.sellablecapacity = hc.sellablecapacity
from [fact].[FinalPosition] fp
join fact.[HotelCapacity] hc
on fp.hotelkey = hc.hotelkey
and fp.staydate = hc.staydate
where fp.staydate = '2016-06-18'
I can't seem to understand why I am getting this error. Any idea?
I think the syntax you want is:
update fp
set totalcapacity = hc.totalcapacity,
sellablecapacity = hc.sellablecapacity
from fp join
fact.[HotelCapacity] hc
on fp.hotelkey = hc.hotelkey and fp.staydate = hc.staydate
where fp.staydate = '2016-06-18';
If you want fp to refer to an actual table, include that in the from clause and make the fp the alias for the table.

Not Updating table when everything right and has null fields. when erase the null fields then it works ?

I have a table (fldAbsentes2) with 6 columns([fldEmployeeID],[fldAbsentDate],[fldAbsentCode]
,[fldRuleViolationWarningType]
,[fldRuleViolationIssueDate]
,[fldLOAEndDate]
When I use the fallowing querystring with sqlcommand it doesnt give me an error but it doesn't update the table.
multstring = "Update tblAbsences2 " & _
"Set fldEmployeeID = '103', " & _
"fldAbsentDate = '1/2/2013', " & _
"fldAbsentCode = 'X', " & _
"fldRuleViolationWarningType = NULL, " & _
"fldRuleViolationIssueDate = NULL, " & _
"fldLOAEndDate = NULL " & _
"Where (fldEmployeeID ='100') AND " & _
"(fldAbsentDate ='1/2/2013') AND " & _
"(fldAbsentCode ='NA') AND " & _
"(fldRuleViolationWarningType = NULL) AND " & _
"(fldRuleViolationIssueDate = NULL) AND " & _
"(fldLOAEndDate = NULL)"
If I erase the last three lines (avoiding the where fldname = NULL) And it works. The problem is that the user should be able to update those values too if they are Null. The same thing happens when i use sql Server management studio. Below is also the code on sql managment VS It would only work if I erase the where fldname = NULL. Any help would be appreciated
UPDATE [DBName].[dbo].[tblAbsences2]
SET fldAbsentCode = 'works', fldEmployeeID = '1', fldAbsentDate = '1/2/2013 00:00:00', fldRuleViolationWarningType = NULL, fldRuleViolationIssueDate = NULL,
fldLOAEndDate = NULL
WHERE (fldEmployeeID = '1') AND (fldAbsentDate = '1/2/2013 00:00:00') AND (fldAbsentCode = 'test') AND (fldRuleViolationWarningType = NULL) AND
(fldRuleViolationIssueDate = NULL) AND (fldLOAEndDate = NULL)
The operator for NULL comparisons in SQL is IS and not = (similarly IS NOT instead of <> for inequality).
Change your queries to use WHERE column IS NULL instead of WHERE column = NULL.
#dai is correct about IS NULL vice = NULL. That shoudl fix the first part of waht is worng with your query. But without seeing the data, I cannot predict if it will fix everything that is wrong. It coudl also be that you simply don't have any records that meet all of those conditions. You may need to add test records on dev to adequately test the accuracy of a query.
I also wanted to provide a technique to help you understand why a query is going wrong, so you can fix it without having to resort to the Internet.
UPDATE [DBName].[dbo].[tblAbsences2]
SET fldAbsentCode = 'works'
, fldEmployeeID = '1'
, fldAbsentDate = '1/2/2013 00:00:00'
, fldRuleViolationWarningType = NULL
, fldRuleViolationIssueDate = NULL,
fldLOAEndDate = NULL
--select * from [DBName].[dbo].[tblAbsences2]
WHERE (fldEmployeeID = '1')
AND (fldAbsentDate = '1/2/2013 00:00:00')
AND (fldAbsentCode = 'test')
AND (fldRuleViolationWarningType = NULL)
AND (fldRuleViolationIssueDate = NULL)
AND (fldLOAEndDate = NULL)
You will see that I have a select embedded in the update that is commented out. Run just the select part and you will see the results of what you would be affecting with the update. Look at the records to see if they make sense. In this case you can eliminate the last 3 where conditions ( or other suspect conditions for other queries)and see what the values for those fields are in the database. If they are null, then you know there is something wrong with how you are checking for a null. If they are not, then it could be the data doesn't actually give any results with all of these conditions. To check then add some records (in dev only!!) to add the conditions you are looking for and then run the select and see if you get the correct answer.
It is irrelevant to this problem but when checking for the accuracty of a query returning the correct results, you may need to also drop off joins or change them temporarily to left joins to see what is happening with the data that is causing your problem.
You should never create an update or delete query without checking what the select for that query would return. The meaning of the data drives the query syntax, if you don't know exactly what records you are affecting, you cannot know if you have correctly written the query. As you see in this case, the query runs, it doesn't have a syntax error. It is simply not returning the correct results. Whether something passes a syntax check does not mean it does what it should be doing. You need to learn to query by meaning.

Convert SQL Server Query to MS Access to Update Table

Could anyone help with this. I don't use Access often, but this process I'm building needs to utilize Access for the business to use. I have the following code, which will not work in Access. I keep getting the error 'Syntax Error (missing operator) in query expression'. Would anyone be able to convert this query into something Access will accept.
UPDATE Auto_DailyDiary_PrimaryTbl a
SET a.TotalDiary = sub.TotalDiary
FROM
(
SELECT CaseEEID, Count(CaseID) as TotalDiary
FROM dbo_Case
WHERE CaseStatus <> 6
GROUP BY CaseEEID
) sub
WHERE sub.EEID = a.EEID AND a.DiaryDt = Date()
Pretty sure Access doesn't like having a joined group by subquery in an update query.
Maybe try something like this?
UPDATE Auto_DailyDiary_PrimaryTbl
SET TotalDiary =
DCOUNT('CaseID', 'dbo_Case',
"CaseStatus <> 6 AND " &
"CaseEEID = '" & Auto_DailyDiary_PrimaryTbl.EEID & "'")
WHERE DiaryDt = Date()

ebean query with subquery

I'm trying to do using EBean the equivalent of
select * from myTable1 where id not in (select id2 from myTable2) ;
I have no reference of table1 Object in table2 Object and the same the other way around.
Does anyone knows how to that using EBean ?
For the moment all I have is :
List<MyTable1> myResult = MyTable1.find.where().eq("id","1" ).findList();
Thanks.
C.C.
Apparently it has been possible to do this since 2009 using the example given in this bug report:
http://www.avaje.org/bugdetail-92.html
The example:
Query<Product> subQuery =
Ebean.createQuery(Product.class)
.select("sku")
.where().idEq(4).query();
List<MinCustomer> list = Ebean.find(MinCustomer.class)
.where().in("name", subQuery)
.findList();
However:
I am unable to make it work because the SQL generated is invalid. Seemingly due to a string replacement happening behind the scene in Ebean where (for me at least) the table name in the subquery is lost.
I expect it has to do with my main query includes a reference to the table from which my subquery "is selecting".
Turning the valid SQL from the example:
select c.id, c.name, c.notes
from o_customer c
where (c.name) in (select p.sku from o_product p where p.id = ? )
... into this invalid SQL in my case:
select t0.id as c0, ... t0.location_id as c8
from myRecordClass t0
where (t0.location_id) in (
select t0.id
from t0.location_id t0 # should be: from location t0
where t0.customer_id = ?
) and t0.creation > ?
order by t0.creation desc
The workaround:
Use the RawSql approach like in https://stackoverflow.com/a/27431625/190599 - example here:
String sql = "select b.id, b.location_id ... " +
"from myRecordClass b " +
"where location_id in (" +
"select id " +
"from location " +
"where customer_id = " + user.getCustomer().getId() +
") " +
"order by creation desc limit 10";
RawSql rawSql = RawSqlBuilder
.parse(sql)
.columnMapping("b.id", "id")
.columnMapping("b.location_id", "location.id")
....
.create();
Query<MyRecordClass> query = Ebean.find(MyRecordClass.class);
query.setRawSql(rawSql);
final List<MyRecordClass> list = query.findList();
I hardly believe that RawSql is fastest way to achieve this kind of query, it allows you to return list of mapped objects.
It's also possible to use SqlQuery (described in Reference guide (PDF)) to fetch a list of SqlRows - so you can find required data without any mapping at all.