Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 File name with variable/pattern to import 28 tlb

Author  Topic 

azdeji
Starting Member

3 Posts

Posted - 2012-04-16 : 06:03:19
I have a stored procedures that will import 28 CSV files with different columns layout for each one, into their own table in SQL Server 2008 and a master stored procedure which will execute the other 28 store procedures.
All the CSV files are stored on the network drive in one folder.
The naming of these files will vary from week to week and I don’t want to manually change the names in the stored procedures. Each table name has a unique Identifier F1 to F28 that will be a constant before the rest of the name for example –
F1_country23
F2_region12

Can I use the unique Identifier to add an wildcard or variable to the stored proc so it will pick any file in the folder for example with "F1" or "F2" meaning format layout 1 or 2 and use the correct stored procedure like below?
WHEN 'F1' THEN 'usp_F1_ImportScript'
WHEN 'F2' THEN 'usp_F2_ImportScript'


Example SQL scripts so far –
Create PROC [dbo].[usp_F1_ImportScript]

AS

-- Check if template table already exists and if it does then delete the table
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CSVTest]') AND type in (N'U'))
DROP TABLE dbo.CSVTest

-- Create a new template table
CREATE TABLE CSVTest
(TCODE Varchar (5),
PSPNR Varchar (255),
ZZLOC Varchar (255),
ZZPLN_QTY Varchar (255))

-- Load data into table
BULK
INSERT CSVTest
FROM 'C:\Users\Desktop\F1_Book1.csv'
WITH
(
FIRSTROW = 2, -- Removes the Header Row
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)




Create PROC [dbo].[usp_F1_ImportScript]

AS

-- Check if template table already exists and if it does then delete the table
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[VCOST]') AND type in (N'U'))
DROP TABLE dbo.VCOST

-- Create a new template table
CREATE TABLE VCOST
(TCODE Varchar (4),
PSPNR Varchar (25),
ZZOBJ_NO Varchar (10))

-- Load data into table
BULK
INSERT VCOST
FROM 'C:\Users\Desktop\F2_Book2.csv'
WITH
(
FIRSTROW = 2, -- Removes the Header Row
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)





CREATE PROCEDURE [dbo].[usp_FilePicker] (@FileName VARCHAR(255))

AS
--check if @FileName is null
IF @FileName IS NULL
BEGIN
SELECT 'No Files Match' AS ERROR
RETURN
END
--Declare our dynamic sql holder
DECLARE @Cmd VARCHAR(MAX)

-- set up exec command
SET @Cmd = 'EXEC '

--Get the stored procedure to run add when statements for each
SET @Cmd = @Cmd + CASE @FileName
WHEN 'F1_Book1.csv' THEN 'usp_F1_ImportScript'
WHEN 'F2_Book2.csv' THEN 'usp_F2_ImportScript'
END

--Execute the Stored Procedure
EXEC( @Cmd )

EXEC usp_FilePicker 'F1_Book2.csv'
EXEC usp_FilePicker 'F2_Book1.csv'

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-16 : 11:35:24
We deal witth these type of logic using SSIS

Parse the file format and based on column layouts we can add expression based precedence constraints to link to multiple execute sql tasks to execute required procedure in each case.

If you're doing it in sql you need a set of case statements or use dynamic sql

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

azdeji
Starting Member

3 Posts

Posted - 2012-04-16 : 12:14:46
Hi Visakh16,

I cannot use SSIS because other users from different countries (50+ so far) may need to add/delete columns in the raw text/cvs files (for their own test environment) meaning there may be changes needed to made to the SQL scripts and with documentation any user without experience with MSSQL would be able to make simple changes.

I understand that dynamic SQL maybe the solution but I lack skill in this area and even my colleague who more proficient in this area can’t help.
Any help would appreciated.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-16 : 12:22:16
have a look at sp_executesql or exec in msdn

http://msdn.microsoft.com/en-us/library/ms188001.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -