SQL Insert Into
There are many ways to add data to any table in Microsoft SQL. By opening a table in edit mode, you can manually add it to the relevant columns on the table manually by entering it manually. But in this article I would like to refer to this in T-SQL, so how to add data with code. Because SQL Server Management Studio is not always used, especially in our software applications, we use the relevant application to record data in projects we create in our WEB applications. In other words, we press on a button, for example, to record it. Therefore, it is important for us to know how the work is done in the coded sense.
Now to continue with this post I’ll create a table on the “staff” database. I’m building it with code.
--SQL SERVER INSERT CREATE TABLE information ( [Person_id] int, [Person_FirstName] varchar(30), [Person_LastName] varchar(30), [Person_Salary] int );
With this code I created a table with the columns Person_id, Person_FirstName, Person_LastName and Person_Salary named “information”. I have Person_id and Person_Salary in my table saying “NOT NULL,” and I could not pass the “INT” type. The columns Person_FirstName and Person_LastName are “varchar” type and have columns of 50 characters. When I run my query, I get the “Command (s) completed successfully” information. Thus, my table is created. When I right-click on the “staff” database on SQL Server Management Studio and when I say “refresh”, I can see the “information” table that I created under tables.
One of the simplest code methods for adding data to SQL is to add data for a single row in each query. For example, the user can fill in a WEB form or windows form and then press the save button. This is the record of single line data with a single command actually. A single INSERT command is sent to SQL Server. Another INERT command must be sent to enter new data. There are two syntaxes for entering information in a single line:
INSERT INTO tableName (column1, column2) SELECT value1, value2;
INSERT INTO tableName (column1, column2) VALUES (value1, value2);
There is a point I would like to mention here, it is not necessary to use the INTO part. So just enough to say INSERT. But they are usually used together with INSERT INTO. It is also more preferable to specify values with VALUES, which I have usually placed in the second order. Of course, these are not obligatory.
That’s enough summary. I’m going to write samples now. Write the examples I wrote. You can change and write samples to understand the topic.
SQL Insert Into Examples
INSERT INTO personel.dbo.information VALUES (1,'Jack','Enderson',7500); INSERT INTO personel.dbo.information VALUES (1,'Maria','Allen',4500); INSERT INTO personel.dbo.information VALUES (1,'Oliver','Pat',5500); INSERT INTO personel.dbo.information VALUES (1,'Rick','Quenn',7000); INSERT INTO personel.dbo.information VALUES (1,'Bob','Quit',10000); INSERT INTO personel.dbo.information VALUES (1,'Allen','Thin',25000); INSERT INTO personel.dbo.information VALUES (1,'Sara','Sait',150000);
INSERT INTO personel.dbo.information(info.person_id,info.person_salary) VALUES (11,9000);