I am a beginner with TSQL scripting and I need a hand to get my stored procedure to work as intended.
I want to use the @date parameter in my sp to take logg@date - in ccyymmdd format. Also, is there any security flaws or optimisation i could think of more than the Tablock? Could tablock become a problem in the future?
To clearify problem: When I do the Bulk Insert, I want the data to get into the db depending on @datum and currently it is not working as intended. Any hints would be appreciated. batch file will be running on daily schedule to insert data into dbname@datum.
Current SP code:
USE [dbname]
GO
/****** Object: StoredProcedure [dbo].[fillDbTablelogg] Script Date: 2016-02-26 16:47:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
=============================================
Author: <Author,,Name>
Create date: <Create Date,,>
Description: <Description,,>
=============================================
ALTER PROC [dbo].[fillDbname]
@datum date(8)
AS
BEGIN
SET NOCOUNT ON;
Declare @bulkInsert NVARCHAR(MAX)
Declare @pathRail nvarchar(100) = 'x:\xxxx\SYSTEMID\';
Declare @pathDate nvarchar(100) = convert(char, getdate(), 12);
--Want to use my @Date parameter instead, sending in 8 digits so ccyymmdd
Declare @fromPath NVARCHAR(MAX) = '@pathRail' + '20' + '@pathDate' + '.tab';
--should print 'x:\xxxx\SYSTEMID\pathRailccyymmdd.tab'
Declare @ErrorPath nvarchar(100) = 'x:\xxxx\SYSTEMID\err';
--x:\xxxx\SYSTEMID\err2014-11-22.12:45:34.log
Declare @ErrorNow nvarchar(100) = curdate() + '.' + curtime() + '.log';
Declare @errorFilePath NVARCHAR (100) = @ErrorPath + @ErrorNow;
--Will change this formatfilename later to something more suiting, I have a working XML format file created with bcp.
Declare @formatFile nvarchar (100) = 'x:\xxxx\SYSTEMID\dbo.nameccyymmdd.xml';
set @bulkInsert = '
BULK INSERT dbname.logg' + @datum + 'FROM' + @fromPath +'
WITH
(
FIRSTROW = 1,
ORDER (Id),
CODEPAGE = Finnish_Swedish_CI_AS,
FIELDTERMINATOR = \t,
ROWTERMINATOR = 0x0a,
TABLOCK,
ERRORFILE = @errorFilePath,
FORMATFILE = @formatFile,
--KEEPNULLS
);'
exec(@bulkInsert)
end
Fixed: Added datatypes to all variables. Made sure all variables that was used were created first. edited @datum variable to datatype date instead of varchar.