Hi, I am not very comfortable with TSQl, I need help with the code below:
This procudure:
drops and recreates table BlankData if table exists; creates it anew otherwise.
--.
-. loads all xml documents in directory specified by @path into BlankData.
Now I want the proc to:
-. add checks for the following conditions, with suitable messages
1 -. failed "exec xp_cmdshell @cmd" command
2 -. @path's referencing a directory that's devoid of .xml
files
3 -. failed attempts to read .xml files
4 -. failed "select name from #filenames where name
like '%.xml'" command
5 -. failed "exec (@sql)" command6 - collapse all sp_Load<documentXX>toDB procedures to a single parameterized procedure
7 - add a second parameter that:
specifies qualifying name of
table from which to load documents
defaults to value given by a new"current epoch" function
The concern here is supporting multi-schema operation by allowing for extraction of different versions of control supporting choice documents (for different schema versions) from different tables in a set of related tables: The Proc
CREATE PROCEDURE [dbo].[LoadComplexChoiceValuesToDB]
@path varchar(256)
AS
BEGIN
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
-- To update the currently configured value for advanced options.
RECONFIGURE
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
-- To update the currently configured value for this feature.
RECONFIGURE
SET NOCOUNT ON;
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'ComplexChoiceValues'))
BEGIN
print('exist')
drop table fas.dbo.[ComplexChoiceValues]
END
create table ComplexChoiceValues(id int identity(1,1),fileName
varchar(256),complexChoiceFile xml)
declare @cmd varchar(256)
set @cmd = 'dir /b ' +'"'+ @path+'"'
create table #filenames(name varchar(256))
insert into #filenames
exec xp_cmdshell @cmd
declare @file nvarchar(256)
declare fileNameCursor CURSOR SCROLL FOR
select name from #filenames where name like '%.xml'
open fileNameCursor
fetch next from fileNameCursor
into @file
WHILE @@FETCH_STATUS = 0
begin
declare @sql varchar(max)
--insert into fas.dbo.SampleData(fileName)
values (@file)
set @sql =
'insert into [fas].[dbo].[ComplexChoiceValues]
select '''+@file+''', * from openrowset
(BULK N'''+@path+'\'+@file+''', SINGLE_BLOB) as xmlfile'
exec (@sql)
FETCH NEXT FROM fileNameCursor
INTO @file
end
CLOSE fileNameCursor
DEALLOCATE fileNameCursor
DECLARE @fileCount int
select @fileCount = COUNT(*) from #filenames
print ('There are '+ convert(varchar(max),(@fileCount-1)) +
' files under the directory')
select @fileCount = COUNT(*) from BlankData
print (convert(varchar(max),@fileCount) +' xml files are imported')
select name as 'File Not Imported'
from #filenames
where name not in
(select fileName from fas.dbo.BlankData)
select fileName as 'File Imported'
from BlankData
END
GO