The first day of SQL Learning -- SQL defines variables as tables (Virtual tables)

Source: Internet
Author: User

SQL defines variables as the Table Type
When defining variables in SQL statements, we often define variables like char, varchar, nvarchar, int ........, so how to make a variable as a virtual table is actually very simple.

Basic Syntax:
Copy codeThe Code is as follows:
Declare @ t table (Data Type 1 in column 1, data type 2 in column 2 ,...............)
Insert into @ t (column name 1, column name 2 ,...............) values (...............)
[Code]
Or
[Code]
Insert into @ t (column name 1, column name 2,...) (select... from ....)

Experiment Table 1 (PeopleInfo ):
Id name phone
XC1 Lee 123333333
XC2 small 785555555
For example:
Copy codeThe Code is as follows:
Declare @ t table (id nvarchar (3), [name] nvarchar (10 ))
Insert into @ t (id, [name])
(Select id, [name] from PeopleInfo where [Name] like 'Li % ')
Select * from @ t

Result:
Id name
XC1 Li

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.