我想使用我在其他地方声明的变量,但我不能(简单的 sql 查询)

2021-09-10 00:00:00 tsql sql-server
DECLARE @path text;
SET @path = 'c:\bulk'

BULK INSERT [HumanResources].[Employee] FROM -- I Want to use the variable here !!
WITH (
    CHECK_CONSTRAINTS,
    CODEPAGE='ACP',
    DATAFILETYPE='widechar',
    FIELDTERMINATOR='\t',
    ROWTERMINATOR='\n',
    KEEPIDENTITY,
    TABLOCK
);

推荐答案

DECLARE @path nvarchar(2000);

    SET @path = 'c:\bulk.(extension)';
    DECLARE @sql NVARCHAR(MAX) =

    '''BULK INSERT [HumanResources].[Employee] FROM' + @path  + '
    WITH (
        CHECK_CONSTRAINTS,
        CODEPAGE=''ACP'',
        DATAFILETYPE=''widechar'',
        FIELDTERMINATOR=''\t'',
        ROWTERMINATOR=''\n'',
        KEEPIDENTITY,
        TABLOCK
    )'''

    EXECUTE sp_executesql(@sql) 

相关文章