In a Person Analysis, Which of the Following Is a Primary Variable That Can Be Affected by Training?

This article will cover the SQL SELECT INTO statement including syntax, parameters and apply with multiple tables, filegroups and a WHERE status

We regularly insert information into SQL Server tables either from an awarding or directly in SSMS. We tin can insert data using the INSERT INTO statement. To do this, we should have a table already in place to insert information into it as we cannot create a table using Insert into the argument.

Nosotros need to practice the following tasks using INSERT INTO statement.

  • Create a table structure with appropriate information types
  • Insert data into it

But the skilful news is that nosotros tin can do both the job together, elegantly, using the SQL SELECT INTO statement. It creates a tabular array structure for the columns returned by Select statement.

Suppose we desire to update many records in a tabular array. We can use the SELECT INTO statement to create a backup table with the existing structure every bit of source table. Permit united states explore the SELECT INTO in this article.

SELECT INTO statement syntax

Parameters in the SELECT INTO Statement

  • Columns list: We need to specify cavalcade we want to remember and insert into a new tabular array
  • New_table: We tin can specify the new table name here. SQL Server creates a new table with columns mentioned in columns list. Nosotros cannot supervene upon an existing table using this. Table name should be unique
  • Tables: Information technology contains a table from where we desire to get records. We can have multiple tables defined here as well with proper Bring together clause
  • WHERE conditions: We tin can filter records using Where clause. Information technology is an optional clause

Let us explore the SQL SELECT INTO statement using examples.

Environs:

In this example, we are using sample database AdventureWorks2017. Suppose we desire to select records from [Employee] table and creates new table [Employee_Demo] using the SELECT INTO statement. We exercise want selected columns simply in the new table. Permit us run a select statement on Employee tabular array with the columns nosotros want to have.

Output of sample table

Before nosotros execute the SQL SELECT INTO statement, nosotros can verify that Employee_Demo tabular array does not be in AdventureWorks2017 database using sp_help command.

In the following screenshot, we can encounter that Employee_Demo table does not exist in my database.

verify table existence

Execute the following query to create new table [Employee_Demo] using the SELECT INTO statement.

We go the following output message in Azure Data Studio. Yous tin can note that the number of rows afflicted is 10. In the query, nosotros selected top 10 records from an Employee table.

Verify output of SQL SELECT INTO

We tin can admission data from newly created table Employee_Demo and verify that it contains same records as of our before select statement.

Verify records in newly created table

We did not specify whatsoever column properties in the SQL SELECT INTO argument. Allow usa compare the source and destination table columns and their properties.

We tin run sp_help 'tablename' command on both Employee and Employee_Demo tabular array. I copied the output of both tables in excel ( for selected columns). In the following screenshot, you can see that column datatype and their properties are similar.

Columns properties comparision for table created by SQL SELECT INTO

In the previous SQL SELECT INTO statement, we prepared a destination table (Employee_Demo) from a few columns in the source table (Employee).

Allow us create another table with all columns in an Employee table with the following query.

In the output, we can see it inserted 290 rows in Employee_All tabular array.

output of newly created table

Nosotros have verified that the SELECT INTO statement creates a new table with columns specified in the column list. It creates a similar data type as well in the destination tabular array.

Suppose we have primary and strange keys in the source table. Does it create a primary key and foreign cardinal likewise similar to the source table? No, the SQL SELECT INTO statement does not create whatever keys in the destination tabular array. If nosotros want, nosotros can define keys on the destination tabular array. Allow u.s.a. verify this in the following section.

In my before example, Employee table contains primary and foreign key defined on it. We tin can get a list of existing keys using the INFORMATION_SCHEMA view. Execute the following code, and information technology returns existing Primary and Foreign keys in Employee tabular array.

We can run across, Employee tabular array contains primary and strange keys.

Check Primary and Foreign keys in existing tables

Nosotros copied all columns in Employee_All from Employee table. We need to check Primary and Foreign key on destination table now.

Nosotros can encounter that it does not comprise any key in destination tabular array. It gives us flexibility to define keys on the destination table.

Check Primary and Foreign keys in newly created table

SQL SELECT INTO – Insert Data from Multiple Tables

In previous examples, we created a tabular array using the SELECT INTO statement from a unmarried table Employee. We can as well bring together multiple tables and use the SELECT INTO statement to create a new table with data besides. In this department, nosotros want to join multiple tables together. In the following query, nosotros joined the following tables together in AdventureWorks2017.

  • [HumanResources].[Employee]
  • [Person].[Person]
  • [Person].[BusinessEntityAddress]
  • [Person].[Address]
  • [Person].[StateProvince]
  • [Person].[CountryRegion]
  • [Person].[PersonPhone]
  • [Person].[PhoneNumberType]
  • [Person].[EmailAddress]

Execute the following query. Information technology gives results from multiple tables based on join status and columns mentioned.

Execute a query, and we get following the output of the SQL SELECT INTO statement.

Verify output of select statement

Nosotros desire to create a table with data returned using the in a higher place tabular array. Allow's execute the SQL SELECT INTO statement.

It creates [HumanResources].[Employee_JoinTables] table and insert data into it. We can verify records in this tabular array by the select argument.

Verify number of records affected

Nosotros can see you tin bring together multiple tables together and creates output table using the SELECT INTO statement.

We exercise non demand to have care of defining data types for destination table. If we want to create a table manually, nosotros demand to look at data blazon of each column and define datatype appropriately. If there is a mismatch between data types, you tin get an fault bulletin like post-obit.

Mistake 1: Due to a mismatch in information types

Msg 245, Level 16, State 1, Line one
Conversion failed when converting the varchar value 'GG' to data type int.

Error two: Msg 8152, Level 16, State 30, Line two

String or binary data would be truncated.

Nosotros do not get these errors while inserting information using the SQL SELECT INTO statement. However, we cannot insert information into existing tables using this method.

SELECT INTO – Specify Filegroup

In previous sections, we explored that we tin create a new table and insert data into information technology using the SQL SELECT INTO statement from existing tables. It creates a tabular array in default Filegroup simply. We cannot specify a item Filegroup until SQL Server 2016. In SQL Server 2017, we can specify a particular Filegroup in which a new table should be created. SQL Server creates a new tabular array in that particular Verify tabular array Filegroup. If we do non specify any Filegroups in SQL Server 2022 and to a higher place, it creates a table in default Filegroup.

Annotation: We cannot specify Filegroups in the SQL SELECT INTO for SQL Server 2022 and before.

Permit united states of america add a new Filegroup in AdventureWorks2017 database. Right click on the database and go to Filegroups.

In this Filegroup page, click on Add FileGroup and specify a name for Filegroup.

Add Filegroup

At present, click on Files and it lists out existing database files(data and log file), in this page, add a new datafile and specify FileGroup from the drop-down list. It should exist FileGroup nosotros created merely now.

Add new data file

We want to create a SQL table in INSERTFILE Filegroup. We have not set this filegroup every bit default filegroup.

In the following query, you can see we specified filegroup name using ON clause. Information technology works similar to a regular SQL SELECT INTO statement with a difference in a filegroup.

Once the tabular array is created, execute a sp_help command on this newly created table. In the following screenshot, we can verify that the table is located on INSERTFILE FileGroup. Information technology is the FileGroup nosotros created earlier.

Verify table Filegroup

Nosotros can also verify this from table properties. Right-click on a table in SSMS. In the Storage section, we tin can see the desired FileGroup.

Verify table Filegroup

SELECT INTO with a Where condition

Suppose nosotros want to create a table with a SQL SELECT INTO statement with few records in it. Nosotros can use a Where clause similar to a select statement. In the following query, we want to create a person.person_WC table for the person having concluding name is like %Duf%.

Conclusion

In this article, nosotros explored the SQL SELECT INTO statement and its usage scenarios. It is a useful control to create a re-create of a table without specifying information types.

  • Writer
  • Recent Posts

Rajendra Gupta

brodythound.blogspot.com

Source: https://www.sqlshack.com/sql-select-into-statement/

0 Response to "In a Person Analysis, Which of the Following Is a Primary Variable That Can Be Affected by Training?"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel