我需要将我的表变量中的数据插入到临时表中,但它会给我以下错误。
Msg 208,Level 16,State 0,Procedure sp_CreateScenario_q2,Line 70
无效的对象名称'#tmpp1'。
这是代码
IF OBJECT_ID('tempdb..#tmpp1') IS NOT NULL
DROP TABLE #tmpp1
INSERT INTO #tmpp1
SELECT
[PlanningHierarchyId]
,[ProductReferenceId]
,[PlanningYear]
,[PlanningSeason]
,[UpdatedBy]
FROM
@paramTable
有没有办法做到这一点?
答案 0 :(得分:4)
错误'无效的对象名称'#tmpp1'发生的原因是您删除临时表,然后尝试插入其中。
尝试使用:
IF OBJECT_ID('tempdb..#tmpp1') IS NOT NULL
DROP TABLE #tmpp1
SELECT
[PlanningHierarchyId]
,[ProductReferenceId]
,[PlanningYear]
,[PlanningSeason]
,[UpdatedBy]
INTO #tmpp1
FROM @paramTable
答案 1 :(得分:3)
CREATE
创建一个或使用select * into
代替insert into
IF OBJECT_ID('tempdb..#tmpp1') IS NOT NULL
DROP TABLE #tmpp1
SELECT
[PlanningHierarchyId]
,[ProductReferenceId]
,[PlanningYear]
,[PlanningSeason]
,[UpdatedBy] into #tmpp1
FROM @paramTable
答案 2 :(得分:2)
当表已存在时使用插入使用SELECT进入。当您尝试在临时表中插入时,临时表不存在。
db.county.aggregate([
{$unwind : "$cities" },
{$unwind : "$cities.zipCodes" },
{$project: {countyName:"$name",
cityName: "$cities.name",
ZipCode: "$cities.zipCodes.zipCode",
"_id":0}},
{$match:{"ZipCode":1234}
}]);