转换varchar值时转换失败SQL错误

时间:2019-11-21 14:37:47

标签: sql sql-server

我正在尝试创建动态SQL查询并执行插入操作。但是由于某种原因,我越来越 错误:Conversion failed when converting the varchar value '

请任何人能帮助我解决此错误。在values('@file_type_id'处出现错误

下面是我的SQL代码:

    DECLARE @file_name VARCHAR(100) = 'MyFile'
    DECLARE @file_type_id INT = 1
    DEclare @filing_id bigint = 57
    DECLARE @created_at DATETIME = GETDATE()
    DECLARE @created_by BIGINT = 2
    DECLARE @is_confidential bit = 1

    set @insertquery = 
    '
    DECLARE @Document AS VARBINARY(MAX)
    SELECT @Document = CAST(bulkcolumn AS VARBINARY(MAX)) FROM OPENROWSET( BULK ''C:\SampleTestFiles\MyWordDoc.doc'', SINGLE_BLOB ) AS Doc

    Insert  ['+@databasename+'].['+@schemaname+'].['+@tablename+'] 
          ( [file_type_id], [file],  [file_name], [filing_id], [created_at], [created_by], [is_confidential])
   values ( '@file_type_id', @Document, @file_name, @filing_id , @created_at, @created_by, @is_confidential)
   '
    exec (@insertquery)

1 个答案:

答案 0 :(得分:3)

怀疑,这就是你所追求的。请注意,我已经对参数进行了参数化,并且安全地引用了动态对象的值:

DECLARE @databasename sysname,
        @schemaname sysname,
        @tablename sysname;

DECLARE @file_name varchar(100) = 'MyFile',
        @file_type_id int = 1,
        @filing_id bigint = 57,
        @created_at datetime = GETDATE(),
        @created_by bigint = 2,
        @is_confidential bit = 1,
        @CRLF nchar(2) = NCHAR(13) + NCHAR(10),
        @insertquery nvarchar(MAX);

SET @insertquery = N'DECLARE @Document AS VARBINARY(MAX);' + @CRLF +
                   N'SELECT @Document = CAST(bulkcolumn AS VARBINARY(MAX)) FROM OPENROWSET( BULK ''C:\SampleTestFiles\MyWordDoc.doc'', SINGLE_BLOB ) AS Doc;' + @CRLF + @CRLF +
                   N'INSERT INTO ' + QUOTENAME(@databasename) + '.' +  QUOTENAME(@schemaname) + N'.' +  QUOTENAME(@tablename) + N'  ( [file_type_id], [file],  [file_name], [filing_id], [created_at], [created_by], [is_confidential])' + @CRLF + 
                   N'VALUES (@file_type_id, @Document, @file_name, @filing_id , @created_at, @created_by, @is_confidential);';

--PRINT @insertquery; --Your best friend.

EXEC sp_executesql @insertquery, N'@file_name varchar(100),@file_type_id int,@filing_id bigint,@created_at datetime, @created_by bigint,@is_confidential bit', @file_name, @file_type_id, @filing_id, @created_at, @created_by, @is_confidential;

我无法测试以上内容,但是,如果您遇到任何错误,我建议您找一个最好的朋友。但是,如果我使用以下声明...:

DECLARE @databasename sysname = N'MyDB',
        @schemaname sysname = N'dbo',
        @tablename sysname = N'MyTable';

我得到这句话,似乎是正确的:

DECLARE @Document AS VARBINARY(MAX);
SELECT @Document = CAST(bulkcolumn AS VARBINARY(MAX)) FROM OPENROWSET( BULK 'C:\SampleTestFiles\MyWordDoc.doc', SINGLE_BLOB ) AS Doc;

INSERT INTO [MyDB].[dbo].[MyTable]  ( [file_type_id], [file],  [file_name], [filing_id], [created_at], [created_by], [is_confidential])
VALUES (@file_type_id, @Document, @file_name, @filing_id , @created_at, @created_by, @is_confidential);

编辑:解决OP中的注释“如何为文件路径放置另一个变量。” 正确的语法是利用QUOTENAME中的第二个参数(请注意,这强烈假设@filepath的长度不能大于128)字符):

SET @insertquery = N'DECLARE @Document AS VARBINARY(MAX);' + @CRLF +
                   N'SELECT @Document = CAST(bulkcolumn AS VARBINARY(MAX)) FROM OPENROWSET( BULK ' + QUOTENAME(@filepath,'''') + N', SINGLE_BLOB ) AS Doc;' + @CRLF + @CRLF +
                   N'INSERT INTO ' + QUOTENAME(@databasename) + '.' +  QUOTENAME(@schemaname) + N'.' +  QUOTENAME(@tablename) + N'  ( [file_type_id], [file],  [file_name], [filing_id], [created_at], [created_by], [is_confidential])' + @CRLF + 
                   N'VALUES (@file_type_id, @Document, @file_name, @filing_id , @created_at, @created_by, @is_confidential);';