开发者

tsql - how to assign a id to temp table

开发者 https://www.devze.com 2023-04-05 13:25 出处:网络
I have to create a temp tables...But the problem is if multiple user will use the system there 开发者_如何学Gois no way to distinguish between their temporary tables. So I thought of putting an id inf

I have to create a temp tables...But the problem is if multiple user will use the system there 开发者_如何学Gois no way to distinguish between their temporary tables. So I thought of putting an id infront of every table. This will be the unique id. Any suggestion how to do this?

This unique id is there in my select query :

SELECT TABLE2.ID, TABLE2.NAME, TABLE2.ADDRESS, TABLE2.PHONE, INTO   ##TEMP_TABLE
FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.NAME = TABLE2.NAME
GROUP BY TABLE2.ID,  TABLE2.NAME, TABLE2.ADDRESS, TABLE2.PHONE;

How can I append this ID with temp table?


Why not just use local temporary tables and not have to worry about it? Just prefix them with a single # mark.


Why not just use a table variable alone, and just put the Table2.Ids into it... That eliminates all these issues completely...

Declare @Ids Table (id integer primary key not null)
Insert @Ids(id)
 Select Distinct t2.ID 
    From Table1 t1 Join Table2 t2 
        On T2.Name = T1.Name 

Then wherever yuou need the other Table2 data later on, jkust perform a joion to this temp table variable... -- To get stuff from table2 for only these rows ...

Select <table2 column data>
From Table2 t2 Join @Ids i on i.id = t2.Id
0

精彩评论

暂无评论...
验证码 换一张
取 消