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)
 Cursor Problem

Author  Topic 

whiteshadow
Starting Member

15 Posts

Posted - 2005-04-13 : 03:28:02
Hi,
This might be a very newbie question but I have a hard time looking for solution on this one. My problem is I have to create a cursor in which has 80+ fields; can I do a FETCH NEXT into a TABLE? If not is the only solution is to create a 80+ varibles for this one?
Thanks a million in advance for your time.

AndyB13
Aged Yak Warrior

583 Posts

Posted - 2005-04-13 : 03:32:25
First - welcome to the forum

Second - is a cursor really necessary (95% of the time they are not required)??

In order for us to help you can you post your code and what you are trying to acheive

Andy

Beauty is in the eyes of the beerholder
Go to Top of Page

whiteshadow
Starting Member

15 Posts

Posted - 2005-04-13 : 03:47:06
Hi,
Thanks for your reply. Actually I havn't started creating this stored procedure yet, I'm still looking for any idea on what is the best way to do on this. I would create a stored procedure that would accept a XML then I would do an OPEN XML on the passed XML and Insert it to a Temporary table. Then from that temporary table I have to loop from each data to parse a resulting SQL statement to be executed dynamically. The only thing that comes in my mind in looping through the records inside the Temporary table is through cursor, is there other way to do this?
Thanks a lot.
Go to Top of Page

AndyB13
Aged Yak Warrior

583 Posts

Posted - 2005-04-13 : 04:19:01
You could use a cursor or create an identity column on your temp table then do something like this

CREATE TABLE #MyTemp ([ID] int identity(1,1), SQL varchar(100))
INSERT #MyTemp (SQL)
SELECT 'SELECT * FROM Orders' UNION ALL
SELECT 'SELECT * FROM Customers' UNION ALL
SELECT 'SELECT * FROM Employees' UNION ALL
SELECT 'SELECT * FROM Products'

SELECT * FROM #MyTemp

DECLARE @ID int, @SQL varchar(100)
SET @ID = (SELECT MIN(ID) FROM #MyTemp)
WHILE @ID < 5
BEGIN
SET @SQL = (SELECT SQL FROM #MyTemp WHERE [ID] = @ID)
EXEC (@SQL)
SET @ID = @ID + 1
END


DROP TABLE #MyTemp

Andy

Beauty is in the eyes of the beerholder
Go to Top of Page

whiteshadow
Starting Member

15 Posts

Posted - 2005-04-14 : 02:40:55
Hi Andy,
Again thanks for your reply. I can't relate your sample script to solve my problem. I am still a newbie in SQL Server2000, please enlighten me on your suggestion.
Thanks a lot and more power to you.
Go to Top of Page

AndyB13
Aged Yak Warrior

583 Posts

Posted - 2005-04-14 : 04:34:24
The sample i gave you works on the northwind database and does the 2nd part of your question
quote:
Then from that temporary table I have to loop from each data to parse a resulting SQL statement to be executed dynamically. The only thing that comes in my mind in looping through the records inside the Temporary table is through cursor, is there other way to do this?

I will step through it for you

--Create a temp table #MyTemp (not a real table) - look up temp tables in BOL
--[ID] int identity(1,1) is like an autonumber column in Access

CREATE TABLE #MyTemp ([ID] int identity(1,1), SQL varchar(100))

--Insert some values into the temp table to make it work
INSERT #MyTemp (SQL)
SELECT 'SELECT * FROM Orders' UNION ALL
SELECT 'SELECT * FROM Customers' UNION ALL
SELECT 'SELECT * FROM Employees' UNION ALL
SELECT 'SELECT * FROM Products'

--Look at the contents of the temp table
SELECT * FROM #MyTemp
Results:
ID SQL
1 SELECT * FROM Orders
2 SELECT * FROM Customers
3 SELECT * FROM Employees
4 SELECT * FROM Products

--Declare variables (from here is the bit you should concentrate on)
DECLARE @ID int, @SQL varchar(100)
--Set @ID to the lowest id (starting point)
SET @ID = (SELECT MIN(ID) FROM #MyTemp)
--Do the next bit until @ID = 4 this is because i know that there are only 4 records
WHILE @ID < 5
BEGIN
--Get SQL value and put into the variable
SET @SQL = (SELECT SQL FROM #MyTemp WHERE [ID] = @ID)
--Execute the SQL string - EXEC can be replaced by PRINT to see what it would execute
EXEC (@SQL)
--Add 1 to the @ID variable
SET @ID = @ID + 1
END

--Clean up (not really necesary with temp tables)
DROP TABLE #MyTemp


Based upon my sample here is something you could work with


DECLARE @MinID int, @MaxID int, @SQL varchar(100)
SET @MinID = (SELECT MIN([ID]) FROM YourTable) --Minimum ID (1st record)
SET @MaxID = (SELECT MAX([ID]) FROM YourTable) --Maximum ID (last record)
WHILE @MinID <= @MaxID
BEGIN
SET @SQL = (SELECT YourSQLColumn FROM YourTable WHERE [ID] = @MinID)
EXEC (@SQL)
SET @MinID = @MinID + 1
END


HTH

Andy


Beauty is in the eyes of the beerholder
Go to Top of Page
   

- Advertisement -