Hi,
The code below works fine:
USE [database]
GO
DECLARE @Environment AS VARCHAR(40)
DECLARE @Error AS VARCHAR (40)
DECLARE @FileName AS VARCHAR(300)
DECLARE @SQLSTRING AS VARCHAR (max)
SET @Error = 'ERROR : Server instance not recognised'
SET @Environment = CASE @@SERVERNAME
WHEN 'MG1VDBAZ01\SQLDEV01'
THEN 'mg1vdbaZ01'
WHEN 'MG1VDBAZ01\SYSTEST'
THEN 'mg1vdbaZ01'
WHEN 'MG1VDBAU02'
THEN 'mg1vdbaU02'
WHEN 'MG1VDBAP04'
THEN 'mg1vdbaP04'
ELSE
@Error
END
IF @Environment = @Error
BEGIN
PRINT @Error
RETURN
END
PRINT @Environment
-- Insert tblTaxDomicile Data
SET @FileName = '\\'+ @Environment +'\SQLDump\tblTaxDomicile.txt'
PRINT @FileName
SET @SQLSTRING =
'
BULK INSERT tblTaxDomicile
FROM '''+ @FileName +'''
WITH
(
FIELDTERMINATOR = ''|'',
ROWTERMINATOR = ''\n'',
KEEPIDENTITY,
DATAFILETYPE=''char''
)
'
PRINT @SQLSTRING
EXEC(@SQLSTRING)
GO
PRINT 'tblTaxDomicile Updated'
If however I try to repeat this code, immediately underneath, using a different table and file names, I get the error below:
"Must declare the scalar variable "@Environment".
Msg 137, Level 15, State 2, Line 6
Must declare the scalar variable "@FileName".
It's almost as though I need to refresh the variable.
Any pointers please?
The code below works fine:
USE [database]
GO
DECLARE @Environment AS VARCHAR(40)
DECLARE @Error AS VARCHAR (40)
DECLARE @FileName AS VARCHAR(300)
DECLARE @SQLSTRING AS VARCHAR (max)
SET @Error = 'ERROR : Server instance not recognised'
SET @Environment = CASE @@SERVERNAME
WHEN 'MG1VDBAZ01\SQLDEV01'
THEN 'mg1vdbaZ01'
WHEN 'MG1VDBAZ01\SYSTEST'
THEN 'mg1vdbaZ01'
WHEN 'MG1VDBAU02'
THEN 'mg1vdbaU02'
WHEN 'MG1VDBAP04'
THEN 'mg1vdbaP04'
ELSE
@Error
END
IF @Environment = @Error
BEGIN
PRINT @Error
RETURN
END
PRINT @Environment
-- Insert tblTaxDomicile Data
SET @FileName = '\\'+ @Environment +'\SQLDump\tblTaxDomicile.txt'
PRINT @FileName
SET @SQLSTRING =
'
BULK INSERT tblTaxDomicile
FROM '''+ @FileName +'''
WITH
(
FIELDTERMINATOR = ''|'',
ROWTERMINATOR = ''\n'',
KEEPIDENTITY,
DATAFILETYPE=''char''
)
'
PRINT @SQLSTRING
EXEC(@SQLSTRING)
GO
PRINT 'tblTaxDomicile Updated'
If however I try to repeat this code, immediately underneath, using a different table and file names, I get the error below:
"Must declare the scalar variable "@Environment".
Msg 137, Level 15, State 2, Line 6
Must declare the scalar variable "@FileName".
It's almost as though I need to refresh the variable.
Any pointers please?