Cannot bulk load. The file “'+ @imagepath +'” does not exist or you don't have file access rights

拈花ヽ惹草 提交于 2020-11-29 17:47:01

问题


I'm trying to insert an image in SQL server but I got this error :

Cannot bulk load. The file "'+ @imagepath +'" does not exist or you don't have file access rights.

Create procedure insert_test
AS
BEGIN
INSERT test(
   Id,
logo

)
     VALUES (
         @Id,
(SELECT  BulkColumn
    FROM Openrowset(Bulk '''+ @imagepath +''', Single_Blob) as img),
END

Execution code

EXEC    @return_value = [dbo].[test_insert]
    @Id = N'0001',
    @imagepath = 'D:\\heart.png',

SELECT  'Return Value' = @return_value

GO

How can I fix this error?

Update :

This is an example of the procedure that I have created to use it:

USE [SysTest]
GO


ALTER PROCEDURE [dbo].[userinformations_insert]
@UserId nvarchar(55),
@UserName nvarchar(max),
@UserPhone int ,
@UserAdress int,
@UserEmail nvarchar(50),
@imagepath nvarchar(100),


AS
BEGIN

IF EXISTS ( SELECT 1 from User)
BEGIN

   update User  set userPicture = (SELECT  BulkColumn
        FROM Openrowset(Bulk '''+ @imagepath +''', Single_Blob) as img) , userEmail = @userEmail, userPhone = @userPhone, userAdress = @userAdress where userId =@UserId
END
ELSE
BEGIN
    INSERT User(
    userId,
    UserName ,
    userPicture
    userEmail,
    userPhone,
    userAdress,
    )
         VALUES (
             @UserId,
    @UserName ,
    (SELECT  BulkColumn
        FROM Openrowset(Bulk '''+ @imagepath +''', Single_Blob) as img),
    @UserEmail,
    @UserPhone,
    @UserAdress)
END
END

回答1:


As I said in the comment "OPENROWSET requires a literal string. You'll need to use dynamic SQL and safely inject the value in.":

DECLARE @imagepath nvarchar(255) = N'C:\Temp\YourImage.png', --Obviously you have set elsewhere.
        @Id int = 1; --Obviously you have set elsewhere.

DECLARE @SQL nvarchar(MAX),
        @CRLF nchar(2) = NCHAR(13) + NCHAR(10);
SET @SQL = N'INSERT INTO dbo.test(Id, logo)' + @CRLF +
           N'SELECT @Id, BulkColumn' + @CRLF +
           N'FROM OPENROWSET(BULK '''+ REPLACE(@imagepath,'''','''''') +''', SINGLE_BLOB) AS img;';

EXEC sys.sp_executesql @SQL, N'@Id int', @Id; --I have guessed the datatype of @Id

It seems to OP isn't familiar with how to CREATE or ALTER a procedure; I must admit I find this odd considering that they have a CREATE statement in their question, but nevermind.

To create or alter a procedure you use the CREATE or ALTER command; in more recent versions there is also CREATE OR ALTER as well.

In simple terms it's like this in psuedo SQL:

CREATE OR ALTER PROC {Your Procedure Name} {Parameters} AS
BEGIN

    {Stored Procedure SQL statements}
END;

So, you might want a stored procedure that returns for a specific customer ID; that would be

CREATE OR ALTER PROC dbo.GetCustomer @ID int AS
BEGIN

    SELECT CustomerName,
           CustomerEmail,
           CustomerPhone
    FROM dbo.Customer
    WHERE ID = @ID;
END;

For what I have you, just do this:

CREATE OR ALTER PROC  dbo.InsertImage @ID int, @imagepath nvarchar(255) AS
BEGIN 
    DECLARE @SQL nvarchar(MAX),
            @CRLF nchar(2) = NCHAR(13) + NCHAR(10);
    SET @SQL = N'INSERT INTO dbo.test(Id, logo)' + @CRLF +
               N'SELECT @Id, BulkColumn' + @CRLF +
               N'FROM OPENROWSET(BULK '''+ REPLACE(@imagepath,'''','''''') +''', SINGLE_BLOB) AS img;';

    EXEC sys.sp_executesql @SQL, N'@Id int', @Id; --I have guessed the datatype of @Id


来源:https://stackoverflow.com/questions/64684914/cannot-bulk-load-the-file-imagepath-does-not-exist-or-you-dont-have-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!