As a 120 WPM typist (you read that right), ChatGPT or its cousins are a time-saver. I know the drill of adding a column or columns to an existing table. I want the columns to be required, but this means I have to first assign a value to each existing row in the database.
Take adding CreatedUtc/ModifiedUtc to a table (or all tables, long term) in a database. Here is what you tell ChatGPT:
For a SQL Server table write the following T-SQL:
- Use ALTER TABLE to add CreatedUtc and ModifiedUtc columns of type DATETIME2 as nullable
- Assign a DATETIME2 UTC value of January 1, 2025, 00:00:00 to a variable @stockCreatedDateTimeUtc
- Update all existing entries in the table to set CreatedUtc and ModifiedUtc to @stockCreatedDateTimeUtc
- Write an ALTER TABLE script to make CreatedUtc and ModifiedUtc not nullable
And here are the results but ChatGPT missed the required GO under the ALTER TABLE:
GO
Total time: under 60 seconds. Maybe 10 minutes if I typed it out by hand. You still need to know how to code T-SQL and understand SQL Server to use AI correctly.