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. To update multiple dynamic field in a single query

To update multiple dynamic field in a single query

Scheduled Pinned Locked Moved Database
databasecomhelpannouncement
3 Posts 2 Posters 2 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.
  • M Offline
    M Offline
    Member 13657485
    wrote on last edited by
    #1

    hi, I need a single qurery which satisfy the muliple dynamic fields get to be updated. 1. I am having the multiple fields in the table and I want to consider only the fieldname contanis "_edt" 2. All the fieldname("_edt") get to be updated as "some value" like "XXX" Looks like : set @sql = 'UPDATE r SET ' + c.name = ''XXX'' FROM sys.columns c INNER JOIN sys.tables t ON c.object_id = t.object_id,Record r where t.name = ''Record'' and c.name like ''%_edt''' Please help on this. Thanks, Arun

    Richard DeemingR 1 Reply Last reply
    0
    • M Member 13657485

      hi, I need a single qurery which satisfy the muliple dynamic fields get to be updated. 1. I am having the multiple fields in the table and I want to consider only the fieldname contanis "_edt" 2. All the fieldname("_edt") get to be updated as "some value" like "XXX" Looks like : set @sql = 'UPDATE r SET ' + c.name = ''XXX'' FROM sys.columns c INNER JOIN sys.tables t ON c.object_id = t.object_id,Record r where t.name = ''Record'' and c.name like ''%_edt''' Please help on this. Thanks, Arun

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Something like this should work:

      DECLARE @SQL nvarchar(max) = N'';

      SELECT
      @SQL = @SQL + N', ' + QUOTENAME(name) + N' = @value'
      FROM
      sys.columns
      WHERE
      object_id = OBJECT_ID('Record')
      And
      name Like '%_edit'
      ;

      SET @SQL = Substring(@SQL, 3, LEN(@SQL) - 2);
      SET @SQL = N'UPDATE Record SET ' + @SQL;

      PRINT @SQL;

      EXEC sp_executesql @statement = @SQL,
      @params = N'@value varchar(10)', -- TODO: Use the correct data type and size here
      @value = 'XXX';


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Something like this should work:

        DECLARE @SQL nvarchar(max) = N'';

        SELECT
        @SQL = @SQL + N', ' + QUOTENAME(name) + N' = @value'
        FROM
        sys.columns
        WHERE
        object_id = OBJECT_ID('Record')
        And
        name Like '%_edit'
        ;

        SET @SQL = Substring(@SQL, 3, LEN(@SQL) - 2);
        SET @SQL = N'UPDATE Record SET ' + @SQL;

        PRINT @SQL;

        EXEC sp_executesql @statement = @SQL,
        @params = N'@value varchar(10)', -- TODO: Use the correct data type and size here
        @value = 'XXX';


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        M Offline
        M Offline
        Member 13657485
        wrote on last edited by
        #3

        Tnks a lot.. it is working for me

        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