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. Stored Procedure PRoblem

Stored Procedure PRoblem

Scheduled Pinned Locked Moved Database
helpdatabasetutorialannouncement
8 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.
  • P Offline
    P Offline
    percyvimal
    wrote on last edited by
    #1

    Hello I planned to genralize a stored procedure where i can pass the table name field names as parameter to the storedprocedure along with the values and then use those information to add or update the table which is passed as parameter. My stored procedure look like this CREATE PROCEDURE Add_Edit_GSupportTables(@AddMode bit,@GenCode int,@GenName varchar(100),@GenTable varchar(50),@GenCodeField varchar(50),@GenNameField varchar(50)) as declare @AutoCode numeric; if @AddMode = 1 begin select @AutoCode = max(@GenCodeField) from @GenTable set @AutoCode = isnull(@AutoCode ,0) set @AutoCode = @AutoCode +1 Insert Into @GenTable values(@AutoCode,@GenName) end else begin update @GenTable set @GenNameField =@GenName where @GenCodeField = @GenCode end GO but it gives incorrect syntax near @GenTable How to do so that this stored procedure can be used as functoin to add / edit with regards vimal Help in need is the help indeed

    M A 3 Replies Last reply
    0
    • P percyvimal

      Hello I planned to genralize a stored procedure where i can pass the table name field names as parameter to the storedprocedure along with the values and then use those information to add or update the table which is passed as parameter. My stored procedure look like this CREATE PROCEDURE Add_Edit_GSupportTables(@AddMode bit,@GenCode int,@GenName varchar(100),@GenTable varchar(50),@GenCodeField varchar(50),@GenNameField varchar(50)) as declare @AutoCode numeric; if @AddMode = 1 begin select @AutoCode = max(@GenCodeField) from @GenTable set @AutoCode = isnull(@AutoCode ,0) set @AutoCode = @AutoCode +1 Insert Into @GenTable values(@AutoCode,@GenName) end else begin update @GenTable set @GenNameField =@GenName where @GenCodeField = @GenCode end GO but it gives incorrect syntax near @GenTable How to do so that this stored procedure can be used as functoin to add / edit with regards vimal Help in need is the help indeed

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      I'd recommend splitting this into two procedures, add and edit. The query processor will either have to build one query plan with redundant code, or, more likely, recompile the query plan every time you swap the value of @AddMode. As for your specific problem, you can only parameterize values. You can't parameterize schema. If you have to, you've probably designed your database incorrectly. Tables are not objects. Creating and dropping tables are very expensive operations. If you absolutely must do this, you should use string concatenation to form the schema part of the query, then use sp_executesql to execute it supplying the parameters, e.g.:

      DECLARE @query varchar(2000)
      SET @query = 'UPDATE ' + @GenTable +
      ' SET ' + @GenNameField + ' = @GenName WHERE ' +
      @GenCodeField + ' = @GenCode'

      EXEC sp_executesql @query,
      N'@GenName varchar(100), @GenCode int',
      @GenName, @GenCode

      Stability. What an interesting concept. -- Chris Maunder

      1 Reply Last reply
      0
      • P percyvimal

        Hello I planned to genralize a stored procedure where i can pass the table name field names as parameter to the storedprocedure along with the values and then use those information to add or update the table which is passed as parameter. My stored procedure look like this CREATE PROCEDURE Add_Edit_GSupportTables(@AddMode bit,@GenCode int,@GenName varchar(100),@GenTable varchar(50),@GenCodeField varchar(50),@GenNameField varchar(50)) as declare @AutoCode numeric; if @AddMode = 1 begin select @AutoCode = max(@GenCodeField) from @GenTable set @AutoCode = isnull(@AutoCode ,0) set @AutoCode = @AutoCode +1 Insert Into @GenTable values(@AutoCode,@GenName) end else begin update @GenTable set @GenNameField =@GenName where @GenCodeField = @GenCode end GO but it gives incorrect syntax near @GenTable How to do so that this stored procedure can be used as functoin to add / edit with regards vimal Help in need is the help indeed

        A Offline
        A Offline
        abbaskaya
        wrote on last edited by
        #3

        Example: Create procedure prg.ExamleProcedure @tabloname varchar(50), @data int as declare @stmt varchar(8000) select @stmt = 'insert into ' + @tabloname + ' (column1, column2) ' + 'select ' + @data + ' , NULL' Exec (@stmt) You can solve your problem like this. abbaskaya

        1 Reply Last reply
        0
        • P percyvimal

          Hello I planned to genralize a stored procedure where i can pass the table name field names as parameter to the storedprocedure along with the values and then use those information to add or update the table which is passed as parameter. My stored procedure look like this CREATE PROCEDURE Add_Edit_GSupportTables(@AddMode bit,@GenCode int,@GenName varchar(100),@GenTable varchar(50),@GenCodeField varchar(50),@GenNameField varchar(50)) as declare @AutoCode numeric; if @AddMode = 1 begin select @AutoCode = max(@GenCodeField) from @GenTable set @AutoCode = isnull(@AutoCode ,0) set @AutoCode = @AutoCode +1 Insert Into @GenTable values(@AutoCode,@GenName) end else begin update @GenTable set @GenNameField =@GenName where @GenCodeField = @GenCode end GO but it gives incorrect syntax near @GenTable How to do so that this stored procedure can be used as functoin to add / edit with regards vimal Help in need is the help indeed

          A Offline
          A Offline
          abbaskaya
          wrote on last edited by
          #4

          Example: Create procedure prg.ExamleProcedure @tabloname varchar(50), @data int as declare @stmt varchar(8000) select @stmt = 'insert into ' + @tabloname + ' (column1, column2) ' + 'select ' + @data + ' , NULL' Exec (@stmt) You can solve your problem:rose: like this. abbaskaya

          P 1 Reply Last reply
          0
          • A abbaskaya

            Example: Create procedure prg.ExamleProcedure @tabloname varchar(50), @data int as declare @stmt varchar(8000) select @stmt = 'insert into ' + @tabloname + ' (column1, column2) ' + 'select ' + @data + ' , NULL' Exec (@stmt) You can solve your problem:rose: like this. abbaskaya

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

            Thanks Mr Abbaskaya and Mr Mike I have 8 tables in my database which have the same structure and so i planned to do a general function so that i can make those 8 modules as a single functions and can use parameters to make it work. I have tried the way you have explained above but i am getting a syntax error.Could you tell me exactly how to implement that with correct syntax so that i can check it here. Thanks a lot for your help with regards vimal Help in need is the help indeed

            W 1 Reply Last reply
            0
            • P percyvimal

              Thanks Mr Abbaskaya and Mr Mike I have 8 tables in my database which have the same structure and so i planned to do a general function so that i can make those 8 modules as a single functions and can use parameters to make it work. I have tried the way you have explained above but i am getting a syntax error.Could you tell me exactly how to implement that with correct syntax so that i can check it here. Thanks a lot for your help with regards vimal Help in need is the help indeed

              W Offline
              W Offline
              WoutL
              wrote on last edited by
              #6

              If you have 8 tables with the same structure you should put them all in one table and drop the seven other ones. That would save your problem. Wout Louwers

              P 1 Reply Last reply
              0
              • W WoutL

                If you have 8 tables with the same structure you should put them all in one table and drop the seven other ones. That would save your problem. Wout Louwers

                P Offline
                P Offline
                percyvimal
                wrote on last edited by
                #7

                those 8 tables are same structured but due to simplicity and future oriented modifications and additions i am in a position to use 8 different tables. with regards vimal Help in need is the help indeed

                W 1 Reply Last reply
                0
                • P percyvimal

                  those 8 tables are same structured but due to simplicity and future oriented modifications and additions i am in a position to use 8 different tables. with regards vimal Help in need is the help indeed

                  W Offline
                  W Offline
                  WoutL
                  wrote on last edited by
                  #8

                  What do you mean with 'due to simplicity'. If the use of the 8 tables was that simple you wouldn't have to ask this question.;) due to simplicity and future oriented modifications and additions i am in a position to use 8 different tables. You sound like a manager (PHB). Not like a programmer.:-D Wout Louwers

                  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