Solved

Stored Procedure to iterate through a dataset

Posted on 2008-10-29
9
761 Views
Last Modified: 2012-05-05
I have the start of my procedure attached. The idea is to get all the user ids and then loop through another table using that id to find records.  I kind of have an outline written but it's telling me that I can't have column names in the BEGIN/END area.

Any ideas on how I can do this? I need all the columns listed in the select statement in the loop.  Ultimately I'd like to do something when a record is found/not found in the loop but I need to be able to get the records first.


USE [CMDB]

GO
 

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER PROCEDURE [dbo].[sptAMITimeSheetIncompleteNotify]

(@StartDate date)

AS
 

DECLARE @UserKey int
 

DECLARE UserRecords CURSOR FOR (SELECT tUser.UserKey FROM tUser (NOLOCK))
 

OPEN UserRecords

FETCH NEXT FROM UserRecords INTO @UserKey
 

WHILE @@FETCH_STATUS = 0
 

BEGIN

	SELECT	tUser.FirstName, tUser.LastName, tTimeSheet.Status, tTimeSheet.StartDate, 

	tTimeSheet.EndDate, tTimeSheet.DateCreated, tTimeSheet.DateSubmitted, tTimeSheet.DateApproved, 

	tUser.UserID, tUser.Email, tUser.UserKey

	FROM         tUser with (nolock) INNER JOIN tTimeSheet ON tUser.UserKey = tTimeSheet.UserKey 

	WHERE     (tUser.UserKey = @UserKey) AND (tTimeSheet.DateSubmitted Is Null) and (tTimeSheet.StartDate = @StartDate)
 

	Print tUser.FirstName, tUser.LastName, tTimeSheet.Status, tTimeSheet.StartDate, 

		tTimeSheet.EndDate, tTimeSheet.DateCreated, tTimeSheet.DateSubmitted, tTimeSheet.DateApproved, 

		tUser.UserID, tUser.Email, tUser.UserKey
 

FETCH NEXT FROM UserRecords INTO @UserKey
 

END
 

CLOSE UserRecords
 

DEALLOCATE UserRecords

Open in new window

0
Comment
Question by:digitalwav
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 9

Expert Comment

by:jamesgu
ID: 22832045
you cannot print out a column name

you may have to declare a variable for the column value, and set the value in your select statement

then print out
0
 
LVL 6

Assisted Solution

by:openshac
openshac earned 100 total points
ID: 22832131
Try something like this
USE [CMDB]

GO

 

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER PROCEDURE [dbo].[sptAMITimeSheetIncompleteNotify]

(@StartDate date)

AS

 

DECLARE @UserKey int

 

DECLARE UserRecords CURSOR FOR (SELECT tUser.UserKey FROM tUser (NOLOCK))

 

OPEN UserRecords

FETCH NEXT FROM UserRecords INTO @UserKey

 

DECLARE @FirstName varchar(100),

		@LastName varchar(100),

		....
 

WHILE @@FETCH_STATUS = 0

 

BEGIN

        SELECT  tUser.FirstName, tUser.LastName, tTimeSheet.Status, tTimeSheet.StartDate, 

        tTimeSheet.EndDate, tTimeSheet.DateCreated, tTimeSheet.DateSubmitted, tTimeSheet.DateApproved, 

        tUser.UserID, tUser.Email, tUser.UserKey

        FROM         tUser with (nolock) INNER JOIN tTimeSheet ON tUser.UserKey = tTimeSheet.UserKey 

        WHERE     (tUser.UserKey = @UserKey) AND (tTimeSheet.DateSubmitted Is Null) and (tTimeSheet.StartDate = @StartDate)

 

        

		SELECT  @FirstName = tUser.FirstName, @LastName = tUser.LastName, ...

        FROM         tUser with (nolock) INNER JOIN tTimeSheet ON tUser.UserKey = tTimeSheet.UserKey 

        WHERE     (tUser.UserKey = @UserKey) AND (tTimeSheet.DateSubmitted Is Null) and (tTimeSheet.StartDate = @StartDate)

 

        

		Print @FirstName, @LastName, ...

 

FETCH NEXT FROM UserRecords INTO @UserKey

 

END

 

CLOSE UserRecords

 

DEALLOCATE UserRecords

Open in new window

0
 
LVL 5

Expert Comment

by:jfmador
ID: 22832332
Hi

I don't really understand what you are trying to do with your cursor, Using a cursor that loop on each tUser to pass the UserKey in a query using the table tUser will result the same than just running the query once without using the where (tUser.UserKey = @UserKey) and will be more performant because you won't run the query several times, if you need a cursor to print out these information add a cursor on this query instead

        SELECT  tUser.FirstName, tUser.LastName, tTimeSheet.Status, tTimeSheet.StartDate,
        tTimeSheet.EndDate, tTimeSheet.DateCreated, tTimeSheet.DateSubmitted, tTimeSheet.DateApproved,
        tUser.UserID, tUser.Email, tUser.UserKey
        FROM         tUser INNER JOIN tTimeSheet ON tUser.UserKey = tTimeSheet.UserKey
        WHERE     (tTimeSheet.DateSubmitted Is Null) and (tTimeSheet.StartDate = @StartDate)

0
 
LVL 1

Author Comment

by:digitalwav
ID: 22832353
The trouble is I need to know which user has no timesheet record and which ones have one but the datesubmitted is null.  Will the query you just suggested do that?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 9

Assisted Solution

by:jamesgu
jamesgu earned 100 total points
ID: 22832424
use this query


 SELECT  tUser.columns ... --put other columns here
              , case then (tUser.UserKey Is Null) then '1' else '0' end case --1- not submit, 0 submitted but date is null
FROM         tUser left JOIN tTimeSheet ON tUser.UserKey = tTimeSheet.UserKey
                  and tTimeSheet.DateSubmitted Is Null  and tTimeSheet.StartDate = @StartDate
0
 
LVL 5

Accepted Solution

by:
jfmador earned 300 total points
ID: 22832432
you could use a left join and a CASE to determined if the Time sheet is submitted

The Case statement will return 0 (false) when date submitted is null, and the column value from tTimeSheet will be null since you are using a left join


SELECT  tUser.UserID, tUser.Email, tUser.UserKey, tUser.FirstName, tUser.LastName, 

CASE WHEN tTimeSheet.DateSubmitted is null THEN 0 ELSE 1 END as Submitted, 

tTimeSheet.Status, tTimeSheet.StartDate, tTimeSheet.EndDate, tTimeSheet.DateCreated, tTimeSheet.DateSubmitted, tTimeSheet.DateApproved,

FROM tUser LEFT JOIN tTimeSheet ON tUser.UserKey = tTimeSheet.UserKey AND tTimeSheet.StartDate = @StartDate

Open in new window

0
 
LVL 9

Expert Comment

by:jamesgu
ID: 22832461
jfmador was right

should be tTimeSheet.DateSubmitted is null instead of tUser.UserKey Is Null in my query
0
 
LVL 1

Author Closing Comment

by:digitalwav
ID: 31511212
Excellent! Between all of you I got the record set I needed. I think I can figure out the rest of the logic. this is much easier than the old fashion loop method.  Thanks!
0
 
LVL 5

Expert Comment

by:jfmador
ID: 22832540
what a team! it seems that we look at the sames questions James :)
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CROSS APPLY 4 45
Query Records that don't match 8 34
SQL Query with Sum and Detail rows 2 43
ISDATE() not working properly on my table? Any suggestions. 7 14
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

22 Experts available now in Live!

Get 1:1 Help Now