1

一時テーブルの作成に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

どんな親切な助けでも大歓迎です。

4

2 に答える 2

0

ReportItemsテーブルに、空白またはその他の不正な文字(疑問符)が含まれているタイトルがある可能性があります。

EXEC(@Statement)最初のエラーは、テーブル定義が無効であるために発生したように見えます。

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '?'.

2番目のエラーはSelect * from ##TEMP_Branch、存在しない一時テーブルに対して実行されている可能性があります。

Msg 208, Level 16, State 0, Line 33
Invalid object name '##TEMP_Branch'.
于 2013-02-14T21:11:26.823 に答える
0

テーブル変数を使用する

    Declare @TEMP_Branch TABLE
    (
       FieldID varchar(50)
    )

関係なしで通常のテーブルとして機能し、削除する必要はありません。より大きなデータ処理のために、データを一時的に保存するための永続的なテーブルを作成し、削除または切り捨てた後

于 2013-02-14T20:54:59.420 に答える