简体   繁体   中英

SQL Update table rows where the is a specific value

I have the following table in SQL server.

SQL表

It is called ScenarioData and I am using the data specifically the fieldValue column to append this data into a form. I have achieved this functionality but unfortunately, the form requires a start date that has to be the present-day or up to 30 days in the future. Because I am storing data in the database as soon as the date passes these values are redundant. I have a stored procedure that selects all values from this table based on the scenarioDataId.

I was thinking to ensure that the date is always viable I could add to this stored procedure to update the relevant rows (coverStartDateDay, coverStartDateMonth, coverStartDateYear) with the current date so that the value will always be accepted.

I have proceeded the following SQL

UPDATE dbo.ScenarioData
SET ScenarioData.FieldValue = DAY(CURRENT_TIMESTAMP)
WHERE ScenarioData.FieldName = "CoverStartDateDay";

This I had hoped would append the current day to the rows in feilfValue wherein the column fieldName it equals the value coverStartDateDay. Unfortunately, I get an error saying CoverStartDateDay is not a column. Firstly where am I going wrong and secondly how can I achieve the functionality I desire?

Thanks in advance

Try this as varchar or string values should be passed in the single quote, not in the double quote.

UPDATE dbo.ScenarioData
SET ScenarioData.FieldValue = DAY(CURRENT_TIMESTAMP)
WHERE ScenarioData.FieldName = 'CoverStartDateDay'

Double quotes are for identifiers. Somehow if you want to pass values which contain a single quote, then you can use '' ie, two times a single quote not the double quote.

Use single quote:

WHERE ScenarioData.FieldName = 'CoverStartDateDay';

In SQL Server double quotes are considers as column name. However, Double quotes have different usage depending on the setting QUOTED_IDENTIFIER .

By default QUOTED_IDENTIFIER is enabled ( ON ) & you can't use it to enclose literal strings.

For instance, if you want to go with double quotes then you need to disable QUOTED_IDENTIFIER .

SET QUOTED_IDENTIFIER OFF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM