C# keeps coming up with an error:
Additional information: You have an error in your SQL syntax
My SQL statement is at the bottom in the picture.
Sql statement:
Update amazon_ordred_items
Set OrderItemId = 666854391288218,
SellerSKu = ..,
Title = 2pcs Fashion Cool Universal Black Real Original Car Headlight Eyelashes Sticker,
QuantityOrdered = 1,
QuantityShipped = 1,
CurrencyCode = USD,
Amount = 0.50,
ScheduledDeliveryEndDate = ..,
ScheduledDeliveryStartDate = ..,
PromotionIds = .,
Where ASIN = B00EISTU74
One thing which would make this a lot better is to add quotes around the text strings in the query:
update table
set sellerSK='.',
Title='2pcs Fashion...',
(etc)
Can't tell from the screenshot if this is the problem, but it certainly looks like it is an issue.
The problem is you are not using single quotes for any of your string values in the update statement. Put quotes on every value that is not an integer and the sql statement will work.
Also you have values of .. for some date fields. Assuming those are of an SQL data type DateTime or similar, this will not work. If you don't have a date, use Null instead.
Update amazon_ordred_items
Set OrderItemId = 666854391288218,
SellerSKu = Null,
Title = '2pcs Fashion Cool Universal Black Real Original Car Headlight Eyelashes Sticker',
QuantityOrdered = 1,
QuantityShipped = 1,
CurrencyCode = 'USD',
Amount = 0.50,
ScheduledDeliveryEndDate = Null,
ScheduledDeliveryStartDate = Null,
PromotionIds = Null,
Where ASIN = 'B00EISTU74'
Note also there's no need to update the ASIN field since you're not changing it's value and it is in your Where clause.
Related
My current SQL:
SELECT B.MESSAGENO, B.LINENO, B.LINEDATA
FROM BILL.MESSAGE AS B, BILL.ACTIVITYAS A
WHERE A.MSGNO = D.MESSAGENO AND A.FUPTEAM = 'DBWB'
AND A.ACTIVITY = 'STOPPAY' AND A.STATUS = 'WAIT'
AND A.COMPANY = D.COMPANY
MESSAGENO LINENO LINEDATA
1234567 1 CHEQUE NO : 9999999 RUN NO : 55555
1234567 2 DATE ISSUED: 12/25/2020 AMOUNT : 710.51
1234567 3 PAYEE : LASTNAME, FIRSTNAME
1234567 4 ACCOUNT NO : 12345-67890
1234567 5 USER : USERNAME
there are 550 sets of 5 lines per MESSAGENO
What I am trying to figure out is how I can get something like where LINENO = 1, concatenate LINEDATA so I just get 9999999 as checkno, where LINENO = 2, concatenate LINEDATA so I get 710.51 as amount, where LINENO = 3, concatenate LINEDATA so I get LASTNAME, FIRSTNAME as payee, where LINENO = 4, concatenate LINEDATA so I get LASTNAME, FIRSTNAME as payee, and lastly, the same thing for USERNAME.
I just cannot seems to conceptualize this. Every time I try, my brain starts turning into macaroni. Any help is appreciated.
UPDATED ANSWER, extracts all fields from stored strings:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=e22d26866053ea6088aa78dc23c4d809
Check this fiddle.
It uses a SUBSTRING_INDEX in the internal query to split the fields at the : or by a combination of : and " ". I used the two spaces because I wasn't sure what your actual whitespace was, and when I copied the data out of your post it was just spaces.
Then MAX is used in the outer query to get everything on one line, grouping by the messageNo. Since some lines have two pieces of data to extract, a second string parser was added. Here is the code from the fiddle, for reference. In this case, the table test was created from the output of OP's initial query, since I didn't have enough info to build both tables and completely recreate his output.
SELECT MESSAGENO,
MAX(if(LINENO = 1, extractFirst, null)) as checkNo,
MAX(if(LINENO = 1, extractLast, null)) as runNo,
MAX(if(LINENO = 2, extractFirst, null)) as issued,
MAX(if(LINENO = 2, extractLast, null)) as amount,
MAX(if(LINENO = 3, extractFirst, null)) as payee,
MAX(if(LINENO = 4, extractFirst, null)) as accountNo,
MAX(if(LINENO = 5, extractFirst, null)) as username
FROM (
SELECT MESSAGENO, LINENO,
trim(substring_index(substring_index(LINEDATA, ": ", -2), " ", 1)) as extractFirst,
trim(substring_index(LINEDATA, ":", -1)) as extractLast
FROM test
) z
GROUP BY MESSAGENO
Again, you will be much better off to alter your tables so that you can use simpler queries, as shown in the last fiddle.
===============================================
ORIGINAL ANSWER (demo of string parsing, suggestion for data model change)
You can achieve this with some string parsing BUT ABSOLUTELY DO NOT unless you have no choice. The reason you are having trouble is because your data shouldn't be stored this way.
I've included a fiddle incorporating this case statement and substring_index to extract the data. I have assumed mySQL 8 because you didn't specify SQL version.
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=068a49a2c819c08018691e54bcdf91e5
case when LINENO = 1 then trim(substring_index(substring_index(LINEDATA, "RUN NO", 1), ":", -1))
else trim(substring_index(LINEDATA, ":", -1)) end
as LDATA
See this fiddle for the full statement. I have just inserted the data from your join into a test table, instead of trying to recreate all your tables, since I don't have access to all the data I would need for that. In future, set up a fiddle like this one with some representative data and the SQL version, and it will be easier for people to help you.
=========================================
I think this is a better layout for you, with all data stored as the proper type and a field defined for each one and the extra text stripped out:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=11d52189b005740cdc53175466374635
UPDATE KopierenJPGs
SET(IdImage, Path, Date)
VALUES (115,'\fantasy\5.jpg', '22/02/2015 18:08:28')
WHERE IdImage = 30
And
WHERE Path = '\fantasy\6.jpg'
AND
WHERE Date = '22/02/2015 18:10:28'
I was taught to execute update statements like this, though not a single site gives this example.
It seems to me that my teacher confused an insert command with an update command?
Do I have it wrong here? Is there an actual error in this statement?
I am aware there are three where clauses, I need them for my program. I cannot update a fixed ID since the column names are variable, this is intentional.
Thank you.
I doubt your teacher made the confusion. Use the correct syntax for update:
UPDATE KopierenJPGs
SET IdImage = 115,
Path = '\fantasy\5.jpg',
Date = '2015-02-22 18:08:28'
WHERE IdImage = 30 And Path = '\fantasy\6.jpg' and Date = '2015-02-22 18:10:28';
Note that I also changed the date formats to ISO standard formats. You should use these in code to avoid ambiguity and future problems.
If you want to insert a new row, then yes instead of update you need insert statement like:
INSERT INTO KopierenJPGs (IdImage, Path, Date)
VALUES (115,'\fantasy\5.jpg', '22/02/2015 18:08:28')
If you want to update an already existing row, you could do something like:
UPDATE KopierenJPGs
SET IdImage = 115,
Path = '\fantasy\5.jpg',
Date = '22/02/2015 18:08:28'
WHERE IdImage = 30
AND Path = '\fantasy\6.jpg'
AND Date = '22/02/2015 18:10:28'
All I need to do is simply get one geography value from a table and store it in another table. There is some logic for which row to take from the origin table so it's not just a straight select.
In any of 50 possible variants of this, I get this error when hitting the update to the target table:
Msg 403, Level 16, State 1, Line 1
Invalid operator for data type. Operator equals not equal to, type equals geography.
My SQL looks like this at the moment:
declare
#EquipmentId int
, #CurrentLocationId int
, #CurrentGeoLocation geography
, #LastUpdated datetime
select #EquipmentId =
(
select top 1 EquipmentId
from Equipment
order by EquipmentId
)
select #CurrentLocationId = (select top 1 EquipmentLocationId from EquipmentLocation where EquipmentId = #EquipmentId order by LastUpdated desc)
select #LastUpdated = (select top 1 LastUpdated from EquipmentLocation where EquipmentId = #EquipmentId order by LastUpdated desc)
UPDATE
dbo.Equipment
SET
CurrentLocationDateTime = #LastUpdated
, CurrentGeoLocation = (select GeoLocation from EquipmentLocation where EquipmentLocationId = #CurrentLocationId)
, ModifiedBy = 'system'
, ModifiedByUserId = -1
, ModifiedDate = getdate()
WHERE
EquipmentId = #EquipmentId
I have had CurrentGeoLocation set in a variable of the same type, selected into by the same statement you see in the update.
I have had an #CurrentGeoLocation variable populated by a geography::STGeomFromText as well as geography::Point() function call.
I've used Lat and Long variables to call Point and FromText functions.
All the same result, the above 403 error. I could understand it somewhat when I was concatenating various permutations of the GeomFromText function that needs well known text format for the point parameter, but field value to field value is killing me, as is the fact that I get this error no matter how I try to give the origin point data to the target table.
Thoughts?
Update:
I've been experimenting a little and found that the following works just fine:
declare #GL geography
select #GL = (select GeoLocation from EquipmentLocation where EquipmentLocationId = 25482766)
print convert(varchar, #GL.Lat)
print convert(varchar, #GL.Long)
update Equipment set CurrentGeoLocation = geography::Point(#GL.Lat, #GL.Long, 4326)-- #NewGL where EquipmentId = 10518
But then when I apply this plan to the original script, I'm back to the same error.
The data in the test is working off the exact same records as in the original script. The original script is working off a collection of EquipmentIds, on the first one, I encounter this problem. The short test script uses the same EquipmentLocationId and EquipemntId that are the selected values used to update the first Equipment record in my collection.
Solved!
The error had nothing to do with the geography type as SQL reported. By pulling items in and out of the update statement in an effort to isolate why I still get the error even if I save everything but CurrentGeoLocation and then another update for the geography, I found that CurrentLocationDateTime (datetime, null) was the culprit. Deleted the column, added it back. Problem solved. Original script works as expected.
Don't know what happened to that datetime column that caused it to throw errors against a geometry type, but it's fixed.
I am having trouble with the following SIMPLE query:
INSERT INTO table (
[Date],
[Name],
[Weight],
[Position],
[Effective_Maturity],
[Yield_to_Worst],
[Modified_Duration],
[OAD],
[CTD],
[OAC],
[OAS],
[Coupon],
[Credit_Rating],
[CUSIP],
[Market_Value],
[Principal],
[Yield_to_Maturity])
VALUES
('6/28/2013','ISHARES BARCLAYS TIPS BOND FUND','100','0','8.647','0.0772','0.0772','3.8629','8.204','8.2040','1.112','9.034','1.543','AAA','TIPETF','1796461','178696')
GO
I'm not sure which variable in my table cannot be converted to numeric and my knowledge of data types in MS SQL is lacking. However, I think most of these inputs correctly fit their respective data types. The table data types are:
Thanks!
You try to put 'TIPETF' string in [Market_Value] column, which is numeric.
Your fields line up incorrectly: 'AAA' looks like a credit rating, not like a CUSIP (CUSIPs are nine characters long).
It looks like you skipped a value, or added a field somewhere in the middle, or both:
[Date] = '6/28/2013',
[Name] = 'ISHARES BARCLAYS TIPS BOND FUND'
[Weight] = '100',
[Position] = '0',
[Effective_Maturity] = '8.647',
[Yield_to_Worst] = '0.0772',
[Modified_Duration] = '0.0772',
[OAD] = '3.8629',
[CTD] = '8.204',
[OAC] = '8.2040',
[OAS] = '1.112',
[Coupon] = '9.034',
[Credit_Rating] = '1.543',
[CUSIP] = 'AAA', -- <<<=== THIS IS NOT A VALID CUSIP
[Market_Value] = 'TIPETF',
[Principal] = '1796461',
[Yield_to_Maturity] = '178696'
To fix this issue, make sure that your column names "line up" with the values that you are inserting into them.
I have found out that my SQL 2008 R2 database is really struggling with COALESCE function if used within search.
CODE:
where
i.id_categ = COALESCE(#id_categ, i.id_categ )
and i.id_brand = COALESCE(#id_brand , i.id_brand )
and i.id_model = COALESCE(#id_model , i.id_model )
and i.id_type = COALESCE(#id_karoseria, i.id_type )
and i.id_fuel = COALESCE(#id_palivo, i.id_fuel )
and (i.year between #year_from and #year_to)
and (i.price between #price_from and #price_to)
DYNAMIC variables:
ALTER PROCEDURE [dbo].[spInzeratSelect]
#id_categ int = null,
#id_brand int = null,
#id_model int = null,
#id_fuel int = null,
#id_type int = null,
Search should work with or without these variables.
Benchmark:
with COALESCE = Total Execution Time: 3582
without COALESCE conditions = Total Execution Time: 13
You get the difference ...
Is there a nice solution how to ignore COALESCE and create dynamic SQL select with different approch ?
Thanks.
tIn your very specific case you should replace all your COALESCE search parameters with the following pattern:
AND ( (#id_brand IS NULL) OR (i.id_brand = #id_brand) )
etc
The parameter is evaluated as a literal before execution, so doing it this way makes the condition sargable.
This is functionally equivalent to your query except you're first checking against the literal value, which can be optimized away if it is, in fact, null.
EDIT: Apparently this is the approach recommended in #Joe Stefanelli's link as well. I originally poached it from Erland Sommerskog.
EDIT2: And I always forget to mention OPTION (RECOMPILE) too.