locked
ExecuteScalar RRS feed

  • Question

  • hi

    i have a query below like :

    declare @Name varchar(250)
    declare @Perno decimal
    select top (1)@Name=name , @Perno=perno
       from tbl_Users 

    now how to get with ExecuteScalar

    please help me

    thanks

    Thursday, October 18, 2012 11:10 AM

Answers

  • Hi,

    Use Execute Reader() and store the values in a variables.There is no other option to your solution.


    PS.Shakeer Hussain

    • Proposed as answer by Norkk Thursday, October 18, 2012 11:48 AM
    • Marked as answer by Jason Dot WangModerator Friday, October 26, 2012 8:32 AM
    Thursday, October 18, 2012 11:31 AM

All replies

  • Hi,

    ExecuteSclar Returns only one value, But in your query it is returning two values, so to get as one value Conctenate both variables in your query .


    PS.Shakeer Hussain

    Thursday, October 18, 2012 11:13 AM
  • thanks for your answer

    i know it

    but i must return the query,what is your solution

    please help me

    thanks

     

    Thursday, October 18, 2012 11:20 AM
  • Hi,

    Use Execute Reader() and store the values in a variables.There is no other option to your solution.


    PS.Shakeer Hussain

    • Proposed as answer by Norkk Thursday, October 18, 2012 11:48 AM
    • Marked as answer by Jason Dot WangModerator Friday, October 26, 2012 8:32 AM
    Thursday, October 18, 2012 11:31 AM
  • what is you purpose of the query? Why are you using "TOP" with NO "ORDER BY" statement. In this case, you will probably get the first row inserted each time you run the SELECT.

    You will probably have something like this where I have added a ORDER BY on name, and will get the first person in the alphabet returned.

    select top (1)@Name=name , @Perno=perno
    from tbl_Users
    order by name desc
     


    Roar Jørstad aka sveroa
    Senior Consultant, EVRY as
    Blog: Notebook, trick & tips
    Please mark as answer or helpful if my post is useful


    Thursday, October 18, 2012 12:20 PM