一時テーブルの作成に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
どんな親切な助けでも大歓迎です。