Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to store result of a query in a variable

How to store result of a query in a variable

Scheduled Pinned Locked Moved Database
databasetutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    veereshIndia
    wrote on last edited by
    #1

    Hi All, Please tel me how to store result of a query in a variable. And I want to use that variable in another query's in where condition Here below is my code. alter procedure LogProc ( @UserId varchar(500), @Password varchar(500) ) as Begin declare @str1 varchar(500) declare @str2 varchar(500) declare @str3 varchar(500) set @str1 =( ' select AstId from UserTable where UserId = ''' + @UserId + ''' and Password = ''' + @Password + '''' ) exec(@str1); set @str2=(' select RoleID from AstProf where AstId = ''''' + @str1 + '''''') exec(@str2); End exec LogProc 'veeresh','veeresh'

    i want to join this group

    I 1 Reply Last reply
    0
    • V veereshIndia

      Hi All, Please tel me how to store result of a query in a variable. And I want to use that variable in another query's in where condition Here below is my code. alter procedure LogProc ( @UserId varchar(500), @Password varchar(500) ) as Begin declare @str1 varchar(500) declare @str2 varchar(500) declare @str3 varchar(500) set @str1 =( ' select AstId from UserTable where UserId = ''' + @UserId + ''' and Password = ''' + @Password + '''' ) exec(@str1); set @str2=(' select RoleID from AstProf where AstId = ''''' + @str1 + '''''') exec(@str2); End exec LogProc 'veeresh','veeresh'

      i want to join this group

      I Offline
      I Offline
      IMQ
      wrote on last edited by
      #2

      Hi, you can store result of the query into a variable like this; select @str1=AstId from UserTable where UserId=@UserId and Password=@Password Here @str1 will store the AstID in it. the complete procedure will liook like this: alter procedure LogProc ( @UserId varchar(500), @Password varchar(500), @str1 int output, @str2 int output ) as Begin declare @str3 varchar(500) /* why u hav declare this variable ???? */ select @str1=AstId from UserTable where UserId @UserId and Password = @Password select @str2=RoleID from AstProf where AstId =@str1 select @str1,@str2 End exec LogProc 'veeresh','veeresh' you may use @str1 and @str2 in your code .

      ________________ Tehmina Qureshi ****************

      V 1 Reply Last reply
      0
      • I IMQ

        Hi, you can store result of the query into a variable like this; select @str1=AstId from UserTable where UserId=@UserId and Password=@Password Here @str1 will store the AstID in it. the complete procedure will liook like this: alter procedure LogProc ( @UserId varchar(500), @Password varchar(500), @str1 int output, @str2 int output ) as Begin declare @str3 varchar(500) /* why u hav declare this variable ???? */ select @str1=AstId from UserTable where UserId @UserId and Password = @Password select @str2=RoleID from AstProf where AstId =@str1 select @str1,@str2 End exec LogProc 'veeresh','veeresh' you may use @str1 and @str2 in your code .

        ________________ Tehmina Qureshi ****************

        V Offline
        V Offline
        veereshIndia
        wrote on last edited by
        #3

        Hi Sir, Thanks for helping me.I got it. Veeresh

        i want to join this group

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups