DECLARE @sqlCommand VARCHAR(MAX) DECLARE @NAME varchar (100)
DECLARE @FIRSTROW INT,@LASTROW INT
SET @FIRSTROW =
(SELECT TOP 1 [Id]
FROM [dbo].[TableB]
ORDER BY [Id] DESC) + 1
SET @LASTROW = @FIRSTROW + 4
SELECT @NAME =
(SELECT 'C:\InsertTestdata.txt')
SET NOCOUNT ON;
SET @sqlCommand = 'BULK INSERT [dbo].[TableA] FROM ''' + @NAME + '''
WITH (DATAFILETYPE = ''char'',
FIELDTERMINATOR = '','',
ROWTERMINATOR = '''+CHAR(10)+''',
FIRSTROW = 2,
LASTROW = '''+ @LASTROW +'''
);'
EXEC (@sqlCommand);
I am getting error while executing above script Anyone can explain what is the right way to give Lastrow and firstrow parameters using some variable in Bulk Insert Statement?
Something like this:
outputs