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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Help me construct a single resulting set

Author  Topic 

ten2the6
Starting Member

7 Posts

Posted - 2004-10-06 : 23:26:55
Here is a T-SQL segiment of my code that works when printed. Please review and run to see the result. What I would like to do is to be able to store the result of what I have printed in an aliased table with a row for each id.


declare @name_ids varchar(30)
set @name_ids = '78764 78758 45525 45465 78764 45525 78758 78755'

declare @count int
declare @start int
set @count = 0
set @start = 1
while(@count < len(@name_ids)/5)
begin
print substring(@name_ids,@start,5)
set @start = (@start + 6)
set @count = (@count + 1)
end

-------------------------------------------------------
Gives the following result

78764
78758
45525
45465
78764
45525
78758
78755

_________________________________________________________


Now what i would like is to store the above in a result set that I can sort, count, select our of, .... just like i can out of a table.
I do not want to use Temp tables to store these values.

I basically want the result to be desplayed in a Grid as a table would rather than the multiple select statements that produced the above result. Can I somehow join each id with something like a union to comeup with a single result set that I can operate on?

Please advise.

Thanks in advance!

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-10-06 : 23:39:36
Why wouldn't you use a temp table for this??? You could use a table function.

From Books Online:


C. Multi-statement table-valued function
Given a table that represents a hierarchical relationship:

CREATE TABLE employees (empid nchar(5) PRIMARY KEY,
empname nvarchar(50),
mgrid nchar(5) REFERENCES employees(empid),
title nvarchar(30)
)

The table-valued function fn_FindReports(InEmpID), which -- given an Employee ID -- returns a table corresponding to all the employees that report to the given employee directly or indirectly. This logic is not expressible in a single query and is a good candidate for implementing as a user-defined function.

CREATE FUNCTION fn_FindReports (@InEmpId nchar(5))
RETURNS @retFindReports TABLE (empid nchar(5) primary key,
empname nvarchar(50) NOT NULL,
mgrid nchar(5),
title nvarchar(30))
/*Returns a result set that lists all the employees who report to given
employee directly or indirectly.*/
AS
BEGIN
DECLARE @RowsAdded int
-- table variable to hold accumulated results
DECLARE @reports TABLE (empid nchar(5) primary key,
empname nvarchar(50) NOT NULL,
mgrid nchar(5),
title nvarchar(30),
processed tinyint default 0)
-- initialize @Reports with direct reports of the given employee
INSERT @reports
SELECT empid, empname, mgrid, title, 0
FROM employees
WHERE empid = @InEmpId
SET @RowsAdded = @@rowcount
-- While new employees were added in the previous iteration
WHILE @RowsAdded > 0
BEGIN
/*Mark all employee records whose direct reports are going to be
found in this iteration with processed=1.*/
UPDATE @reports
SET processed = 1
WHERE processed = 0
-- Insert employees who report to employees marked 1.
INSERT @reports
SELECT e.empid, e.empname, e.mgrid, e.title, 0
FROM employees e, @reports r
WHERE e.mgrid=r.empid and e.mgrid <> e.empid and r.processed = 1
SET @RowsAdded = @@rowcount
/*Mark all employee records whose direct reports have been found
in this iteration.*/
UPDATE @reports
SET processed = 2
WHERE processed = 1
END

-- copy to the result of the function the required columns
INSERT @retFindReports
SELECT empid, empname, mgrid, title
FROM @reports
RETURN
END
GO

-- Example invocation
SELECT *
FROM fn_FindReports('11234')
GO



MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

kselvia
Aged Yak Warrior

526 Posts

Posted - 2004-10-07 : 00:04:14
Or take a look at this article: http://www.sqlteam.com/item.asp?ItemID=2652

--Ken
I want to die in my sleep like my grandfather, not screaming in terror like his passengers.
Go to Top of Page

ten2the6
Starting Member

7 Posts

Posted - 2004-10-08 : 11:33:57
quote:
Originally posted by kselvia

Or take a look at this article: http://www.sqlteam.com/item.asp?ItemID=2652

--Ken



Ken,

What a link!! A single statement took care of all my needs. No loops, no Temp tables, no table variables ....

Thanks a lot!
Go to Top of Page
   

- Advertisement -