Solved

Save record field for use later

Posted on 2013-01-08
9
213 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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
 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 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