Update Field with Special Character ' (single quote) - sql

I am workign with SQL Server 2008 and have to update one field (FIELD_NAM) which contains values of this format:
First value in the field: 'abc', 'efg', 'xyz'
Second value in the field: 'aaaaa', 'bbbb', 'vvvvvv'
I tried with the following statement:
UPDATE Table
SET FIELD = 'ttttt', 'kkkk', 'mmmmmm'
WHERE ID = 1
(only one row and one field/column has to be updated)
The error I got is
Incorrect syntax near 'ttttt'

update "table"
set field = '''ttttt'', ''kkkk'', ''mmmmmm'''
where id = 1
;

UPDATE Table
SET FIELD = 'ttttt\', \'kkkk\', \'mmmmmm'
WHERE ID = 1
OR
UPDATE Table
SET FIELD = 'ttttt'', ''kkkk'', ''mmmmmm'
WHERE ID = 1

Related

how to use update query in to update query in postgresql

I want to update ChannelInfo what ever Reporting ChannelId update row returns
but this query is not working, giving error:
ERROR: syntax error at or near ""AdsReporting""
LINE : where "CampaignInfo"."id" in (update "AdsReporting" set "adS...
^
SQL state: 42601
Character: 240
can anyone please help me?
Reporting ChannelId is a foreign key relationship with ChannelInfo primary key.
code:
update "ChannelInfo" set "Amount"=CASE WHEN "Duration"='60' THEN "Amount"-12.25 ELSE "Amount"-6.13 END
where "id"=(update "Reporting" set "Status"='run' where "Status"='a' RETURNING "ChannelId");
It sounds like you want to use CTEs:
with u as (
update "Reporting"
set "Status" = 'run'
where "Status" = 'a'
RETURNING "ChannelId"
)
update "ChannelInfo" ci
set "Amount"= (CASE WHEN ci."Duration" = '60' THEN ci."Amount" - uc.cnt * 12.25 ELSE ci."Amount" - uc.cnt * 6.13 END)
from (select "ChannelId", count(*) as cnt
from users u
group by "ChannelId"
) uc
where uc."ChannelId" = ci.id;

Update value by using previous value to increment

I have a query that inserts a new row and creates a unique date. If there is a conflict, I want to update the row instead.
const updateInsight = await Insight.createQueryBuilder("Insight")
.insert()
.values({ sales: 1 })
.onConflict("(date) DO UPDATE SET sales = sales + 1")
.execute();
This is the same as:
INSERT INTO "Insight" ("sales") VALUES(1) ON CONFLICT (date) DO UPDATE SET sales = sales + 1;
The table looks like this where the date field is default: NOW() with type: Date:
Running the query I get the following error:
"column reference "sales" is ambiguous"
I have tried using Insight.sales = Insight.Sales + 1 in the UPDATE SET, but then I get:
"missing FROM-clause entry for table "insight\

Update multiple columns from a sub query

UPDATE PINPOINT_SUPPLEMENT
SET (ATTACHMENT_VALUE,ATTACHMENT_TYPE) = (
SELECT key,'file'
FROM PINPOINT_DOCUMENT
WHERE PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
)
WHERE ATTACHMENT_VALUE IS NULL
Getting Error when i execute this query
ERROR: syntax error at or near "SELECT"
LINE 3: SELECT key,'file
update PINPOINT_SUPPLEMENT
set
ATTACHMENT_VALUE = PINPOINT_DOCUMENT.key,
ATTACHMENT_TYPE = 'file'
from PINPOINT_DOCUMENT
where
PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
and PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE IS NULL
or
update PINPOINT_SUPPLEMENT
set
(ATTACHMENT_VALUE,ATTACHMENT_TYPE) = (PINPOINT_DOCUMENT.key, 'file')
from PINPOINT_DOCUMENT
where
PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
and PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE IS NULL
Support for updating tuples was introduced in Postgres 9.5, so you can't use that syntax with your version.
But as the second value is a constant I don't see a reason to use that syntax to begin with:
UPDATE PINPOINT_SUPPLEMENT
SET ATTACHMENT_VALUE = (SELECT "key"
FROM PINPOINT_DOCUMENT
WHERE PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID),
ATTACHMENT_TYPE = 'file'
WHERE ATTACHMENT_VALUE IS NULL
Note, that the sub-query might result in an error if there is more than one ATTACHMENT_VALUE for a document_id!

How to not update null value into a column when it already have a value in SQL Server query

How can I not update the null value into a column when it already have a value in SQL Server query in the query prepared in Mule? The payload contains the result set from Data weave. While updating, I want to check whether the updating value is null or not, in case of it "not null" then only value should be updated.
Query is:
UPDATE dbo.ix_str_store
SET NAME = '#[payload.Site.Name]',
ADDRESS1 = '#[payload.Site.Address1]',
ADDRESS2 = '#[payload.Site.Address2]',
ADDRESSCITY = '#[payload.Site.AddressCity]',
ADDRESSSTATE = '#[payload.Site.AddressState]',
ADDRESSPOSTALCODE = '#[payload.Site.AddressPostalCode]',
ADDRESSCOUNTRY = '#[payload.Site.AddressCountry]',
EMAIL = '#[payload.Site.Email]',
PHONE = '#[payload.Site.Phone]',
FAX = '#[payload.Site.Fax]',
REGION = '#[payload.Site.Region]',
COMPANY = '#[payload.Site.Company]',
DESC1 = '#[payload.Site.Desc1]',
DESC2 = '#[payload.Site.Desc2]',
DESC7 = '#[payload.Site.Desc7]',
DESC8 = '#[payload.Site.Desc8]',
VALUE1 = #[payload.Site.Value1],
VALUE2 = #[payload.Site.Value2],
DBTIME = CURRENT_TIMESTAMP,
DBSTATUS = 1
WHERE
STORENUMBER = #[payload.Site.StoreNumber]
try it's
isnull(new_value,old_value)
In that case, instead of using insert into (...) values(...) construct ; use insert into .. select from construct like
insert into table (..)
select ..,
case when new_col_val is not null then new_col_val else old_col_val end
from ...
(OR) you can as well use COALESCE() function instead of CASE expression saying coalesce(new_col_val, old_col_val)

DB2 MERGE does not INSERTwhen NOT MATCHED

My Statement:
MERGE INTO tblshoppingcart AS target USING
(SELECT * FROM tblshoppingcart
WHERE session_id = 'f7f2eb03-5ca5-4a85-b83e-70f197c087ae ' AND primlink = '19830625000054' AND store = 17 AND catalog = 'SS3' AND quantity = 35 AND item_type = 0) AS source
ON target.primlink = source.primlink AND
target.session_id = source.session_id AND target.item_type = source.item_type
WHEN NOT MATCHED THEN
INSERT VALUES ( 'f7f2eb03-5ca5-4a85-b83e-70f197c087ae', '19830625000054', 17, 'SS3', 'PAS', 35, 5, '', 0 )
WHEN MATCHED THEN
UPDATE SET quantity = 15
When matched the UPDATE works fine
When NOT MATCHED the INSERT doesn't throw an error but doesn't insert anything either.
Try changing WHEN NOT MATCHED THEN to:
WHEN NOT MATCHED BY TARGET THEN
Thanks, for the repsonses I found the issue to be I was passing hardcoded values but when I used the source/target names it worked.
So instead of INSERT VALUES ( 'f7f2eb03-5ca5-4a85-b83e-70f197c087ae'
I do INSERT VALUES ( target.session