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
| SELECT column1 , column2 . . . ColumnN INTO New_table FROM tables [ Where weather ] ; |
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.
| SELECT Height ( 10 ) [ NationalIDNumber ] , [ LoginID ] , [ JobTitle ] , [ BirthDate ] , [ MaritalStatus ] , [ Gender ] , [ HireDate ] , [ VacationHours ] FROM [ AdventureWorks2017 ] . [ HumanResources ] . [ Employee ] |
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.
| sp_help '[AdventureWorks2017].[HumanResources].[Employee_Demo]' |
In the following screenshot, we can encounter that Employee_Demo table does not exist in my database.
Execute the following query to create new table [Employee_Demo] using the SELECT INTO statement.
| SELECT TOP ( 10 ) [ NationalIDNumber ] , [ LoginID ] , [ JobTitle ] , [ BirthDate ] , [ MaritalStatus ] , [ Gender ] , [ HireDate ] , [ VacationHours ] INTO [ AdventureWorks2017 ] . [ HumanResources ] . [ Employee_Demo ] FROM [ AdventureWorks2017 ] . [ HumanResources ] . [ Employee ] |
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.
We tin can admission data from newly created table Employee_Demo and verify that it contains same records as of our before select statement.
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.
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.
| SELECT * INTO [ AdventureWorks2017 ] . [ HumanResources ] . [ Employee_All ] from [ AdventureWorks2017 ] . [ HumanResources ] . [ Employee ] |
In the output, we can see it inserted 290 rows in Employee_All tabular array.
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.
| SELECT DISTINCT Constraint_Name Every bit [ Constraint ] , Table_Schema AS [ Schema ] , Table_Name AS [ TableName ] FROM INFORMATION_SCHEMA . KEY_COLUMN_USAGE where Table_Name = 'Employee' GO |
We can run across, Employee tabular array contains primary and strange keys.
Nosotros copied all columns in Employee_All from Employee table. We need to check Primary and Foreign key on destination table now.
| SELECT DISTINCT Constraint_Name As [ Constraint ] , Table_Schema Equally [ Schema ] , Table_Name AS [ TableName ] FROM INFORMATION_SCHEMA . KEY_COLUMN_USAGE where Table_Name = 'Employee_All' Become |
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.
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.
| one 2 3 4 5 6 7 8 ix 10 11 12 thirteen 14 15 16 17 18 19 xx 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | SELECT e . [ BusinessEntityID ] , p . [ Title ] , p . [ FirstName ] , p . [ MiddleName ] , p . [ LastName ] , p . [ Suffix ] , eastward . [ JobTitle ] , pp . [ PhoneNumber ] , pnt . [ Proper noun ] Every bit [ PhoneNumberType ] , ea . [ EmailAddress ] , p . [ EmailPromotion ] , a . [ AddressLine1 ] , a . [ AddressLine2 ] , a . [ City ] , sp . [ Name ] Every bit [ StateProvinceName ] , a . [ PostalCode ] , cr . [ Name ] AS [ CountryRegionName ] , p . [ AdditionalContactInfo ] FROM [ HumanResources ] . [ Employee ] e INNER JOIN [ Person ] . [ Person ] p ON p . [ BusinessEntityID ] = e . [ BusinessEntityID ] INNER Join [ Person ] . [ BusinessEntityAddress ] bea ON bea . [ BusinessEntityID ] = e . [ BusinessEntityID ] INNER Join [ Person ] . [ Address ] a ON a . [ AddressID ] = bea . [ AddressID ] INNER Join [ Person ] . [ StateProvince ] sp ON sp . [ StateProvinceID ] = a . [ StateProvinceID ] INNER JOIN [ Person ] . [ CountryRegion ] cr ON cr . [ CountryRegionCode ] = sp . [ CountryRegionCode ] LEFT OUTER JOIN [ Person ] . [ PersonPhone ] pp ON pp . BusinessEntityID = p . [ BusinessEntityID ] LEFT OUTER JOIN [ Person ] . [ PhoneNumberType ] pnt ON pp . [ PhoneNumberTypeID ] = pnt . [ PhoneNumberTypeID ] LEFT OUTER Bring together [ Person ] . [ EmailAddress ] ea ON p . [ BusinessEntityID ] = ea . [ BusinessEntityID ] ; GO |
Execute a query, and we get following the output of the SQL SELECT INTO 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.
| one two 3 iv five 6 seven eight 9 ten 11 12 13 xiv xv sixteen 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | SELECT e . [ BusinessEntityID ] , p . [ Championship ] , p . [ FirstName ] , p . [ MiddleName ] , p . [ LastName ] , p . [ Suffix ] , east . [ JobTitle ] , pp . [ PhoneNumber ] , pnt . [ Proper noun ] AS [ PhoneNumberType ] , ea . [ EmailAddress ] , p . [ EmailPromotion ] , a . [ AddressLine1 ] , a . [ AddressLine2 ] , a . [ Urban center ] , sp . [ Proper noun ] Equally [ StateProvinceName ] , a . [ PostalCode ] , cr . [ Name ] Equally [ CountryRegionName ] , p . [ AdditionalContactInfo ] INTO [ HumanResources ] . [ Employee_JoinTables ] FROM [ HumanResources ] . [ Employee ] east INNER JOIN [ Person ] . [ Person ] p ON p . [ BusinessEntityID ] = e . [ BusinessEntityID ] INNER JOIN [ Person ] . [ BusinessEntityAddress ] bea ON bea . [ BusinessEntityID ] = e . [ BusinessEntityID ] INNER JOIN [ Person ] . [ Address ] a ON a . [ AddressID ] = bea . [ AddressID ] INNER Join [ Person ] . [ StateProvince ] sp ON sp . [ StateProvinceID ] = a . [ StateProvinceID ] INNER Join [ Person ] . [ CountryRegion ] cr ON cr . [ CountryRegionCode ] = sp . [ CountryRegionCode ] LEFT OUTER Bring together [ Person ] . [ PersonPhone ] pp ON pp . BusinessEntityID = p . [ BusinessEntityID ] LEFT OUTER JOIN [ Person ] . [ PhoneNumberType ] pnt ON pp . [ PhoneNumberTypeID ] = pnt . [ PhoneNumberTypeID ] LEFT OUTER JOIN [ Person ] . [ EmailAddress ] ea ON p . [ BusinessEntityID ] = ea . [ BusinessEntityID ] ; Become |
It creates [HumanResources].[Employee_JoinTables] table and insert data into it. We can verify records in this tabular array by the select argument.
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.
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.
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.
| select * into person . person_temp ON INSERTFILE --FILEGROUP NAME from person . person |
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.
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.
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%.
| select * into person . person_WC ON INSERTFILE -- Verify table Filegroup from person . person where Lastname 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
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