Kentico:修改页面类型字段名且保留现有值的方法
Got it, let's break this down and fix this properly—you're right about Function being a SQL reserved keyword, which is causing that syntax error. The reason your data went blank when renaming to BusinessFunction is because Kentico doesn't automatically migrate existing data when you just rename a field's code name directly in the admin. Here are a couple of solid solutions:
Option 1: Migrate Data First (Recommended, Kentico Best Practice)
This is the safest approach to avoid data loss:
- Backup your database first—always do this before making schema changes!
- In the Kentico admin, go to your Staff page type and create a new field named
BusinessFunction, matching the exact type, settings, and validation rules of the originalFunctionfield (e.g., text length, required status). - Run a SQL script to copy data from the old field to the new one. Replace
CMS_Staffwith your actual Staff page type's database table name (it usually starts withCMS_followed by the page type display name):UPDATE [CMS_Staff] SET [BusinessFunction] = [Function] -- We use square brackets around [Function] because it's a reserved SQL keyword - Verify the migration worked: Open a few Staff pages in the admin and check that the
BusinessFunctionfield has the same values asFunctiondid. - Once confirmed, delete the original
Functionfield from the Staff page type. - Update your repeater's WHERE condition to
BusinessFunction LIKE '%insurance%'—no more syntax errors!
Option 2: Directly Rename the Database Column (More Technical)
If you'd prefer not to create a new field, you can sync the Kentico field definition with a database column rename:
- Backup your database—again, non-negotiable.
- In the Kentico admin, edit the
Functionfield's settings: Change its Code name toBusinessFunctionbut don't save yet (or save, but note that the data will appear blank temporarily). - Open your database management tool (like SSMS) and run this script to rename the actual database column:
EXEC sp_rename 'CMS_Staff.Function', 'BusinessFunction', 'COLUMN'; -- Syntax: sp_rename 'TableName.OldColumnName', 'NewColumnName', 'COLUMN' - Go back to the Kentico admin, refresh the field settings page, and check your Staff pages—your data should now appear in the
BusinessFunctionfield. - Update your repeater's WHERE condition as needed.
Bonus: Quick Fix Without Renaming the Field
If you don't want to rename the field at all, you can just escape the reserved keyword in your WHERE condition by wrapping it in square brackets:
[Function] LIKE '%insurance%'
SQL will recognize [Function] as a column name instead of the reserved keyword, resolving the syntax error immediately. This is a great shortcut if renaming feels like overkill.
内容的提问来源于stack exchange,提问作者Michelle

