SQL Server Temporary tables of are divided into local temporary tables and global temporary tables, which are described as follows in books online:
There are two types of temporary tables: local and global, both of which have different names, visibility, and availability. The name of the local temporary table starts with a single numeric sign (#). They are only visible to the current user connection and are deleted when the user is disconnected from the Microsoft SQL Server 2000 instance. The name of the global temporary table starts with a mathematical sign (#). After being created, it is visible to all users. All users who reference this table are deleted when they are disconnected from SQL Server.
The local temporary table created in the stored procedure is equivalent to the stored procedure variable. It is visible only in the stored procedure, andProgramThe created local temporary table is the same as that written in the Microsoft online series and is visible to the current user connection. With this understanding, you can use different methods to create a local temporary table under different situations and requirements. This will make the temporary table the most useful. The SQL code:CodeRun later! ">
Normal browsing
Copy code
Print Code
-
-
- /***************** Table variables ********************/
- Declare@ TTable(
-
- CataidInt
-
- )
-
-
- Insert Into@ TSelectA. cataidFromF_getchlid (27,'123')
-
- Select Top10 favorites. *, multicata. cataname
- FromFavoritesLeft Outer JoinMulticataOnFavorites. cataid = multicata. cataid
-
- WhereFavorites. userid ='123' AndFavorites. cataidIn(
-
- Select*From@ T)
-
-
- /**************** Temporary table ********************* ***/
-
- Method 1,
-
- SelectA. cataidInto# TFromF_getchlid (27,'123')
-
- Select Top10 favorites. *, multicata. cataname
- FromFavoritesLeft Outer JoinMulticataOnFavorites. cataid = multicata. cataid
-
- WhereFavorites. userid ='123' AndFavorites. cataidIn(
-
- Select*From# T)
- Drop Table# T
-
-
-
- Method 2,
-
- Create Table# T (
-
- CataidInt
-
- )
- Insert Into# TSelectA. cataidFromZhaoguoguang. f_getchlid (27,'123')
-
- Select Top10 favorites. *, multicata. cataname
-
- FromFavoritesLeft Outer JoinMulticataOnFavorites. cataid = multicata. cataid
- WhereFavorites. userid ='123' AndFavorites. cataidIn(
-
- Select*From# T)
-
- Drop Table# T