?
Solved

Save record field for use later

Posted on 2013-01-08
9
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 27

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 27

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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 27

Accepted Solution

by:
Chris Luttrell earned 2000 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 27

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

752 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