Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Save record field for use later

Posted on 2013-01-08
9
Medium Priority
?
222 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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