Solved

Save record field for use later

Posted on 2013-01-08
9
207 Views
Last Modified: 2013-01-10
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
Comment
Question by:BobRosas
  • 5
  • 4
9 Comments
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 38757886
Is there anything that ties the 4 records for each Poll Session together?
0
 

Author Comment

by:BobRosas
ID: 38759493
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
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 38760526
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
 

Author Comment

by:BobRosas
ID: 38760621
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:BobRosas
ID: 38761098
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
 
LVL 26

Accepted Solution

by:
Chris Luttrell earned 500 total points
ID: 38761421
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
 

Author Comment

by:BobRosas
ID: 38763764
WOW!  Yes!  That works great!  Thank you so much!  I've maxed out points.
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 38763928
Thank you.  I am happy to help.  The fun part is solving a new problem and someone appreciating it.
0
 

Author Comment

by:BobRosas
ID: 38764280
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now