I came across this situation while migrating our DB from Foxpro to SQL.
Below is the sample scenario.. (My original code is little complicated so just tried to create my own example)
I created several table variables in my sql server function to get what I require.
DECLARE @temp_1 TABLE(ID INT,
Name NCHAR(7),
bday DATE,
m_status NVARCHAR(10));
INSERT INTO @temp_1
SELECT Name, bday, m_status from Employee_Info
DECLARE @temp_2 TABLE(ID INT,
City NCHAR(7),
Country NVARCHAR(10),
zip NVARCHAR(10));
INSERT INTO @temp_2
SELECT City, Country, zip from Employee_Address
Next comes a situation where I need to use one each of the fields from the above table variables to derive data into my third table variable.
For example,
DECLARE @temp_full TABLE (
Name NCHAR(7),
City NCHAR(7));
INSERT INTO @temp_full
SELECT @temp_1.Name, @temp_2.City FROM @temp_1, @temp_2 WHERE @temp_1.ID = @temp_2.ID
[EDITED TO INCLUDE INSERT USING JOINS]
INSERT INTO @temp_full
SELECT @temp_1.Name, @temp_2.City FROM @temp_1 INNER JOIN @temp_2 ON @temp_1.ID = @temp_2.ID
However, when I do @temp_1.Name, @temp_2.City I get an error saying I must delcare the scalar variable @temp_1, @temp_2.
Can anyone please tell me how to resolve this problem.
Thank you for your help.
This is not foxpro and @variable_tables are not cursors like in foxpro, there are no any current record pointer for table, tables are sets of records and you must work with it as with sets.
If you need to fill @temp_full with records combined from tables @temp_1 and @temp_2, you must use select with joining this two tables like this:
Also note that you must use alias for variable tables