After reading this post about ChatGPT imitating Linux, I wanted it to be a database server.
Let’s try it out!
Imagine you are a Microsoft SQL Server. I type commands, and you reply with the result, and no other information or descriptions. Just the result. Start with exec xp_cmdshell ‘whoami’;
Wow, this looks like a promising start.
And, it “thinks” that it is running as
LOCAL SYSTEM - quite funny actually.
Let’s see what databases it knows about?
Nice, looks like it had some sample databases as training data, like
Now, let’s create a new database.
Next, create a table to store some information.
And now, let’s insert some data!
Yes, that seemed to have worked. Can we select it?
Now, I was wondering…
Can ChatGPT write a stored procedure to perform an UPSERT on the newly created
users table? An upsert is an operation that will
UPDATE a provided record, and in case it does not yet exist
INSERT it into the table.
Additionally, ChatGPT also provided an example on how to call the stored procedure:
As a final example for this post, I used the newly created stored procedure and it works.
It actually runs the full logic, inserting new records and updating existing ones!
What I found so interesting about the update path of the stored procedure is that technically the last statement would result in different result, as an email address wasn’t provided, but ChatGPT “understands” the intent and it did what I thought it should do, and what most users would expect.
There is so much more to explore, but it’s quite obvious how powerful these capabilities are.
Appendix: T-SQL Commands
If you’d like to try these commands out yourself, here are T-SQL commands in plain text, rather then the screenshots above.
Imagine you are a Microsoft SQL Server. I type commands, and you reply with the result, and no other information or descriptions. Just the result. Start with exec xp_cmdshell 'whoami';
CREATE DATABASE ChatBot;
CREATE TABLE users ( userId INT NOT NULL PRIMARY KEY CLUSTERED, name NVARCHAR(MAX) NOT NULL, email NVARCHAR(MAX) NOT NULL );
INSERT INTO users VALUES (1, 'wuzzi', 'email@example.com'); INSERT INTO users VALUES (2, 'mallory', 'firstname.lastname@example.org'); INSERT INTO users VALUES (3, 'hacker', 'email@example.com');
SELECT * from users;
Now, write a stored procedures in T-SQL to perform UPSERT for the users table
Results are not deterministic, so you might get different results. There is a “Try again” button that you can use, where it seems to provide different seed values as you can see different results