How to get results from a table dynamically

  • I want to retrive data from a table dynamically.

    Say it contains 3 rows name,age and gender

    I want to retrieve name and gender through age.

    Instead of using Select Name,gender from table wher age=26 is there any method which first asks for the age and then displays the result

  • ash0550 (9/9/2011)


    I want to retrive data from a table dynamically.

    Say it contains 3 rows name,age and gender

    I want to retrieve name and gender through age.

    Instead of using Select Name,gender from table wher age=26 is there any method which first asks for the age and then displays the result

    Are you asking if sql server can present a user with an input box? NO. Sql server is a DBMS. There is no UI. You could create a stored procedure that has a parameter of age that would do this but you still have to capture this parameter in the front end.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (9/9/2011)


    ash0550 (9/9/2011)


    I want to retrive data from a table dynamically.

    Say it contains 3 rows name,age and gender

    I want to retrieve name and gender through age.

    Instead of using Select Name,gender from table wher age=26 is there any method which first asks for the age and then displays the result

    Are you asking if sql server can present a user with an input box? NO. Sql server is a DBMS. There is no UI. You could create a stored procedure that has a parameter of age that would do this but you still have to capture this parameter in the front end.

    thanks for the reply.

    Could you provide me the outline how to do it

  • Simple example

    The code below is what you would use to test your results

    DECLARE @clientage varchar(10)

    set @clientage = '26'

    select name, age, gender

    from customer

    where age = @clientage

    This is how it should look in a store procedure once you are happy with the results

    USE [Test]

    GO

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    Create PROCEDURE [dbo].[Age]

    -- declaring the variables to be used in the procedure

    (

    @clientage int

    )

    AS

    select name, age, gender

    from customer

    where age = @clientage

    __________________________________________________________________________________
    Steve J
    Jnr Developer
    BSc(Hon)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply