0
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
);
4

2 回答 2

1
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) 
于 2013-10-06T18:29:17.783 回答
0

这个查询怎么样?使用动态查询来执行查询。只知道单引号。

 DECLARE @path nvarchar(2000);
DECLARE @sql nvarchar(2000);
SET @path = 'c:\bulk.txt'

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

print @sql
exec sp_executesql @sql
于 2013-10-06T18:33:27.113 回答