Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Sean_B
Posting Yak Master
111 Posts |
Posted - 2007-10-11 : 11:11:20
|
| Hi,I've written a series of procedures that are writing output lines into a common table that I'm then reporting on.I've included some simplified code to show waht I've done.I was hoping someone could tell me if it is possible to remove the loop that I've created to get the report headings.I've tried to write this as a with statement, but can't quite get it right.--------CREATE TABLE JobReport( JobRunId int NOT NULL, ReportDate datetime NOT NULL, Process] varchar(200) NOT NULL, Line varchar(200) NOT NULL)INSERT INTO jobreport(jobrunid,reportdate,process,line)SELECT 1,CURRENT_TIMESTAMP,'Proc A','testline A1' unionSELECT 1,CURRENT_TIMESTAMP,'Proc A','testline A2' unionSELECT 1,CURRENT_TIMESTAMP,'Proc A','testline A3' unionSELECT 1,CURRENT_TIMESTAMP,'Proc b','testline b1' unionSELECT 1,CURRENT_TIMESTAMP,'Proc b','testline b2' UNIONSELECT 1,CURRENT_TIMESTAMP,'Proc b','testline b3' UNIONSELECT 1,CURRENT_TIMESTAMP,'Proc c','testline c1' UNIONSELECT 1,CURRENT_TIMESTAMP,'Proc c','testline c2' UNIONSELECT 1,CURRENT_TIMESTAMP,'Proc c','testline c3' UNIONSELECT 1,CURRENT_TIMESTAMP,'Proc c','testline c4' UNIONSELECT 1,CURRENT_TIMESTAMP,'Proc d','testline d1' UNIONSELECT 1,CURRENT_TIMESTAMP,'Proc d','testline d2' UNIONSELECT 1,CURRENT_TIMESTAMP,'Proc d','testline d3'SELECT IDENTITY(INT,1,1) id,headings.Process INTO #headings from(SELECT Process FROM jobreportWHERE jobrunid = 1GROUP BY Process) headingsBEGINDECLARE @max INT, @COUNT INT,@heading AS VARCHAR(200),@report VARCHAR(MAX), @jobrunid INTSET @jobrunid = 1set @report = ''SELECT @max = COUNT(*) FROM #headingsSET @count = 0WHILE @count < @maxBEGINSET @count = @count + 1SELECT @heading = h.Process FROM #headings h WHERE h.id = @count SELECT @report = @report + ' ----' + @heading + '----' + CHAR(13) SELECT @report = @report + line + CHAR(13) FROM jobcontrol.dbo.jobreport rep WHERE rep.jobrunid = @jobrunid AND rep.Process = @headingEND --------any help would be appreciated.thanksSean |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-11 : 11:24:07
|
[code]CREATE TABLE #Headings ( ID INT IDENTITY(1, 1) PRIMARY KEY, Process VARCHAR(200) )DECLARE @Heading VARCHAR(200), @Report VARCHAR(MAX), @CurrentRow INT, @MaxRow INT INSERT #Headings ( Process )SELECT ProcessFROM JobReportWHERE JobRunID = 1GROUP BY ProcessSELECT @CurrentRow = 1, @MaxRow = @@ROWCOUNT, @Report = ''WHILE @CurrentRow <= @MaxRow BEGIN SELECT @Heading = Process FROM #Headings WHERE ID = @CurrentRow SELECT @Report = @Report + ' ----' + @Heading + '----' + CHAR(13) SELECT @Report = @Report + Line + CHAR(13) FROM JobControl.dbo.JobReport WHERE JobRunID = 1 AND Process = @Heading SET @CurrentRow = @CurrentRow + 1 END[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Sean_B
Posting Yak Master
111 Posts |
Posted - 2007-10-11 : 11:46:12
|
| I was wondering if it was possible to produce the same report without using a loop (while) statemnt, but I guessthe while loop is needed.Sean |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
|
|
Sean_B
Posting Yak Master
111 Posts |
Posted - 2007-10-12 : 05:37:41
|
| Thanks for the article, very usefulSean |
 |
|
|
|
|
|
|
|