使用 CURSOR 创建临时表时出现以下错误:
(1 row(s) affected)
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '?'.
Msg 208, Level 16, State 0, Line 33
Invalid object name '##TEMP_Branch'.
没有 CURSOR 一切正常。这是整个代码:
declare @TableSchema table
(
Id Int Identity(1,1),
Name nVarchar(50),
DataType nVarchar(50)
)
declare @reportid uniqueidentifier
set @reportid = '597d37c0-563b-42f0-99be-a15000dc7a65'
declare @ttl nvarchar(100)
declare cur CURSOR LOCAL for
SELECT title
FROM ReportItems
where reportid = @reportid
and del = 0
ORder by so
open cur
fetch next from cur into @ttl
while @@FETCH_STATUS = 0 BEGIN
INsert @TableSchema Values(@ttl,'nVarchar(max) NULL')
fetch next from cur into @ttl
end
close cur
deallocate cur
Declare @Statement Varchar(1000)
Select @Statement = 'Create Table [##TEMP_Branch](FieldID Varchar(50)'
Select @Statement = COALESCE(@Statement +',','') + Name + ' ' + DataType from @TableSchema
Select @Statement = @Statement + ')'
EXEC(@Statement)
Select * from ##TEMP_Branch
drop table ##TEMP_Branch
任何帮助将不胜感激。