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. Doubt in StoredProcedure [modified]

Doubt in StoredProcedure [modified]

Scheduled Pinned Locked Moved Database
sharepointdatabasehelp
8 Posts 5 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
    John L Ponratnam
    wrote on last edited by
    #1

    create proc sp_temp @columnname varchar(25),@value varchar(25) as declare @sql nvarchar(400) set @sql='select *from tblname where '+@columnname+' = '+@value exec(@sql) This is the code. This procedures works for integer value when I insert in @value but for a string value; it gives invalid column name. Can anyone help me out -- modified at 2:50 Monday 29th October, 2007

    Regards, LEE

    N N S J 4 Replies Last reply
    0
    • J John L Ponratnam

      create proc sp_temp @columnname varchar(25),@value varchar(25) as declare @sql nvarchar(400) set @sql='select *from tblname where '+@columnname+' = '+@value exec(@sql) This is the code. This procedures works for integer value when I insert in @value but for a string value; it gives invalid column name. Can anyone help me out -- modified at 2:50 Monday 29th October, 2007

      Regards, LEE

      N Offline
      N Offline
      neeraj_indianic
      wrote on last edited by
      #2

      Just check your table detail. It will be because of you have defined the column name as int while creating the table.

      N 1 Reply Last reply
      0
      • J John L Ponratnam

        create proc sp_temp @columnname varchar(25),@value varchar(25) as declare @sql nvarchar(400) set @sql='select *from tblname where '+@columnname+' = '+@value exec(@sql) This is the code. This procedures works for integer value when I insert in @value but for a string value; it gives invalid column name. Can anyone help me out -- modified at 2:50 Monday 29th October, 2007

        Regards, LEE

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        Print your SQL query before executing, you will get the problem.


        My Website | Ask smart questions

        1 Reply Last reply
        0
        • J John L Ponratnam

          create proc sp_temp @columnname varchar(25),@value varchar(25) as declare @sql nvarchar(400) set @sql='select *from tblname where '+@columnname+' = '+@value exec(@sql) This is the code. This procedures works for integer value when I insert in @value but for a string value; it gives invalid column name. Can anyone help me out -- modified at 2:50 Monday 29th October, 2007

          Regards, LEE

          S Offline
          S Offline
          soni uma
          wrote on last edited by
          #4

          Try This it's help u set @sql='select * from jc_student_master where '+@columnname+' = '''+@value+''' '

          1 Reply Last reply
          0
          • J John L Ponratnam

            create proc sp_temp @columnname varchar(25),@value varchar(25) as declare @sql nvarchar(400) set @sql='select *from tblname where '+@columnname+' = '+@value exec(@sql) This is the code. This procedures works for integer value when I insert in @value but for a string value; it gives invalid column name. Can anyone help me out -- modified at 2:50 Monday 29th October, 2007

            Regards, LEE

            J Offline
            J Offline
            John ph
            wrote on last edited by
            #5

            If you are using EXEC command to execute a sql String then the parameter has to converted into Character and made as a part of the query before execution. try this...

            set @sql = 'select * from tblname where ' + 
            @columnname + ' = ' + CAST(@value AS NVARCHAR(50)) 
            EXEC(@sql)
            

            Regards
             - J O H N -


            J 1 Reply Last reply
            0
            • J John ph

              If you are using EXEC command to execute a sql String then the parameter has to converted into Character and made as a part of the query before execution. try this...

              set @sql = 'select * from tblname where ' + 
              @columnname + ' = ' + CAST(@value AS NVARCHAR(50)) 
              EXEC(@sql)
              

              Regards
               - J O H N -


              J Offline
              J Offline
              John L Ponratnam
              wrote on last edited by
              #6

              Let me have table name tblname which has empid and empname. In the paramater @columnname empid and @value(int) this query gets the perfect output. But If I use @columnname empname and @value as string; then it gives invalid column name

              Regards, LEE

              J 1 Reply Last reply
              0
              • J John L Ponratnam

                Let me have table name tblname which has empid and empname. In the paramater @columnname empid and @value(int) this query gets the perfect output. But If I use @columnname empname and @value as string; then it gives invalid column name

                Regards, LEE

                J Offline
                J Offline
                John ph
                wrote on last edited by
                #7

                ok I suggest you a better way of doing this. In sql Server we have one method of executing Dynamic Sql using sp_executesql which does a parameter substution based on the parameter definition.Let us do something like this to solve your problem...

                create proc sp_temp 
                @columnname varchar(25),
                @value varchar(25) 
                as 
                 
                DECLARE @SQLQuery AS NVARCHAR(500)
                DECLARE @ParameterDefinition AS NVARCHAR(100)
                 
                set @SQLQuery = 'select * from tblname where (1=1) AND ' 
                 
                IF @columnname = 'EmpID'
                BEGIN
                set @SQLQuery = @SQLQuery + @columnname + ' =  @value' 
                SET @ParameterDefinition =  '@value INT'
                END
                 
                IF @columnname = 'EmpName' 
                BEGIN
                set @SQLQuery = @SQLQuery + @columnname + ' = @value' 
                SET @ParameterDefinition =  '@value NVARCHAR(25)'
                END
                 
                EXECUTE sp_executesql @SQLQuery, @ParameterDefinition, @value
                 
                GO
                

                Here in the above procedure I build the ParameterDefinition list with the appropriate Datatype based on the column name. And the sp_executesql would automatically do a parameter substitution based on the DataType. Jus check it out whether it works...

                Regards
                 - J O H N -


                1 Reply Last reply
                0
                • N neeraj_indianic

                  Just check your table detail. It will be because of you have defined the column name as int while creating the table.

                  N Offline
                  N Offline
                  neeraj_indianic
                  wrote on last edited by
                  #8

                  ok, Now i got your problem and your solution is given here. set @sql='select *from tblname where '+@columnname+' = '+''''+@value+'''' I have tried it and its working.

                  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