Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8858703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:53:19+00:00 2026-06-14T14:53:19+00:00

I have this query: SELECT name, lastname FROM contestant WHERE name= ‘John’ AND lastname

  • 0

I have this query:

SELECT name, lastname
FROM contestant 
WHERE  name= 'John'  AND lastname = 'Smith'

I get several results from the query above and I need to use them for the following query:

SELECT  name, lastname,prize, city 
FROM draw
WHERE  name= name from table contestant  AND lastname= name from table contestant  

Now I’m building a table valued function with a cursor and a WHILE so I can have a table with the results.

Here’s my try, can you please help me complete it? it will be very helpful to me in order to understand this TSQL chapter. Thanks!

CREATE FUNCTION [dbo].[myFunction]
(
    @name varchar (44),
    @lastname varchar (44) 
)
RETURNS 
@tmpTable TABLE 
(   
    name char(44),
    lastname char(44),
    prize varchar(44),
    city char(44)

)
AS
BEGIN

    DECLARE 
/* what do I have to input here */

    DECLARE myCursor CURSOR FOR

SELECT name, lastname
FROM contestant 
WHERE  name= @name  AND lastname = @lastname

    OPEN myCursor

    FETCH NEXT FROM myCursor INTO  /* what goes here?*/



    WHILE (@@FETCH_STATUS = 0) 
    BEGIN

 -- and here? 

        FETCH NEXT FROM myCursor INTO /* what goes here?*/


    END /*WHILE*/

    CLOSE myCursor
    DEALLOCATE myCursor


    INSERT INTO @tmpTable (name, lastname,prize, city)
    SELECT name, lastname,prize, city 
        FROM prize
        WHERE name = @name AND lastname = @lastname

    RETURN
END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:53:20+00:00Added an answer on June 14, 2026 at 2:53 pm

    OK as long as you understand that:

    1. The table designs are incorrect – you should have a contestant key in both tables.
    2. A join is the solution to this, not a cursor
    3. What I am providing here is the worst possible way to solve this and what you need to learn from this is that you should never use this as a solution to this problem!

    But in answer to your question how do I use a cursor, here is some untested code that hopefully gives you the concept.

    CREATE FUNCTION [dbo].[myFunction]
    (
    @name varchar (44),
    @lastname varchar (44) 
    )
    RETURNS 
    @tmpTable TABLE 
    (   
    name char(44),
    lastname char(44),
    prize varchar(44),
    city char(44)
    )
    AS
    BEGIN
    
    DECLARE @c_name varchar (44)
    DECLARE @c_lastname varchar (44) 
    
    
    DECLARE myCursor CURSOR FOR
    
    SELECT name, lastname
    FROM contestant 
    WHERE  name= @name  AND lastname = @lastname
    
    OPEN myCursor
    
    FETCH NEXT FROM myCursor INTO  @c_name, @c_lastname
    
    WHILE (@@FETCH_STATUS = 0) 
    BEGIN
    
        -- we've found a row. Name look for the matching row in prize
        INSERT INTO @tmpTable (name, lastname,prize, city)
        SELECT name, lastname,prize, city 
        FROM prize
        WHERE name = @c_name AND lastname = @c_lastname
    
        FETCH NEXT FROM myCursor INTO @c_name, @c_lastname
    
    END /*WHILE*/
    
    CLOSE myCursor
    DEALLOCATE myCursor
    
    RETURN
    END
    

    and as a comparison, here is the proper solution:

    SELECT draw.name, draw.lastname, draw.prize, draw.city
    FROM 
    draw
    INNER JOIN
    contestant 
    ON draw.name = contestant.name
    AND draw.lastname = contestant.lastname
    WHERE  contestant.name= 'John'  
    AND contestant.lastname = 'Smith'
    

    Its smaller, simpler and faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this working query: $q = $this->db->query('SELECT u.name FROM users u JOIN user_group
i have this sql query select * from table where name like ? but
I have a query like this: select empno,name from emp where job = 'CLERK'
I have a query like this SELECT TOWN, NAME FROM CINEMA WHERE CITY_ID =
I have this query: SELECT id, name FROM users WHERE ((id = 1) OR
I have this mysql query: SELECT CONCAT(u.lastname, ', ', u.firstname) AS Name, start.timestamp start,
I have this query: Select (Country.Name + ', ' + City.Name) as Location ...
So I have this query that works perfectly: SELECT users.*, GROUP_CONCAT(categories.category_name) AS categories FROM
I have this query: SELECT * From checkfinale where AssociateID = 51 AND `CompletedDate`
I have a simple query select t.firstname, t.lastname, t.zipcode, t.city from name_data t where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.