# Create Your Own SQL Insertion Script

> Generate INSERT statements with a SELECT and move a table from SQL Server to PostgreSQL without buying a migration tool.

- Author: Felipe Fischer
- Published: 2022-07-03
- Series: Databases
- Tags: backup, create an insertion script, transport data, data engineering
- Reading time: 5 min
- URL: https://fischer.dev.br/writing/create-your-own-sql-insertion-script
- Originally published at: https://medium.com/@fischerfelipe7/sql-insert-creation-structure-2e1004554d4f

![](https://fischer.dev.br/assets/create-your-own-sql-insertion-script-01-BXXYMgVf.webp)

Have you ever faced a problem where you needed to **export data** from one database to another? Let’s say from SQL Server to PostgreSQL?

There are so many ways to do it, but probably one of the firsts would be: simply find some software that’s already there, but you’ll need to pay for it; or you would have enough control over the process; or even: you just want to learn how to do it for yourself. Well, this article was made for you.

An approach that I found really interesting is to create your own inserts using a SELECT statement, based on what you already have inside the table. So let’s create your own SQL INSERT using a SELECT statement. The SELECT will run on SQL Server and will be used to insert into PostgreSQL, for example. Or even on SQL Server to test it, this one here works on both (since we are not using columns with PascalCase — PostgreSQL needs an extra help, packing columns like “LastName” with double quotes when in PascalCase).

The sample database I’m using here comes from this link [here](https://www.sqlservertutorial.net/sql-server-sample-database/).

Let’s imagine that you need to transfer all of a customers/clients database from a legacy software to a new database that you’re using. After creating your ORM using a new structure (a better one), respecting data sizes, validations, and all the stuff you need in a really good software structure, it’s time to import data.

The image below shows how to do it from a customers table, and I’ll explain to you the lines below.

![You can reach this code on my github: here.](https://fischer.dev.br/assets/create-your-own-sql-insertion-script-02-BHRuqZv9.webp)
*You can reach this code on my github: [here](https://github.com/fischer7/SqlSamples/blob/master/Creating%20Script.sql).*

First of all, if you are creating an insertion script, for each new line you need the INSERT statement. So you’ll type your insertion script with every column you need.

Next come the values, but usually you may need some ID and some other information for your new table. If you are creating everything new, you could use a uuid_generate_v4() function on Postgres, as I’m using here on line 4, plus the creation date (previous image). Notice, if you already have this data inside your current table, you can just use it in your script, ignoring the first two fields on the insert statement, and removing line 4 of this script (remember to add the ‘(’ again on line 5 — which will become the new line 4 — as below).

> **[Initial with Creating Insert Sample · fischer7/SqlSamples@23ef2fd](https://github.com/fischer7/SqlSamples/commit/23ef2fd0a61c475d386f64a45bfd20c66a7deebd)**
> Just samples of how to do things with SQL. Contribute to fischer7/SqlSamples development by creating an account on… — github.com

Once you have prepared the insertion, now it’s time to prepare the data (columns) to fit into an insert statement.

From inside to outside functions:

1. cast(customer_id as varchar(max)) tells SQL that an integer field should become a string, once we’re creating an insertion string 🙂

2. isnull( something, ‘NULL’) = says that if the value of the field (something) is NULL, then we should just insert NULL into this field.

3. replace(first_name, ‘’’’, ‘’’’’’) = That’s actually a little magic to prevent problems. See, sometimes a person’s name, last name, email, etc. may come with one or more apostrophes, like the name: “O’neill”. But an apostrophe in an insertion script means that you’re opening a varchar/string (or closing it). So we need to cancel this operation by adding another apostrophe with the first one. Right now you’re doing it outside the script you’re creating, and it may be a little tricky to count all of your apostrophes. Take a little time and practice with this one so you’ll not create a real mess in your script😃

Moving forward, remember: data types such as integers don’t require apostrophes (’), but varchars do. And **DateTimes may come from a varchar/string (implicit conversion)**, although sometimes you may need to cast them, depending on the format that this date comes in and how your new database interprets DateTime format inside a string. You’ll need to get ready to improve it, if you need to. Let me help you a little bit on this one.

I’m already converting DateTime here to format 21 (yyyy-mm-dd hh:mm:ss:nnn). See more formats [here](https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/):

This format (21) usually is a good solution for databases, using this ordering: Year(4) — Month(2) — Day(2) Hour (2) : Minute (2) : Second(2) : Millisecond(3). But you may need something different, so feel free to test those things out ^\_^

There is a lot of information about the data type conversion [here](https://docs.microsoft.com/pt-br/sql/t-sql/data-types/data-type-conversion-database-engine?view=sql-server-ver16).

Okidoki, now you’re ready to **test your insertion script** against your new table (create your own table, ok?).

Just for this example, I’ve created the table again inside this database, but in another schema. So the script came with the INSERT INTO testing. customer:

![](https://fischer.dev.br/assets/create-your-own-sql-insertion-script-03-4IxYyc67.webp)

After running the insertion:

![The insertion process will run with F5, if you’re inside MSSQL Management Studio](https://fischer.dev.br/assets/create-your-own-sql-insertion-script-04-DNhCrjx9.webp)
*The insertion process will run with F5, if you’re inside MSSQL Management Studio*

You’ll be able to select against **both tables** to check it out:

![](https://fischer.dev.br/assets/create-your-own-sql-insertion-script-05-DG_Ad1UZ.webp)

Nice job! Right now all of your data is ready to be used, or almost! Databases should be consistent, so your constraints, primary and foreign keys, need to be respected.

Before your insertions, you may need to prepare the database for this process. Anyway, this is a discussion for another time.

If you enjoyed this content, please let me know by liking or leaving a comment below.

God bless you and your family (Families, blood or not, are awesome).

Have a nice and enjoyable coding day.
