0

MS SQL Server 2005 でクエリを実行しようとすると、上記のエラーが表示されます。これは、複数の部分で構築された動的クエリです。単純化された構造とデータは次のとおりです。

CREATE TABLE [record_fields](
[field_id] [int] NOT NULL,
[campaign_id] [int] NOT NULL,
[record_import_type_id] [int] NOT NULL,
[fieldname] [varchar](150) NOT NULL,
[import_file_column_index] [smallint] NOT NULL,
[records_fieldname] [varchar](50) NOT NULL,
[show_field] [bit] NOT NULL,
[field_order] [int] NOT NULL,
[dialler_field_required] [bit] NULL,
[dialler_field_name] [varchar](50) NULL,
[dialler_field_order] [int] NULL,
[field_group_id] [int] NOT NULL
);     

INSERT INTO [record_fields] VALUES(1,2,1,'Record Id',47,'record_id',0,1,1,'Record Id',NULL,1);
INSERT INTO [record_fields] VALUES(2,2,1,'Field Name 1',46,'field01',0,1,1,'Field 1',NULL,1);
INSERT INTO [record_fields] VALUES(3,2,1,'Field Name 2',46,'field02',0,1,1,'Field 2',NULL,1);

CREATE TABLE [records](
[record_id] [int] NOT NULL,
[campaign_id] [int] NOT NULL,
[dialler_entry_created] BIT NOT NULL,
[field01] [VARCHAR](50) NULL,
[field02] [VARCHAR](50) NULL
);

INSERT INTO [records] VALUES(1,2,0,'Field01 Value','Field02 Value');
INSERT INTO [records] VALUES(1,2,0,'Field01 Value','Field02 Value');

そして、実行しようとしているクエリは次のとおりです。

DECLARE @campaignId INT
SET @campaignId = 2

DECLARE @FieldName VARCHAR(250)
DECLARE @ColumnName VARCHAR(250)
DECLARE @SelectQuery VARCHAR(2000)
DECLARE @InsertQuery VARCHAR(2000)

SET @SelectQuery = ''
SET @InsertQuery = ''

declare #FieldNames cursor for SELECT records_fieldname, dialler_field_name FROM record_fields where campaign_id = @campaignid AND dialler_field_required = 1 ORDER BY dialler_field_order
        open #FieldNames
        fetch next from #FieldNames into @FieldName, @ColumnName
        while @@fetch_status = 0
        begin
            -- Build up a dymamic string of columns to read in the select query
            SET @SelectQuery = @SelectQuery + '''"''+' + @FieldName + '+''"'', '
            -- Build up a dynamic string of column names to add to our temp table
            SET @InsertQuery = @InsertQuery + '[' + @ColumnName + '] varchar(255), '

    fetch next from #FieldNames into @FieldName, @ColumnName
    end
    close #FieldNames
    deallocate #FieldNames

    IF Len(@SelectQuery) > 1 AND Len(@InsertQuery) > 1
    BEGIN
        -- Trim the trailing ','
        SET @InsertQuery = Left(@InsertQuery,Len(@InsertQuery)-1)
        SET @SelectQuery = Left(@SelectQuery,Len(@SelectQuery)-1)

        EXEC ('DECLARE @RecordData TABLE (' + @InsertQuery + ');'
                + 'INSERT INTO @RecordData SELECT ' + @SelectQuery + ' from records WHERE campaign_id =' + @campaignId + ' AND ISNULL(dialler_entry_created, 0) = 0; '
                + 'SELECT * FROM @RecordData;')
    END

この問題は、 「varchar 値 '"' をデータ型 int に変換するときに変換に失敗しました」というエラーが発生する record_id を選択しようとしたことが原因のようです。

record_id 列 (選択リストの唯一の INT 列) を含めない場合、問題なく動作するようです。

CONVERT(VARCHAR(250), record_id) を適用しようとしましたが、正しい構文を取得できないようです。

どんな助けでも大歓迎です

4

3 に答える 3

1

エラーが 2 件ありました。exec 関数で選択クエリとキャンペーン ID を作成する場合:

DECLARE @campaignId INT
SET @campaignId = 2

DECLARE @FieldName VARCHAR(250)
DECLARE @ColumnName VARCHAR(250)
DECLARE @SelectQuery VARCHAR(2000)
DECLARE @InsertQuery VARCHAR(2000)

SET @SelectQuery = ''
SET @InsertQuery = ''

declare #FieldNames cursor for SELECT records_fieldname, dialler_field_name FROM record_fields where campaign_id = @campaignid AND dialler_field_required = 1 ORDER BY dialler_field_order
        open #FieldNames
        fetch next from #FieldNames into @FieldName, @ColumnName
        while @@fetch_status = 0
        begin
            -- Build up a dymamic string of columns to read in the select query
            SET @SelectQuery = @SelectQuery + @FieldName + ', '            
            -- Build up a dynamic string of column names to add to our temp table
            SET @InsertQuery = @InsertQuery + '[' + @ColumnName + '] varchar(255), '

    fetch next from #FieldNames into @FieldName, @ColumnName
    end
    close #FieldNames
    deallocate #FieldNames

    IF Len(@SelectQuery) > 1 AND Len(@InsertQuery) > 1
    BEGIN
        -- Trim the trailing ','
        SET @InsertQuery = Left(@InsertQuery,Len(@InsertQuery)-1)
        SET @SelectQuery = Left(@SelectQuery,Len(@SelectQuery)-1)
        Declare @result nvarchar(max) ='DECLARE @RecordData TABLE (' + @InsertQuery + ');'
                + 'INSERT INTO @RecordData SELECT ' + @SelectQuery + ' from records WHERE campaign_id =' + cast(@campaignId as nvarchar(50))+ ' AND ISNULL(dialler_entry_created, 0) = 0; '
                + 'SELECT * FROM @RecordData;'
        Exec(@result)                    
    END

ここにフィドルが働いています: http://sqlfiddle.com/#!6/e450c/23

于 2015-04-22T11:44:07.907 に答える
0
        SET @SelectQuery = @SelectQuery + '''"''+' + @FieldName + '+''"'', '

への変更

        SET @SelectQuery = @SelectQuery + ' ' + @FieldName + ' ,'

数値を文字列に変換するための、しばしば推奨されない (しかし非常に便利な) ショートカットが必要な場合は、一番下のビットを次のように変更します。

@campaignId

RTRIM@campaignId)

文字変換を行い、末尾などをすべて 1 つの単語で切り捨てます。

于 2015-04-22T12:04:04.107 に答える
0

これがあなたの問題だと思います:

DECLARE @campaignId INT
SET @campaignId = 2
. . .
+ 'INSERT INTO @RecordData SELECT ' + @SelectQuery + ' from records WHERE campaign_id =' + @campaignId + ' AND ISNULL(dialler_entry_created, 0) = 0; '

に注意して+ @campaignIdください。

代わりにこれを試してください:

DECLARE @campaignId varchar(255);
SET @campaignId = '2';
于 2015-04-22T11:12:36.433 に答える