473,486 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Error handling in some of TSQL commands. An example is how to handle errors in the EX

2 New Member
Hi all,

while trying to hone my skils on T-SQL, i came across a procedure that

Load blank per-activity data files for multi-instance activities to a DB's BlankData table, then it should be

-- creating this table if BlankData isn't in the DB and recreating it otherwise.

THE PROCEDURE IS AS BELOW)

-- Directory containing files to load specified as a @path argument to this procedure.

-- Directory containing files to load specified as a @path argument to this procedure.

MY QUESTIONS:

1. should procedure warn if table already present before deleting BlankData?

2. HOW DO I :

-- -. add checks for the following conditions, with suitable messages
-- -. failed "exec xp_cmdshell @cmd" command
-- -. @path's referencing a directory that's devoid of .xml files
-- -. failed attempts to read .xml files
-- -. failed "select name from #filenames where name like '%.xml'" command
-- -. failed "exec (@sql)" command

3. -. should option be added for writing messages to a log?

4. HOW TO:

collapse all sp_Load<documentXX>toDB procedures to a single,
-- parameterized procedure

5.HOW DO I SPECIFY A SECOND PARAMAMETER TO
-- -. specifies qualifier (e.g., 201308) for table from which to load documents
-- -. defaults to value given by a new "current epoch" function
-- concern here is supporting multi-schema operation by allowing for extraction of different schema versions' of
-- blank documents from different tables in a set of related tables:
-- e.g.., BlankData_201308, BlankData_201309...

THE PROCEDURE

Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE [dbo].[sp_LoadBlankDataToDB] 
  2.     @path varchar(256)
  3. AS
  4. BEGIN
  5.     -- To allow advanced options to be changed.
  6.     EXEC sp_configure 'show advanced options', 1
  7.  
  8.     -- To update the currently configured value for advanced options.
  9.     RECONFIGURE
  10.  
  11.     -- To enable the feature.
  12.     EXEC sp_configure 'xp_cmdshell', 1
  13.  
  14.     -- To update the currently configured value for this feature.
  15.     RECONFIGURE
  16.  
  17.     SET NOCOUNT ON;
  18.     IF (EXISTS (SELECT * 
  19.                  FROM INFORMATION_SCHEMA.TABLES 
  20.                  WHERE TABLE_SCHEMA = 'dbo' 
  21.                  AND  TABLE_NAME = 'BlankData'))
  22.     BEGIN
  23.         print('exist')
  24.          drop table fas.dbo.[BlankData]
  25.     END
  26.  
  27.     create table BlankData(id int identity(1,1),fileName varchar(256),blankFile xml)
  28.  
  29.     declare @cmd varchar(256)
  30.      set @cmd = 'dir /b ' +'"'+ @path+'"'
  31.     create table #filenames(name varchar(256))
  32.  
  33.     insert into #filenames 
  34.     exec xp_cmdshell @cmd
  35.     declare @file nvarchar(256)
  36.  
  37.     declare fileNameCursor CURSOR SCROLL FOR
  38.     select name from #filenames where name like '%.xml'
  39.  
  40.     open fileNameCursor
  41.     fetch next from fileNameCursor
  42.     into @file
  43.  
  44.     WHILE @@FETCH_STATUS = 0
  45.  
  46.     begin
  47.         declare @sql varchar(max)
  48.  
  49.         --insert into fas.dbo.SampleData(fileName) values (@file)
  50.  
  51.         set @sql = 
  52.         'insert into [fas].[dbo].[BlankData]
  53.         select '''+@file+''', * from openrowset (BULK N'''+@path+'\'+@file+''', SINGLE_BLOB) as xmlfile'
  54.  
  55.         exec (@sql)
  56.         FETCH NEXT FROM fileNameCursor
  57.         INTO @file
  58.  
  59.     end
  60.  
  61.     CLOSE fileNameCursor
  62.     DEALLOCATE fileNameCursor
  63.  
  64.     DECLARE @fileCount int
  65.     select @fileCount = COUNT(*) from #filenames
  66.     print ('There are '+ convert(varchar(max),(@fileCount-1)) + ' files under the directory')
  67.     select @fileCount = COUNT(*) from BlankData
  68.     print (convert(varchar(max),@fileCount) +' xml files are imported')
  69.  
  70.     select name as 'File Not Imported'
  71.     from #filenames
  72.     where name not in 
  73.         (select fileName from fas.dbo.BlankData)
  74.  
  75.     select fileName as 'File Imported'
  76.     from BlankData
  77.  
  78. END
  79. GO
Jul 13 '13 #1
2 1397
Rabbit
12,516 Recognized Expert Moderator MVP
Please use code tags when posting code.

We only allow one question per thread on the forum. So I will answer the first one.

As far as whether or not you should warn the user, that's up to you. It depends on your requirements. For some people, they need to keep the old data, for others, they don't because they're only using it as a staging table or it's just temporary data. Whether or not you want to warn the user depends on which camp you fall into.
Jul 13 '13 #2
yommy1831
2 New Member
Thanks for your response.
Jul 14 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
6641
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
4
2438
by: atv | last post by:
Whatis the proper way to handle errors from function calls? For example, i normally have a main function, with calls to mine or c functions. Should i check for errors in the functions called...
1
1861
by: GS | last post by:
Any points of what would be the good error handling design for application? User error handling in Application_OnError and throw() new errors on conditions through the code? I'd like utlimiately to...
7
2326
by: Garth Wells | last post by:
I'm trying to create a DAL and am wondering what's the proper way to handle errors in this Insert method. public string Insert() { Database db = DatabaseFactory.CreateDatabase(); string...
1
1161
by: ss | last post by:
Hi Can anyone show a sample code where the error handling is done with the logging the errors. What are the best methods of logging the errors in .net perticularly in WEB Application ...
3
4001
by: Wolfram | last post by:
I have a problem with displaying errors in an embedded situation. The "main program" I want to embed Python into is a windows, MFC, non-console, C++ application. My issue is that I have not been...
4
1943
by: John Wright | last post by:
I need some good ideas or references for robust error handling in VB.NET. I am using try catch but find myself using the generic exception handler. I would like to get more precise error handling...
35
3734
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
2
1862
by: Omar Abid | last post by:
Reason of this project: Error handling is one of the most difficult thing that may afford a programmer. It isn't as easy as you think and handling errors in a program some time can make errors...
16
5027
by: john6630 | last post by:
Coming from the .Net world, I am used to the try...catch...finally approach to error handling. And PHP 5 now supports this approach. But I am not clear what happens to unhandled errors/exceptioins?...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4863
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.