I'm trying to use apache camel (sql component) to update the DB. Problem is that the DB just doesn't get updated. The sql:update is working fine when i hard code the query, but when i try to use ${body[0][id]} it doesn't update required field. Any feedback on what might be going wrong?
from("direct:updateSql")
.to("sql:select * from mytable limit 1")
.log("update mytable set status = '100' where id = '${body[0][id]}'")
.to("sql:update mytable set status = '100' where id = '${body[0][id]}'")
.end();
Note that status and id are integer fields but if i remove the ' from the .to() then i throws some errors.
According to the docs you must use :# as the placeholder syntax
http://camel.apache.org/sql-component
So try with
.to("sql:update mytable set status = 100 where id = :#${body[0][id]}")
Below code worked too.
from("direct:updateSql")
.to("sql:select * from mytable limit 1")
.setHeader("id", simple("${body[0][id]}"))
.to("sql:update mytable set status_id = 100 where id = :#id")
.end();
Related
I am running executing raw sql to delete some records that I added for the test. If I run the same query in management studio it works fine but when I run that query EF Core 2.0 it throws below error
System.Data.SqlClient.SqlException: 'Conversion failed when converting the nvarchar value '1,2' to data type int.'
Code
var idList = await Context.User.ToListAsync();
var ids = string.Join(",",idList.Select(x=>x.Id));
await _context.Database.ExecuteSqlCommandAsync($"Delete from User where Id in ({ids}) and RoleId = {contact.RoleId}");
Query executing
Delete from sale.WatchList where OfferId in (1,2) and UserId = 9
Could anybody please advise on what wrong with the above code.
Thanks
EF Core will transform interpolated strings into queries with parameters to create reusable queries and protect against SQL Injection vulnerabilities. See: Raw SQL Queries - EF Core - Passing Parameters
So
$"Delete from User where Id in ({ids}) and RoleId = {contact.RoleId}"
is transformed into
Delete from User where Id in (#ids) and RoleId = #RoleId
With SqlParameters bound.
If that's not what you want, just build the SQL Query on a previous line.
This will not work. You have to write dynamic query. Please try like below one
var idList = await _dataContext.User.ToListAsync();
var ids = string.Join(",", idList.Select(x => x.Id));
await _dataContext.Database.ExecuteSqlCommandAsync($"execute('Delete from User where Id in ({ids}) and RoleId = {contact.RoleId}')");
although accepted answer does work, it creates lot of warnings so for now I am using what #Abu Zafor suggested with small change/fix
await _dataContext.Database.ExecuteSqlCommandAsync($"execute('Delete from User where Id in ({ids}) and RoleId = {contact.RoleId}')",ids,contact.RoleId);
I have a select statement That I need to turn into an Update.
I need to update a particular unix time field to January 1, 2016.
I have to select the records using a compound select statement.
Update archive_queue set archive_time = 1451606400
FROM
select recordings.(star), archive_queue.(star)
from
recordings, archive_queue
where
recordings.device_alias = '70285' and recordings.keepdays = 120
and recordings.ident = archive_queue.rec_ident
The above gives me a syntax error. The select gives me the records that I need to update. I looked at some examples here, but can't figure out the proper syntax based on my needs. Thanks everyone !
You should remove the select and only have one FROM statement:
Update archive_queue set archive_time = 1451606400
from recordings, archive_queue
where recordings.device_alias = '70285' and recordings.keepdays = 120
and recordings.ident = archive_queue.rec_ident
I am trying to make a report, where I can select a multiple values from a parameter - and then make a query, where i state "where in (#partner)"
However, my problems occours, when I want to make a value, that will not do the "where in (#partner)", but will simply select all from the same query, without the where in clause - as not all rows in my db table, have the specific values set, so I want to catch them too.
All the values for the parameter is selected from a query, and I have a union on it, to create a value for the one, where I need to select all - not using the where in.
It actually works, if only one value from the parameter list is chosen - but as soon as I try to select 2 or more, I get the error "query execution failed for dataset1......"
The value of the parameter value, that is used for selecting all is 0, as you see in the code - and it does work as I can execute both select statements below, but not when I select more then 1 value from the parameter list.
IF #partner = 0
BEGIN
SELECT
thedata
FROM
thetable
WHERE
thedata = 3
END
ELSE
SELECT
thedata
FROM
thetable
WHERE
thedata = 3
and partner in (#partner)
END
ELSE
Hope there is some clever guys out there:) Thanks
Just do:
SELECT thedata
FROM thetable
WHERE thedata = 3
AND (partner IN (#partner) or #partner = 0)
I have Imported Data Form XLS my CMS. but finally realize that it has wrong data without any Extension at the end of the data, in One of the column of the table, so now I want to change the column data of multiple rows based on specific ID? How to do that using single SQL Query?
I just want to add '.jpg' at the end on these data.
Image
UPDATE a
SET a.Imagepath = a.ImagePath + '.jpg'
FROM aspx_itemimages a
WHERE a.ItemId = yourid
Try this one -
UPDATE aspx_itemimages
SET ImagePath = ImagePath + '.jpg'
--WHERE itemid = 755
Try:
UPDATE ImagePath SET ImagePath = ImagePath + '.jpg'
According to your screenshot, some entries actually do end with .jpg. If you want to append .jpg only where it is absent, you could use a NOT LIKE predicate:
UPDATE aspx_itemimages
SET ImagePath = ImagePath + '.jpg'
WHERE ImagePath NOT LIKE '%.jpg'
;
That will also prevent you from adding the extension multiple times if you run the query again accidentally.
The above answers are correct but adding 2 strings returns 0, you will have to concat two strings.
here is the example:
UPDATE a
SET a.Imagepath = concat(ImagePath , '.jpg')
FROM aspx_itemimages a
WHERE a.ItemId = yourid
Best and Easiest way to do this...
"UPDATE aspx_itemimages
SET itempath = itempath +'.jpg'
WHERE itemid = 755 ";
Note: The Update doesn't generate a result set. If you want to know which records will be modified, first run a Select query that uses the same criteria. If the results are satisfactory, then run the update query.
I want to update on a linked server the result of a query as well from a linked server.
The first sql snippet gives me the value to be updated:
SELECT mmdb_vessel.IMONo, mmdb_vessel.DeathDate
From OPENQUERY(MMDB, 'SELECT FunctionalLocation, IMONo, VesselStatus, CONVERT(VARCHAR(10), DeathDate, 102) AS DeathDate
FROM VESSEL
WHERE VESSEL.VesselStatusID <> 42 AND VESSEL.DeathDate is not null') as mmdb_vessel
, eb_all_v
WHERE
eb_all_v.IMO_No = mmdb_vessel.IMONo
AND eb_all_v.status = 'in service'
the second is actually what I'm not able to implement, it should show what I want to achieve:
UPDATE EPI2..EPI.PLANT
SET KIND_OF_LIQUIDATION_NO = 1
, LIQUIDATION_DATE = [result from snippet above].DeathDate
Where EPI2..EPI.PLANT.IMONo = [result from snippet above].IMONo
I'm not so sure if my explanation is sufficient, please feel free to ask for additional information!
Thanks, already in advance,
Werner
I would recommend to select the data from the remote server first and store the required data e.g. in a temptable, because LinkedServer and updates can have some sideeffects (e.g. performing a tablescan on the remote table, altough you would not expect it if an updaet is involved, etc) - but this depends on your exact usage/scenario.
Select data you need to update
SELECT * INTO #tmpTable FROM LINKEDSERVER.EPI.dbo.PLANT WHERE ....
Perform the update on local server
UPDATE EPI2..EPI.PLANT SET KIND_OF_LIQUIDATION_NO = 1, LIQUIDATION_DATE = t.DeathDate FROM #tmpTable t INNER JOIN EPI2..EPI.PLANT p on t.IMONo = p.IMONo