Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 26th, 2004, 07:52 AM
Authorized User
 
Join Date: Apr 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default manipulate variable

Hello there!
I would like to retrieve a recordset but for every row I need to pass a new argument that is the return value in a specific table in the DB.
The only way is to assign the retrieved value to a variable and then continue the sql query with parameter this variable.
The problem is that I can not assign to that variable each time a new value (during the select execution). Any ways to achieve that?!

Best,
Theodore.


 
Old May 26th, 2004, 01:23 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sorry, I am not sure what you are trying to achieve. Can you explain it a bit clearer? May be with some examples?

_________________________
-Vijay G
Strive for Perfection
 
Old May 28th, 2004, 03:27 AM
Authorized User
 
Join Date: Apr 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi again!
Thanks for the interest but I found the solution!
I re-state the problem:
Suppose you have the following table but the COUNTRY and WRITER can be retrieved only by ID (this is going to be the variable I will describe later)
ID COUNTRY WRITER
1 Greece
1 Kostas
2 Ireland
2 Fiona

Solution:

-- drop table in case it exists
drop table movie_table
-- create table
CREATE TABLE movie_table
(
Country varchar(200),
Writer varchar(200),
)


-- Declare the variables to store the values returned by FETCH.
DECLARE @system_name_variable varchar(50)

DECLARE authors_cursor CURSOR FOR
SELECT id
FROM table_1

OPEN authors_cursor
-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement.

    FETCH NEXT FROM authors_cursor
    INTO @system_name_variable

     -- Check @@FETCH_STATUS to see if there are any more rows to fetch.
     WHILE @@FETCH_STATUS = 0
     BEGIN
       -- Concatenate and display the current values in the variables.
      insert into movie_table
      select
             Country = (select country
                           from table_1 where id =@system_name_variable),
             Writer = (select writer
                           from table_1 where id=@system_name_variable),
      FROM table_1

        -- This is executed as long as the previous fetch succeeds.
        FETCH NEXT FROM authors_cursor
        INTO @system_name_variable
     END

CLOSE authors_cursor
DEALLOCATE authors_cursor
GO

select * from movie_table
go
--------------------------------------------------------------

Hope this helps guys!
Regards,
Theodore.







Similar Threads
Thread Thread Starter Forum Replies Last Post
How to manipulate diagramms? zip Crystal Reports 1 June 12th, 2006 11:20 AM
How to Manipulate Download Dlg BrianWren ASP.NET 1.x and 2.0 Application Design 0 July 7th, 2005 10:23 AM
how to manipulate recordsets allang PHP Databases 1 September 25th, 2004 01:21 PM
manipulate recordset k2_andy Access VBA 1 May 30th, 2004 05:46 PM
how to manipulate recordset allang Classic ASP Databases 1 November 3rd, 2003 05:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.