我正在尝试声明一个表变量,然后将其加入到我在数据库中创建的表中。每次我尝试将我的“ NAME
”字段插入到我的表中时,我都会收到错误“ Invalid Column Name "NAME"
”,即使 GNAME 字段工作正常。我做错了什么,我该如何加入我的NAME
专栏?
DECLARE @Names TABLE
(
ID INT,
NAME VARCHAR(100),
GNAME VARCHAR(100)
)
INSERT INTO @Names
(
ID,
NAME,
GNAME
)
SELECT
CName.ID,
Ref.NAME,
Ref.GNAME
FROM
@CurrentPositions AS CName
LEFT OUTER JOIN
dbo.NameField AS Ref
ON
CName.ID = Ref.ID
IF ( OBJECT_ID('dbo.ReportTable', 'U') IS NOT NULL)
DROP TABLE dbo.ReportTable
CREATE TABLE [dbo].[ReportTable]
(
[ID_NUMBER] [INT],
[NAME] [VARCHAR](150)
[GNAME] [VARCHAR](150)
)
INSERT INTO [dbo].[ReportTable]
(
ID_NUMBER,
NAME,
GNAME
)
SELECT
C.ID_NUMBER,
N.NAME,
N.GNAME
FROM
@Names AS N
INNER JOIN
@CurrentPositions AS C
ON N.ID_NUMBER = C.ID_NUMBER