Edit column data at SQL - sql

I am trying to update my retrieved data as described below:
UPDATE vw_public_task_priority
SET task_state = REPLACE(task_state, 'NULL', 'DONE')
After I execute it I get the next error:
Msg 4406, Level 16, State 1, Line 41
Update or insert of view or function 'vw_public_task_priority'
failed because it contains a derived or constant field.
Can you please advice to me what I'm doing wrong + there is a possibility to update the results at new column instead of edit "task_state" data?
Thanks!

Try this
UPDATE vw_public_task_priority SET task_state = 'DONE' where task_state is null

Related

How to solve the error_syntax error near '< '

Msg 102, Level 15, State 1, Line 3 Incorrect syntax near '<'.
I got the above error message every time I tried to execute the below query.
UPDATE [dbo].[FM1]
SET [Datum] = <Datum, smalldatetime,>
,[Gesamtzeit] = <Gesamtzeit, nvarchar(5),>
You can try this no need to specify datatype here.
UPDATE [dbo].[FM1]
SET [Datum] = 'YourValue'
,[Gesamtzeit] = 'YourValue'
where ...

Rerun Error Message While Creating a Function

i am fairly new to SQL programming, and I am currently learning to create FUNCTIONS.
The problem that I am having, is creating the following FUNCTION.
create function CreatePI
(
)
returns decimal(10,6)
with returns null on null input
as
begin
declare #P as decimal(10,6)
set #P = 4*(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15)
return #P
end
go
The above function is supposed to replicate the number PI. But the problem that I am having is:
Msg 156, Level 15, State 1, Procedure CreatePI, Line 11
Incorrect syntax near the keyword 'return'.
If anyone could help me out with why I am getting this problem, it would be greatly appriciatead.
You are missing a closing paren on the set line:
set #P = 4*(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15))
----------------------------------------------------------^

Updating part of a SQL cell

Trying to run this SQL script
update Promotions
set PromotionDiscountData = '<ArrayOfPromotionRuleBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><PromotionRuleBase xsi:type="StartDatePromotionRule"><StartDate>2013-11-11T00:00:00</StartDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationDatePromotionRule"><ExpirationDate>2014-01-12T00:00:00</ExpirationDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationNumberOfUsesPerCustomerPromotionRule"><NumberOfUsesAllowed>1</NumberOfUsesAllowed></PromotionRuleBase><PromotionRuleBase xsi:type="ProductIdPromotionRule"><ProductIds><int>55232</int></ProductIds><RequireQuantity>false</RequireQuantity><Quantity>1</Quantity><AndTogether>false</AndTogether></PromotionRuleBase></ArrayOfPromotionRuleBase>'
where PromotionDiscountData = '<ArrayOfPromotionRuleBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><PromotionRuleBase xsi:type="StartDatePromotionRule"><StartDate>2013-11-18T00:00:00</StartDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationDatePromotionRule"><ExpirationDate>2014-01-12T00:00:00</ExpirationDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationNumberOfUsesPerCustomerPromotionRule"><NumberOfUsesAllowed>1</NumberOfUsesAllowed></PromotionRuleBase><PromotionRuleBase xsi:type="ProductIdPromotionRule"><ProductIds><int>55232</int></ProductIds><RequireQuantity>false</RequireQuantity><Quantity>1</Quantity><AndTogether>false</AndTogether></PromotionRuleBase></ArrayOfPromotionRuleBase>'
but getting this error
Msg 402, Level 16, State 1, Line 1
The data types xml and varchar are incompatible in the equal to operator.
any idea how to fix this?
Basically within each cell I am trying to change the StartDate only
I suppose the type of PromotionDiscountData column is XML, so that's why I suggest you to use the following code snippet:
update Promotions
set PromotionDiscountData = '<ArrayOfPromotionRuleBase ...'
where CAST(PromotionDiscountData as NVARCHAR(MAX)) = '<ArrayOfPromotionRuleBase...'

Why SQL Server complains about non existing column, if the actual SQL is not executed?

I have the following SQL:
IF EXISTS (SELECT 1 FROM sys.columns WHERE name='RequireNamespaceClaim' AND object_id = OBJECT_ID('DefaultBaseUrl'))
BEGIN
UPDATE DefaultBaseUrl SET AuthenticationTypeId = at.AuthenticationTypeId
FROM DefaultBaseUrl dbu
JOIN (
SELECT AuthenticationTypeId, CASE CodeName WHEN 'NATIVE' THEN 0 ELSE 1 END RequireNamespaceClaim
FROM AuthenticationType
) at ON dbu.RequireNamespaceClaim = at.RequireNamespaceClaim
END
Running it prints:
Msg 207, Level 16, State 1, Line 8
Invalid column name 'RequireNamespaceClaim'.
However, running
SELECT 1 FROM sys.columns WHERE name='RequireNamespaceClaim' AND object_id = OBJECT_ID('DefaultBaseUrl')
reveals that no such column indeed exists.
So, the IF EXISTS statement is FALSE, hence the body of the if-statement does not run. However, the error is still printed.
What is going on?
How can I fix it?
SQL Server compiles the entire query and checks it for validity.
At that time the column doesn't exist.
There is a command to run an SQL from a string which is not compiled.
Have a look at (I think that's the right one):
http://technet.microsoft.com/en-us/library/ms175170(v=sql.105).aspx

Getting error while executing the query?

When I run my query:
UPDATE oms.Document_Latest
SET size ='54324', CheckInBy = 'Anshul123',
Status ='checkedin', CheckOutBy = 'NULL',
CheckInOn = '4/28/2016 1:45:36 PM', CheckOutOn = 'NULL'
WHERE (Id = '1')
Datatype of columns are given in image which is attached with this question
I get this error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Please help me find the reason why I get this error while execute this query.
Thanks
You receive an error that because you trying to update CheckOutOn column of datatype DATETIME with string 'NULL'. You have to remove quotes '' from NULL in following:
UPDATE oms.Document_Latest
SET size ='54324',
CheckInBy = 'Anshul123',
Status ='checkedin',
CheckOutBy = NULL,
CheckInOn = '4/28/2016 1:45:36 PM',
CheckOutOn = NULL
WHERE (Id = '1')