Usage of table variables in SQL Server real-world development

Source: Internet
Author: User

In the actual development, we may encounter the problem is that in a stored procedure, we may want to return a multi-segment of the result set of SQL, but finally how to set a number of results together, then this time the temporary table variable comes

Declare @tmp table--Declaration table variable
(
ID int identity (total),--the field must correspond to the number one by one in the Insert table variable
Name varchar (60),
[Description] varchar (60),
Category varchar (60)
)
Insert @tmp
Select Book.name,book. [Description],book.category from book
SELECT * FROM @tmp

This is normal SQL notation, note that: Select to from the middle of the field must be the same as the field in the table variable, order, number one by one corresponds to

Here's how the stored procedure is written:

Create proc P_getselect
As
Begin
Declare @tmp table--Declaration table variable
(
ID int identity (+),
Name varchar (60),
[Description] varchar (60),
Category varchar (60)
)
Insert @tmp
Select Book.name,book. [Description],book.category from book

DECLARE @tmp2 table
(
ID int identity (+),
Name varchar (60),
[Description] varchar (60),
Category varchar (60)
)
Insert @tmp2--declaration table Variable 2 Here for convenience I put the data of a table into two table variables.
Select Book.name,book. [Description],book.category from book
Select t.*,tt.* from @tmp T, @tmp2 TT

End

EXEC P_getselect

Usage of table variables in SQL Server real-world development

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.