0

我在将我们的数据库从 Foxpro 迁移到 SQL 时遇到了这种情况。

下面是示例场景..(我的原始代码有点复杂,所以只是尝试创建我自己的示例)

我在我的 sql server 函数中创建了几个表变量来获得我需要的东西。

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

接下来是一种情况,我需要使用上述表变量中的每个字段来将数据派生到我的第三个表变量中。

例如,

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

但是,当我执行@temp_1.Name,@temp_2.City 时,我收到一条错误消息,提示我必须删除标量变量@temp_1,@temp_2。

谁能告诉我如何解决这个问题。

感谢您的帮助。

4

1 回答 1

2

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:

INSERT INTO @temp_full
SELECT 
    temp_1.Name, 
    temp_2.City 
FROM @temp_1 temp_1 
JOIN @temp_2 temp_2 on temp_2.ID = temp_1.ID

Also note that you must use alias for variable tables

于 2012-06-28T19:58:06.993 回答