在动态查询

时间:2015-04-22 11:05:30

标签: sql sql-server dynamicquery

当我尝试在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

问题似乎源于尝试选择record_id,这会在将varchar值'“转换为数据类型int'错误时将'转换失败

如果我不包含record_id列(这是选择列表中唯一的INT列),它似乎工作正常。

我曾尝试应用CONVERT(VARCHAR(250),record_id),但似乎无法使语法正确。

非常感谢任何帮助

2 个答案:

答案 0 :(得分: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';

答案 1 :(得分:0)

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

更改为

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

如果您想要一个经常不鼓励(但仍然非常方便)的快捷方式将数字转换为字符串,请更改底部的位

@campaignId

RTRIM@campaignId)

字符转换和修剪尾随等都在一个单词中。