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