Solved

SQL select statement that returns a row for every value in a while loop

Posted on 2013-01-31
9
456 Views
Last Modified: 2013-04-22
Alright I've got two tables in two separate DB's... which I am joining to write to a table in a third DB.. Every 10 miuntes I'm going to delete the values in th tird table and write from the other 2 again... becasue of a later use of the third tabel I need to show records whether or not the select statement from the joined tables returns a value...

How can I do this....

Code below....

delete CurrentlyRunning.dbo.ExpectTput -- output table which needs to have 20 records each time

declare @rpline int --counter for while loop

set @rpline = -1
while @rpline < 20
Begin
set @rpline = @rpline +1

insert into CurrentlyRunning.dbo.ExpectTput

select  
@rpline,
spwo.item,
unit_weight,
pcs_per_mch_hr,
(pcs_per_mch_hr * unit_weight)

from [spdaccess]...[work order] spwo  
inner join item on spwo.item = item.item
inner join jrt_sch on item.job = jrt_sch.job
and jrt_sch.suffix = 1
where [order status] = 5
and [End Date] is null
and @rpline = spwo.line
order by line
end

Thanks

Z
0
Comment
Question by:ztangent
[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 12

Expert Comment

by:mwochnick
ID: 38841622
any reason you can't create a view that joins the two tables and looks like the third table and query appropriately against the view instead of repeatedly copying the data into the third table?
0
 

Author Comment

by:ztangent
ID: 38841668
how would I do that... remember the output need to come back showing all 20 lines even if the line is not found in the spwo table....
0
 
LVL 12

Expert Comment

by:mwochnick
ID: 38844239
ok you have a couple of issues - your insert statement assumes that there is a row coming back from the select statement for 20 rows - so you need to some logic to handle the scenario where there isn't a row for alll 20  
second you don't any logic to insert a "blank" row if you select doesn't return a value

One way to do this would be to use a cursor like in this example to insert the valid rows
DECLARE Employee_Cursor CURSOR FOR
SELECT EmployeeID, Title 
FROM AdventureWorks2012.HumanResources.Employee
WHERE JobTitle = 'Marketing Specialist';
OPEN Employee_Cursor;
FETCH NEXT FROM Employee_Cursor;
WHILE @@FETCH_STATUS = 0
   BEGIN
      FETCH NEXT FROM Employee_Cursor;
   END;
CLOSE Employee_Cursor;
DEALLOCATE Employee_Cursor;
GO

Open in new window

from this page http://msdn.microsoft.com/en-us/library/ms178642.aspx
and then do a count of rows into a variable
select @myInt = COUNT(*) from myTable
set @myInt = (select COUNT(*) from myTable)

Open in new window

and use another while loop starting at count to insert the remaining blank rows similar to your current while loop
0
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 

Author Comment

by:ztangent
ID: 38844361
Only problem is that the counter is more than just a counter.. it actuall represents a production line in my mfg facility.. There fore the "blank" line is not really "blank" it needs to tell me what line number it didn't find and hopefully be able top write null or "0" for the tput value...

I was thinking over night... would this entire process be easier with an "upset".... instead of a delete and insert... in the destination table write 20 records... line 1 -20... all other fields null or 0... then upsert (merge?) each time I run the sp... only problem is if a line shuts down it will continue to show the last tput... when what I really needit to show at that point is 0...

Any thoughts on how to make it work this direction or am I making it harder on myself...
0
 
LVL 12

Expert Comment

by:mwochnick
ID: 38844486
With the caveat of not understanding your problem fully...I'd make the smarts of this thing in the ui that displays the results and not in the database.

It doesn't seem that you need to keep the history of this stuff in the third table so why store it?  If you don't have 1000's of clients you shouldn't have database performance issues if you only allow the clients to refresh every 10 minutes


but if you need to store it in the table then you could add some if else logic around the select to check if it has the values you want before inserting appropriately so
so inside your while loop
select fields into some variables
if the fields have the correct values
  do your insert from the select
else
  do your custom insert for with the 0's and nulls

IF - ELSE
http://msdn.microsoft.com/en-us/library/ms182717.aspx
0
 

Author Comment

by:ztangent
ID: 38844957
whats the if logic to tell me the select did not return any data...??
0
 

Author Comment

by:ztangent
ID: 38844959
I also tried thsi which failed due to syntax...

declare @rpline int
declare @rpitem nvarchar(50)
declare @rpweight float
declare @rppcsphr float
declare @rptput float


set @rpline = -1
while @rpline < 20
Begin
set @rpline = @rpline +1
set @rpitem = null
set @rpweight = 0
set @rppcsphr = 0
Set @rptput = 0

select  
@rpline,
spwo.item into @rpitem,
unit_weight into @rpweight,
pcs_per_mch_hr into @rppcsphr ,
(pcs_per_mch_hr * unit_weight) into @Rptput

from [spdaccess]...[work order] spwo  
inner join item on spwo.item = item.item
inner join jrt_sch on item.job = jrt_sch.job
and jrt_sch.suffix = 1
where [order status] = 5
and [End Date] is null
and @rpline = spwo.line
order by line

insert into CurrentlyRunning.dbo.ExpectTput

@rpline,
@rpitem,
@rpweight,
@rppcsphr,
@rptput


end
0
 
LVL 12

Accepted Solution

by:
mwochnick earned 500 total points
ID: 38845082
if the select you run returns 0 rows when empty then you could use the select count I had in the earlier post for your if logic  instead of what i had suggested later

pseudo code
count = select count().....
if count = 0 then
 insert blank
else
 insert your select
0
 

Author Closing Comment

by:ztangent
ID: 39101062
ended up using my own solution but thanks for all of the feedback and help
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.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 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