我在此存储过程的开头创建了一个临时表。它已成功创建,可以从中进行选择和插入。
这是创建声明
CREATE TABLE #tmpImportData (GuideFirstName VARCHAR(MAX), GuideLastName VARCHAR(MAX),
email VARCHAR(MAX), group_id_text VARCHAR(MAX), CandidateName VARCHAR(MAX),
grade_text VARCHAR(5), dateofbirth DATE
)
我的问题是在更改临时表后尝试更新列。我收到了错误
Msg 207,Level 16,State 1 列名无效
代码:
declare @SQl1 nvarchar(max)
set @SQL1 ='
ALTER TABLE #tmpImportData
ADD group_id INT
ALTER TABLE #tmpImportData
ADD guide_id INT
ALTER TABLE #tmpImportData
ADD password_plain_text VARCHAR(500)
ALTER TABLE #tmpImportData
ADD guide_email VARCHAR(500)
ALTER TABLE #tmpImportData
ADD class_id INT'
exec sp_executesql @Sql1
UPDATE #tmpImportData
SET group_id = CAST(group_id_text AS INT)
UPDATE #tmpImportData
SET group_id = 0 WHERE group_id IS NULL
答案 0 :(得分:0)
尝试这样做
declare @SQl1 nvarchar(max)
set @SQL1 ='
ALTER TABLE #tmpImportData
ADD ['+@group_id+'] INT
ALTER TABLE #tmpImportData
ADD ['+@guide_id+'] INT
ALTER TABLE #tmpImportData
ADD ['+@password_plain_text+'] VARCHAR(500)
ALTER TABLE #tmpImportData
ADD ['+@guide_email+'] VARCHAR(500)
ALTER TABLE #tmpImportData
ADD ['+@class_id+]' INT'
exec @Sql1
UPDATE #tmpImportData
SET group_id = CAST(group_id_text AS INT)
UPDATE #tmpImportData
SET group_id = 0 WHERE group_id IS NULL