0
USE [NganHangCauHoi]
GO
/****** Object:  StoredProcedure [dbo].[ThemCauHoi]    Script Date: 3/3/2023 8:40:57 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ThemCauHoi]
    @MaCauHoi NVARCHAR(500),
    @NhomCauHoi VARCHAR(500),
    @IDMonHoc INT,
    @FilePath NVARCHAR(500)
AS
BEGIN
    INSERT INTO TblCauHoi (MaCauHoi, NhomCauHoi, IDMonHoc, NoiDung)
    SELECT @MaCauHoi, @NhomCauHoi, @IDMonHoc, *
    FROM OPENROWSET(BULK ''' + + @FilePath + ''', SINGLE_BLOB) AS NoiDung
END

EXEC ThemCauHoi 
    @MaCauHoi = "0000001",
    @NhomCauHoi = 'C1',
    @IDMonHoc = 5,
    @FilePath = 'E:\Example.docx'

It get the message

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

The path is correct and the file is there, what about the file access rights.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Norm4l
  • 41
  • 1
  • 4
  • 1
    Not sure that gets interpolated in a string. – tadman Mar 03 '23 at 02:45
  • 2
    The filepath/URI supplied to `BULK` must be a string constant. You can't use a scalar variable there nor concatenate string segments. Seems like you'll need to use dynamic SQL via `sp_executesql`. – AlwaysLearning Mar 03 '23 at 02:47

0 Answers0