Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

Nice Day,

I want to have a Table Variable, in which I want to have structure of a physical table.

How to declare a table variable with existing structure?


Eg:

Declare @tblTemp table() <-- Here I stuck with how to give structure of an existing physical table?

SELECT * INTO @tblTemp FROM tbl_PhysicalTable where 1=2 <-- This wont work for table variable, but works for Temporary Table.

And finally I want to do a insert into the Table variable like, with conditions and UNIONS.


INSERT @tblTemp (*)
(SELECT * FROM tbl_PhysicalTable WHERE nID= 1125
union
SELECT * FROM tbl_PhysicalTable1 WHERE nID= 1125)


Then Finally, I will have @tblTemp of filtered results.

How to achieve this?

Thanks in Advance.

Thanks and Regards
PMM :)
Posted

1 solution

CREATE TYPE mytableType AS TABLE( MyField1 VARCHAR(50), MyField2 INT )
DECLARE @MyVarTable AS mytableType
INSERT INTO @MyVarTable ....


Take a look at Here[^]
 
Share this answer
 
Comments
PeerMohamedMydeen 2-Oct-10 5:37am    
Hi,
You are creating a type.
But, How can I have the structure of the existing physical table?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900