all groups > sql server programming > september 2007 >
You're in the

sql server programming

group:

How to handle error in a sproc


How to handle error in a sproc Marc Robitaille
9/6/2007 11:01:50 PM
sql server programming:
Hello,

I have this sproc to insert csv files in my tables. First, It truncates the
table. Afther that, it inserts the csv file.

CREATE PROCEDURE [dbo].[I_TDE_Imports]
@FilePath VARCHAR(500),
@FileName VARCHAR(40),
@TableName VARCHAR(6)
AS

DECLARE @sql NVARCHAR(2000)
DECLARE @Delete NVARCHAR(40)

SET @Delete = 'TRUNCATE TABLE ' + @TableName

EXECUTE sp_executesql @Delete

SET @sql = 'INSERT INTO ' + @TableName + ' SELECT * FROM
OPENROWSET(''MSDASQL'', ''Driver={Microsoft Text Driver (*.txt;
*.csv)};DefaultDir=' + @FilePath + ';'',''SELECT * FROM ' + @FileName +
''')'

EXECUTE sp_executesql @sq


What I need is to know how to handle errors? When I pass a wrong filepath,
filename or a table name, I get this error:

Server: Msg 7399, Level 16, State 1, Line 1
OLE DB provider 'MSDASQL' reported an error. The provider did not give any
information about the error.

So I tried this

CREATE PROCEDURE [dbo].[I_TDE_Imports]
@FilePath VARCHAR(500),
@FileName VARCHAR(40),
@TableName VARCHAR(6)
AS

DECLARE @sql NVARCHAR(2000)
DECLARE @Delete NVARCHAR(40)
DECLARE @err INT

SET @Delete = 'TRUNCATE TABLE ' + @TableName

EXECUTE sp_executesql @Delete

SET @sql = 'INSERT INTO ' + @TableName + ' SELECT * FROM
OPENROWSET(''MSDASQL'', ''Driver={Microsoft Text Driver (*.txt;
*.csv)};DefaultDir=' + @FilePath + ';'',''SELECT * FROM ' + @FileName +
''')'


EXECUTE sp_executesql @sq
SET @err = @@ERROR

--EXECUTE @err = sp_executesql @sq

IF @err <>o
RETURN @err
ELSE
RETURN 0

This is how I call my sproc. The file does not exists

DECLARE @RC int
EXEC @RC = I_TDE_Imports 'C:\', 'A.csv', 'TDE001'
PRINT @RC

When I try the first or the second version of my sproc, I allways have this
error. I am unable to print the result of the call...

Server: Msg 7399, Level 16, State 1, Line 1
OLE DB provider 'MSDASQL' reported an error. The provider did not give any
information about the error.

What do I have to do to return the error correctly from my sproc?

Thank you

Marc R.









Re: How to handle error in a sproc TheSQLGuru
9/7/2007 12:00:00 AM
Is this sql 2k or 2k5? If the former, consider using the sp_OA...
constructs to validate files/paths prior to executing your dynamic sql. If
2k5, consider using CLR code for the same validation.

--
TheSQLGuru
President
Indicium Resources, Inc.

[quoted text, click to view]

RE: How to handle error in a sproc ML
9/7/2007 12:42:01 AM
C:\ points to a location on the server, so make sure the file is there and
that the SQL Server service account has all appropriate file system
permissions.

Also: is the SCHEMA.INI file in that folder?


The following line:

'SELECT * FROM ' + @FileName

....translates to:

SELECT * FROM A.csv

Replace the dot in the name to the hash character, so you end uo with:

SELECT * FROM A#csv


ML

---
Matija Lah, SQL Server MVP
AddThis Social Bookmark Button