sql - stored procedure to remove quotes -


i trying write stored procedure removed leading , trailing quotes arbitrary table , column. keep getting error saying table name isn't declared.

here sp

create table [dbo].[test] (id nvarchar(20)) insert dbo.test values ('"07617966004766"') go  create procedure sp_stripdoublequotes      @tablename sysname,     @columnname sysname begin     set nocount on;      update @tablename     set @columnname = substring(@columnname, 2, len(@columnname))     left(@columnname, 1) = '"'      update @tablename     set @columnname = substring(@columnname, 1, len(@columnname)-1)     right(@columnname, 1) = '"'  end go exec [dbo].[sp_stripdoublequotes] n'[dbo].[test]', n'[id]'    select * test 

here link fiddle: link fiddle

create procedure sp_stripdoublequotes      @tablename sysname,     @columnname sysname,     @sql varchar(max) begin     set nocount on;         set @sql =      'update ' + '[' + @tablename +']' +     'set' + '[' + @columnname +']' +'= substring(' +'[' + @columnname +']' +', 2, len(' +'[' + @columnname +']' +'))     left('  + '[' + @columnname +']' +', 1) = '+'''"'''     --print(@sql)     exec (@sql)      set @sql =     'update ' + '[' + @tablename +']' +     'set' + '[' + @columnname +']' +'= substring(' + '[' + @columnname +']' +', 1, len(' + '[' + @columnname + ']' +')-1)     right(' + '[' + @columnname +']' +', 1) = '+'''"'''     --print(@sql)     exec (@sql) end go exec [dbo].[sp_stripdoublequotes] n'test', n'id'  -- exec [dbo].[sp_stripdoublequotes] n'[dbo].[test]', n'[id]'  

updated 2nd: added [] wrap table , column incase table , column name have whitespace in them. @sean lange , @richard

updated 3rd: @[benjamin moskovits] (xd) mentioned, if hard coded brackets, correct execute command exec [dbo].[sp_stripdoublequotes] n'test', n'id'. try add or remove brackets , print see whether syntax correct before executing it.


Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

javascript - Confirm a form & display message if form is valid with JQuery -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -