Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Adhoc import using a cursor

Author  Topic 

Jim77
Constraint Violating Yak Guru

440 Posts

Posted - 2006-12-05 : 05:19:16
--

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-05 : 05:26:04
Hve you tried using the TABLOCK option?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Jim77
Constraint Violating Yak Guru

440 Posts

Posted - 2006-12-05 : 05:31:08
No let me read BOL and get back to you, does my code look right to you though ? I am trying to to a loop for each store for each day and then onto the next day for each store!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-05 : 09:45:03
WHAT? YOU EMPTIED/ERASED YOUR POST?

It is not a nice thing to do.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Jim77
Constraint Violating Yak Guru

440 Posts

Posted - 2006-12-05 : 11:17:51
Sorry Peso found a solution I had the Deallocate statement in the wrong place in a nested cursor:

Here is my correct code:

SET NOCOUNT on
go

go

DECLARE @exists as varchar(300)
DECLARE @result int
DECLARE @failedstores as int
DECLARE @date as varchar(100)
DECLARE @sql as varchar(200)
DECLARE @storeno as smallint
DECLARE @tdate as char(3)
DECLARE @errordescrip as varchar(100)
DECLARE @rowc as int
DECLARE storecursor2 CURSOR FOR
Select store_no From
retail2 Where store_no in (SELECT storenum FROM RetailChoiseStore)
DECLARE daycursor CURSOR FOR
Select tdate From
adhoctaurex
DECLARE @textdate as varchar(200)


--drop and re-create items tables
DROP TABLE dbo.tmpisaleimportT
CREATE TABLE dbo.tmpisaleimportT ([salesdata] [char] (56)
[dayint] [int] )
DROP TABLE dbo.itemsales_stores_input_errorT
CREATE TABLE [itemsales_stores_input_errorT] (
[StoreNumber] [smallint] NULL ,
[Description] varchar(200),
[DateInserted] [datetime] NULL CONSTRAINT [DF__itemsales__Date6I2__43F17C88] DEFAULT (getdate())
) ON [PRIMARY]

--drop and re-create offer tables
DROP TABLE dbo.tmpoffersaleimportT
CREATE TABLE dbo.tmpoffersaleimportT ([salesdata] [varchar] (156)
[dayint] [int] )

DROP TABLE dbo.offersales_stores_input_errorT
CREATE TABLE dbo.[offersales_stores_input_errorT] (
[ErrorDescription] varchar(200) NULL ,
[DateInserted] [datetime] NULL CONSTRAINT [DF__offersales__Date6I2__43F17C88] DEFAULT (getdate())
) ON [PRIMARY]


OPEN daycursor
FETCH NEXT FROM daycursor INTO @tdate
WHILE @@FETCH_STATUS = 0
BEGIN

OPEN storecursor2
FETCH NEXT FROM storecursor2 INTO @storeno
WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @exists = '"' + 'DIR /B \\jy2003s\Data\isale'+ cast(@storeno as char(3))+'.'+@tdate+'"'
EXEC @result = master..xp_cmdshell @exists , no_output
-- if it does then bulk insert it into the tmpisaleimport table
IF (@result = 0)
begin

select @date =''''+ '\\jy2003s\Data\isale'+ cast(@storeno as char(3))+'.'+@tdate+''''
Set @sql='
BULK INSERT [tmpisaleimportT]
FROM '+@date
Exec(@sql)
IF @@rowcount = 0
Begin
INSERT INTO itemsales_stores_input_errorT(StoreNumber, [Description])
VALUES (@storeno,'File does exist but has no data')
End
end
ELSE
-- if it doesn't plug it into the the error table which shows all failed store imports
begin
INSERT INTO itemsales_stores_input_errorT(StoreNumber, [Description])
VALUES (@storeno,'File does not exist')
end

FETCH NEXT FROM storecursor2 INTO @storeno
END
CLOSE storecursor2




--find if the current offersale file exists in the itemsale folder
select @exists = '"' + 'DIR /B\jy2003s\Data\OffrSale.'+@tdate+'"'
EXEC @result = master..xp_cmdshell @exists
-- if it does then bulk insert it into the tmpoffersaleimport table
IF (@result = 0)
begin
select @date =''''+ '\\jy2003s\Data\OffrSale.'+@tdate+''''
Set @sql='
BULK INSERT [tmpoffersaleimportT]
FROM '+@date
Exec(@sql)
end
ELSE
-- if it doesn't plug it into the the error table which shows all failed store imports
begin
set @errordescrip = 'The offer sale file did not exist for the date specified'
INSERT INTO offersales_stores_input_errorT(ErrorDescription)
VALUES (@errordescrip)
set @rowc = @@rowcount
end

IF @rowc > 0
begin
SELECT @textdate = 'The offersales file for ' +@tdate+ 'did not get imported please check offersales_stores_input_error table to check the time of failure'
RAISERROR (@textdate,16,1)
end



FETCH NEXT FROM daycursor INTO @tdate
END


DEALLOCATE storecursor2

CLOSE daycursor
DEALLOCATE daycursor

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-05 : 11:25:48
good. but please let posts be so toher can learn and benefit from our mistakes.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Jim77
Constraint Violating Yak Guru

440 Posts

Posted - 2006-12-05 : 11:35:13
ok.
Go to Top of Page
   

- Advertisement -