• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Save record field for use later

I created a stored procedure that imports a text file into SQL Server.  The attached excel spreadsheet shows the results which has different field values all in the same column.  So now I need code that will pick out just the Failed info but show with it's corresponding store.  Which means I need to save off the store from a previous record and add it to the failed record.  I don't know how to do this and could really use your help.  Please see attached.
ErrorFile.xls
0
BobRosas
Asked:
BobRosas
  • 5
  • 4
1 Solution
 
Chris LuttrellSenior Database ArchitectCommented:
Is there anything that ties the 4 records for each Poll Session together?
0
 
BobRosasAuthor Commented:
Thank you for your response.  No there is not anything that ties the 4 records.  The way I did this in access was I just read each record in the order that it was imported into the table.  Saved off the store name into a variable and if I found a failed record I added the value of the variable to it.   I don't know how to do this in SQL.
0
 
Chris LuttrellSenior Database ArchitectCommented:
Oh! First, I am not sure about Access but in SQL Server (per ANSI Standards) there is no guarantee of order of rows returned from a table unless you use an ORDER BY clause.  So  no matter how many times you think your Select is proving that you are getting the rows back in the same order you Inserted them, it is an illusion that will bite you at some time in the future.
I would add an IDENTITY field and a field to group those records or normalize and move the 3 success/fail records to a detail table with a ref back to the parent with the name and date.
For any query I could write to try to group the records as-is it would have to have something to do an ORDER BY with and it just does not exist yet.  Can you change the existing table and process that loads into it?
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
BobRosasAuthor Commented:
Thanks for the info.  I thought I could easily just add an identity field to the table and it would just work but I get this error...
     Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (LoadLogID).

Is there an easy way to fix the following code so it will at least import correctly?

ALTER PROCEDURE [dbo].[sp_StoresPollingErrs]

AS
declare @x char(50) = '\\MED-WKS-21\Txt\' + replace(convert(varchar, DATEADD(d, -1,getdate()), 101), '/', '') + '.txt'

declare @sql varchar(max) = 'BULK INSERT dbo.tblLoadLogErrors from '''+rtrim(@x)+''' WITH (FIELDTERMINATOR = ''-'', ROWTERMINATOR = ''\n'')'
DELETE FROM dbo.tblLoadLogErrors
print @sql
exec (@sql)
0
 
BobRosasAuthor Commented:
Instead of changing my bulk insert I just created another procedure that moves the data from tblLoadLogErrorsInitial to tblLoadLogErrors and adds an ID
DROP TABLE dbo.tblLoadLogErrors
CREATE TABLE dbo.tblLoadLogErrors (
   [LoadLogID] [int] IDENTITY(1,1) NOT NULL,
   [PollDate] [varchar](50) NULL,
   [PollTime] [varchar](255) NULL,
   [Store] [varchar](255) NULL,
) ON [PRIMARY]
 
INSERT INTO dbo.tblLoadLogErrors(PollDate, PollTime, Store)
   SELECT PollDate, PollTime, Store
   FROM dbo.tblLoadLogErrorsInitial

Now that I have that, can you show me how I use the id to move the success/fail records to a third table?   I really appreciate your help so I've increased points.
0
 
Chris LuttrellSenior Database ArchitectCommented:
Putting it into a second table still does not guarantee the order of the rows from the first table, as I described above.
I set up a test and this worked for me.  First I altered the table to add an Idendity Field
ALTER TABLE dbo.tblLoadLogErrors ADD Id INT IDENTITY (1,1)
GO

Open in new window

Next I created a view that only exposed the original 3 columns (to facilitate Bulk Insert requiremetns)
CREATE VIEW dbo.vwLoadLogErrors AS SELECT Date, Time, Store FROM dbo.tblLoadLogErrors;

Open in new window

Then I altered your Procedure to insert into the View instead of Directly into the table
ALTER PROCEDURE [dbo].[sp_StoresPollingErrs]

AS
declare @x char(50) = '\\MED-WKS-21\Txt\' + replace(convert(varchar, DATEADD(d, -1,getdate()), 101), '/', '') + '.txt'

declare @sql varchar(max) = 'BULK INSERT dbo.vwLoadLogErrors from '''+rtrim(@x)+''' WITH (FIELDTERMINATOR = ''-'', ROWTERMINATOR = ''\n'')'
DELETE FROM dbo.tblLoadLogErrors
print @sql
exec (@sql) 
GO

Open in new window

Now the table has an Identity auto set and incremented on each load and this query seems to give the results you are looking for
SELECT RIGHT(Header.Date, 12) AS PollDate, Header.Store, Detail.Type, Detail.Error
FROM dbo.tblLoadLogErrors Header
INNER JOIN 
(
SELECT        Id, Time AS Type, Store AS Error
FROM            tblLoadLogErrors
WHERE        (LTRIM(Store) = 'Failed') AND (LTRIM(Time) <> 'Time')
) Detail ON Detail.Id BETWEEN Header.Id AND Header.Id + 3
WHERE Header.Date <> 'Z1'
;

Open in new window

Final_Query_ResultsI hope that is what you are looking for.
-- Chris
0
 
BobRosasAuthor Commented:
WOW!  Yes!  That works great!  Thank you so much!  I've maxed out points.
0
 
Chris LuttrellSenior Database ArchitectCommented:
Thank you.  I am happy to help.  The fun part is solving a new problem and someone appreciating it.
0
 
BobRosasAuthor Commented:
If you are interested -  I have another question regarding how to package up all the code you gave me.  It's so nice to find someone so knowledgable and helpful!
Thanks!

http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL_Server_2008/Q_27991404.html
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now