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 assign a query result to a stored procedure

How to assign a query result to a stored procedure

Scheduled Pinned Locked Moved Database
databasehelptutorial
4 Posts 4 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.
  • N Offline
    N Offline
    Nekkantidivya
    wrote on last edited by
    #1

    Hi, I my stored procedure I have an insert query. For this I have to get one value from a sub query. But it is not accepting that query. So I tried to declare a separate variable and assign the subquery value to this and insert it in the table. Create Procedure [dbo].[AddOrRemoveFavourites] ( @UserId bigint, @VideoName varchar(50), ) as Declare @VideoId int Set @VideoId = Select VideoId from Videos where Title='Monkeydog' Insert into Favourites(UserId,VideoId) values(@UserId,@VideoId) but in this I am getting error like "Incorrect syntax near the keyword 'Select'." If anyone have any idea to solve this please reply me. Thanks in advance.

    L S N 3 Replies Last reply
    0
    • N Nekkantidivya

      Hi, I my stored procedure I have an insert query. For this I have to get one value from a sub query. But it is not accepting that query. So I tried to declare a separate variable and assign the subquery value to this and insert it in the table. Create Procedure [dbo].[AddOrRemoveFavourites] ( @UserId bigint, @VideoName varchar(50), ) as Declare @VideoId int Set @VideoId = Select VideoId from Videos where Title='Monkeydog' Insert into Favourites(UserId,VideoId) values(@UserId,@VideoId) but in this I am getting error like "Incorrect syntax near the keyword 'Select'." If anyone have any idea to solve this please reply me. Thanks in advance.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Try this one;

      SELECT @VideoId = VideoId
      FROM Videos
      WHERE Title = 'Monkeydog'

      :)

      I are Troll :suss:

      1 Reply Last reply
      0
      • N Nekkantidivya

        Hi, I my stored procedure I have an insert query. For this I have to get one value from a sub query. But it is not accepting that query. So I tried to declare a separate variable and assign the subquery value to this and insert it in the table. Create Procedure [dbo].[AddOrRemoveFavourites] ( @UserId bigint, @VideoName varchar(50), ) as Declare @VideoId int Set @VideoId = Select VideoId from Videos where Title='Monkeydog' Insert into Favourites(UserId,VideoId) values(@UserId,@VideoId) but in this I am getting error like "Incorrect syntax near the keyword 'Select'." If anyone have any idea to solve this please reply me. Thanks in advance.

        S Offline
        S Offline
        Syed Mehroz Alam
        wrote on last edited by
        #3

        Hi, You need to enclose your select in parenthesis and include a top(1) clause, like this:

        Set @VideoId = ( Select top(1) VideoId from Videos where Title='Monkeydog' )

        Regards,

        Syed Mehroz Alam My Blog | My Articles
        Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

        1 Reply Last reply
        0
        • N Nekkantidivya

          Hi, I my stored procedure I have an insert query. For this I have to get one value from a sub query. But it is not accepting that query. So I tried to declare a separate variable and assign the subquery value to this and insert it in the table. Create Procedure [dbo].[AddOrRemoveFavourites] ( @UserId bigint, @VideoName varchar(50), ) as Declare @VideoId int Set @VideoId = Select VideoId from Videos where Title='Monkeydog' Insert into Favourites(UserId,VideoId) values(@UserId,@VideoId) but in this I am getting error like "Incorrect syntax near the keyword 'Select'." If anyone have any idea to solve this please reply me. Thanks in advance.

          N Offline
          N Offline
          Niladri_Biswas
          wrote on last edited by
          #4

          Hi, Set @VideoId = Select VideoId from Videos where Title='Monkeydog' is wrong use

          Select @VideoId = VideoId from Videos where Title='Monkeydog'

          The reason is if we want to assign values to more than one variable at a time then go with Select. SET allows you to assign data to only one variable at a time. For more information visit here http://vyaskn.tripod.com/differences_between_set_and_select.htm[^] Also searching in google will reveal more info :)

          Niladri Biswas

          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