Hi,
I am loading some data into a temp table. I then use a cursor to loop through the temp table, minipulate the data, and insert it into another table. The identity of the newly inserted record is then captured in a variable and inserted to another table to allow a look up of the newly inserted data.
DECLARE c1 CURSOR READ_ONLY
FOR
SELECT NEWClientIndex, ClientGroup, ClientAccount, SKAccount, SKDesc, SKBase, SKBranch, ClientType, SKStatus, GFCID, GFPID, Account_Open_Date, Account_Update,
SKType
FROM @ClientsAccounts
OPEN c1
FETCH NEXT FROM c1
INTO @NEWClientIndex, @ClientGroup, @ClientAccount, @SKAccount, @SKDesc,
@SKBase, @SKBranch, @ClientType, @SKStatus, @GFCID, @GFPID, @Account_Open_Date,
@Account_Update, @SKType
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'Processing Account'
Print 'Inserting Account Information'
-- We now need to loop through the data inserting account information
-- and maintaining the Account link table
-- Each itteration of the cursor should result in one insert
-- as each account should be linked to one client
INSERT INTO CitiClientsAccounts (SKBranch, SKAccount, SKName, SKBase, SyncStatus,
GFCID, GFPID, SyncInput, SyncUpdate, Deleted, Branch_Account, LastUpdatedBy, AccountTypeID)
VALUES(convert(varchar(2), @SKBranch), convert(varchar(12), @SKAccount), convert(varchar(255),@SKDesc),
convert(varchar(16),@SKBase), convert(varchar(50),@SKStatus), convert(varchar(10),@GFCID), convert(varchar(10),@GFPID),
@Account_Open_Date, @Account_Update, 0, convert(varchar(16),@ClientAccount), 'Admin', convert(int, @SKType))
Declare @NEWID int
Select @NEWID = SCOPE_IDENTITY()
IF @NEWID is NULL
BEGIN
Print 'Account Insert Failed'
END
ELSE
BEGIN
--Match up account to the Client ID
Declare @ClientID int
Select @ClientID = LocalPrimaryKey
from dbo.CitiClients_LIBRARY
Where AccessPimaryKey = @NEWClientIndex
Print 'Updating Library'
Insert into dbo.ClientAccountLink
(AccountID, ClientID, LastUpdatedBy)
Values
(@NEWID, @ClientID, 'Admin')
END
Print 'End Insert Account Information'
-- Move to Next Row
FETCH NEXT FROM c1
INTO @NEWClientIndex, @ClientGroup, @ClientAccount, @SKAccount, @SKDesc,
@SKBase, @SKBranch, @ClientType, @SKStatus, @GFCID, @GFPID, @Account_Open_Date,
@Account_Update, @SKType
END
CLOSE c1
DEALLOCATE c1
At times the insert fails, I have no idea why the insert does fail. I am just wondering how I can display more detailed information on why the insert is actually failing?