2
Könnten Sie mir bitte helfen, den folgenden CP so neu schreiben, wenn es aus der Datei liest, um andere Zeichen außer Zahlen zu entfernen: was es tut, liest eine Datei zum Beispiel block.txt und fügt jede Zeile in den Tisch.Entfernen Sie alle Zeichen außer Zahlen
USE [db_Test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Cp_ImportBlackList]
AS
BEGIN
BEGIN TRY
BEGIN TRANSACTION Acbc
DECLARE @command nvarchar(max)
DECLARE @delcommand varchar(200)
DECLARE @txtfile nvarchar(200)
DECLARE @line nvarchar(2)
DECLARE @andreas nvarchar(200)
set @line='\n'
DECLARE @isExists INT
DECLARE @tempcol int
DECLARE MyCursor cursor fast_forward for
Select Users_ID from Prj_Users
open MyCursor
fetch next from MyCursor
into @tempcol
while @@fetch_status = 0
begin
set @txtfile = 'c:\BlackList\ ' + LTRIM(RTRIM(str(@tempcol))) + '.txt'
exec master.dbo.xp_fileexist @txtfile,
@isExists OUTPUT
if (@isExists =1)
begin
BEGIN TRY
BEGIN TRANSACTION ad
set @command=' BULK INSERT Prj_TempBlackList FROM ''' + @txtfile + ''''
set @command += ' WITH(ROWTERMINATOR = '''+ @line +''')'
print @command
EXECUTE(@command)
delete Prj_TempBlackList where Tell in(select [BLList_TEll] from prj_BlackList where [BLList_UserID] = @tempcol)
insert into prj_BlackList select DISTINCT Tell,@tempcol from Prj_TempBlackList where Tell not in(select [BLList_TEll] from prj_BlackList where [BLList_UserID] = @tempcol)
delete from Prj_TempBlackList
set @delcommand ='del ' + @txtfile
exec xp_cmdshell @delcommand
print 'end'
COMMIT TRANSACTION ad
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION ad
SELECT ERROR_Message() AS ErrorNumber;
END CATCH
end
else
print 'no'
fetch next from MyCursor
into @tempcol
end
close MyCursor
deallocate MyCursor
set @txtfile = 'c:\BlackList\block.txt'
exec master.dbo.xp_fileexist @txtfile,
@isExists OUTPUT
if (@isExists =1)
begin
BEGIN TRY
BEGIN TRANSACTION ada
set @command=' BULK INSERT Prj_TempBlackList FROM ''' + @txtfile + ''''
set @command += ' WITH(ROWTERMINATOR = '''[email protected]+''')'
EXECUTE(@command)
delete Prj_TempBlackList where Tell in(
select [BLList_TEll] from prj_BlackList where [BLList_UserID] is null)
insert into prj_BlackList SELECT DISTINCT Tell,null from
Prj_TempBlackList where Tell not in(
select [BLList_TEll] from prj_BlackList where [BLList_UserID] is null)
delete from Prj_TempBlackList
set @delcommand ='del ' + @txtfile
exec xp_cmdshell @delcommand
print 'yes'
COMMIT TRANSACTION ada
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION ada
SELECT ERROR_Message() AS ErrorNumber;
END CATCH
end
COMMIT TRANSACTION Acbc
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION Acbc
END CATCH
END
Wenn die Daten in a Datei, warum verwenden Sie SQL Server für diesen Zweck? Es gibt viele bessere Werkzeuge für die Verarbeitung von Dateien. –
Ist es erforderlich, Ihren gesamten ETL-Prozess in einer gespeicherten Prozedur auszuführen? Es ist kein effizienter Weg. Sie wären viel besser dran, wenn Sie etwas wie SSIS verwenden, wo Sie Ihre Datei laden, und dann eine Skriptkomponenten-Umwandlung, um die Regex-Funktionen von .NET zu nutzen ([ähnlich dem] (http://www.codeproject.com/Tips/531971)/SSIS-Transform-Data-Using-Regex)), dann laden Sie in Ihre Zieltabelle. – GarethD
@iman, Wenn meine Antwort Ihr Problem gelöst hat, akzeptieren Sie es, indem Sie auf das Annahmetest unter dem Abstimmungszähler klicken. [Bitte lesen Sie: jemand-Antworten] (http://stackoverflow.com/help/someone-answers). Danke! – Shnugo