ALTER PROCEDURE [dbo].[bulk_import_csv_from_dir]
-- Add the parameters for the stored procedure here
@ext varchar(10),
@likefilename varchar(max),
@tablename varchar(max),
@tabeltemplate varchar(max),
@directory varchar(8000)
AS
BEGIN
--DECLARE @daily varchar(20);
DECLARE @filename varchar(255);
DECLARE @directory_table table (
id int IDENTITY(1,1)
,subdirectory nvarchar(512)
,depth int
,isfile bit);
SET NOCOUNT ON;
BEGIN TRY
-----------------------------------------------------------------
--fill temp table with listed files
INSERT into @directory_table (subdirectory,depth,isfile)
EXECUTE master.sys.xp_dirtree @directory,1,1;
--create cursor file
execute ('IF OBJECT_ID(''cur_files'') IS NOT NULL DEALLOCATE cur_files');
SELECT subdirectory as fname FROM @directory_table
WHERE isfile = 1 AND RIGHT(subdirectory,4) = @ext
AND subdirectory like @likefilename
ORDER BY id;
open cur_files;
FETCH NEXT FROM cur_files
INTO @filename
WHILE @@FETCH_STATUS = 0
BEGIN
execute ('IF OBJECT_ID('''+@tablename+''') IS NULL SELECT * INTO '+@tablename+' FROM ' + @tabeltemplate);
declare @sql varchar(max);
SET @sql ='BULK INSERT '+@tablename+' FROM '''+@directory+@filename+'''
WITH (FIELDTERMINATOR = ''\n'', ROWTERMINATOR = '''+CHAR(10)+''', FIRSTROW = 3)';
execute(@sql);
FETCH NEXT FROM cur_files
INTO @filename
END
close cur_files;
DEALLOCATE cur_files;
-----------------------------------------------------------------
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
IF @@TRANCOUNT > 0
begin
ROLLBACK TRANSACTION;
execute ('IF OBJECT_ID(''cur_files'') IS NOT NULL DEALLOCATE cur_files');
end
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
END
这是我的商店程序 为什么总是显示((一个名称为' cur_files'的光标不存在。)如果执行。我忘了这个程序的东西?
感谢
答案 0 :(得分:1)
你永远不会声明你的光标。我怀疑你想要像
这样的东西--fill temp table with listed files
INSERT into @directory_table (subdirectory,depth,isfile)
EXECUTE master.sys.xp_dirtree @directory,1,1;
DECLARE cur_files CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR
SELECT subdirectory AS fname
FROM @directory_table
WHERE isfile = 1
AND RIGHT(subdirectory,4) = @ext
AND subdirectory LIKE @likefilename
ORDER BY id;
OPEN cur_files;
我已经删除了检查以释放游标(如果存在),因为我在声明中添加了LOCAL
选项它只存在于当前作用域中,并且因为你还没有声明它,所以它不能已经存在,所以这是一个冗余的检查。其他选项只是为了改善内存管理,并尽可能简化光标。
答案 1 :(得分:1)
这里有两个主要问题。首先是你不要声明你的光标。第二个是你使用动态sql来检查你的光标是否存在。这行永远不会做任何事情,因为动态sql在自己的范围内运行。这很容易证明。
declare cur_files cursor for select * from sys.objects
execute ('IF OBJECT_ID(''cur_files'') IS NOT NULL DEALLOCATE cur_files');
DEALLOCATE cur_files
答案 2 :(得分:0)
开始时您要取消分配 cur_files Cursor
,之后您没有声明 cur_files Cursor
,请替换
execute ('IF OBJECT_ID(''cur_files'') IS NOT NULL DEALLOCATE cur_files');
SELECT subdirectory as fname FROM @directory_table
WHERE isfile = 1 AND RIGHT(subdirectory,4) = @ext
AND subdirectory like @likefilename
ORDER BY id;
与
DECLARE cur_files CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR
SELECT subdirectory as fname FROM @directory_table
WHERE isfile = 1 AND RIGHT(subdirectory,4) = @ext
AND subdirectory like @likefilename
ORDER BY id;