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. concat command in stored procedure

concat command in stored procedure

Scheduled Pinned Locked Moved Database
databasehelptutorial
5 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.
  • J Offline
    J Offline
    jithbiz0033
    wrote on last edited by
    #1

    Hi Guys I have a stored procedure which has the coding to insert values in tables with different suffix but one prefix. Eg table_fruits, table_vegetables, table_pulses etc., I am passing the suffix in the procedure. I dont know how to concat the suffix to insert the value in the specific table. create procedure [dbo].[my_proc]( @category varchar(30), @Name varchar(50)) As Begin insert into table_+@category+ value name=@Name; End I dont want to follow if(@category = 'fruits') insert into table_fruits values name=@Name; else if(@category = 'vegetables') etc ., etc., Is there any way to do this. Kindly help me. Thanks Jith

    P 1 Reply Last reply
    0
    • J jithbiz0033

      Hi Guys I have a stored procedure which has the coding to insert values in tables with different suffix but one prefix. Eg table_fruits, table_vegetables, table_pulses etc., I am passing the suffix in the procedure. I dont know how to concat the suffix to insert the value in the specific table. create procedure [dbo].[my_proc]( @category varchar(30), @Name varchar(50)) As Begin insert into table_+@category+ value name=@Name; End I dont want to follow if(@category = 'fruits') insert into table_fruits values name=@Name; else if(@category = 'vegetables') etc ., etc., Is there any way to do this. Kindly help me. Thanks Jith

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      You will have to build the INSERT statement in a varchar variable and execute it dynamically using the EXECUTE command.

      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

      J 2 Replies Last reply
      0
      • P pmarfleet

        You will have to build the INSERT statement in a varchar variable and execute it dynamically using the EXECUTE command.

        Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

        J Offline
        J Offline
        jithbiz0033
        wrote on last edited by
        #3

        Hi Paul, Thanks for you suggestion. It works well. Thanks Jith

        1 Reply Last reply
        0
        • P pmarfleet

          You will have to build the INSERT statement in a varchar variable and execute it dynamically using the EXECUTE command.

          Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

          J Offline
          J Offline
          jithbiz0033
          wrote on last edited by
          #4

          Hi Mr. Paul I have one more doubt. If I want to get a value using SELECT Command in EXECUTE while using Stored Procedure. How to get it I tried Statement 1 : @GetValue = Exec('Select Id from table_'+@Category+' where name = @Name') Statement 2 : Exec('Select @GetValue = Id from table_'+@Category+' where name = @Name') But both are not working, Thanks Jith

          P 1 Reply Last reply
          0
          • J jithbiz0033

            Hi Mr. Paul I have one more doubt. If I want to get a value using SELECT Command in EXECUTE while using Stored Procedure. How to get it I tried Statement 1 : @GetValue = Exec('Select Id from table_'+@Category+' where name = @Name') Statement 2 : Exec('Select @GetValue = Id from table_'+@Category+' where name = @Name') But both are not working, Thanks Jith

            P Offline
            P Offline
            pmarfleet
            wrote on last edited by
            #5

            Have a look at this article[^].

            Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

            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