Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1105831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:41:48+00:00 2026-05-17T01:41:48+00:00

Does anybody know of a proc or script which will generate any row into

  • 0

Does anybody know of a proc or script which will generate any row into an insert statement into the same table?

Basically, I’d like to call something like

exec RowToInsertStatement 'dbo.user', 45;

And the following code would be output

insert into dbo.MyTable( FirstName, LastName, Position)
values( 'John', 'MacIntyre', 'Software Consultant');

I realize I could

insert into dbo.MyTable
select * from dbo.MyTable where id=45;

But this obviously won’t work, because the ID column will complain (I hope it complains) and there’s no way to just override that one column without listing all columns, and in some tables there could be hundreds.

So, does anybody know of a proc that will write this simple insert for me?

EDIT 3:04: The purpose of this is so I can make a copy of the row, so after the INSERT is generated, I can modify it into something like

insert into dbo.MyTable( FirstName, LastName, Position)
values( 'Dave', 'Smith', 'Software Consultant');

.. no obviously this contrived example is so simple it doesn’t make sense, but if you have a table with 60 columns, and all you need is to change 3 or 4 values, then it starts to be a hassle.

Does that make sense?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:41:49+00:00Added an answer on May 17, 2026 at 1:41 am

    Update

    I believe the following dynamic query is what you want:

    declare @tableName varchar(100), @id int, @columns varchar(max), @pk varchar(20)
    set @tableName = 'MyTable'
    set @pk = 'id'
    set @id = 45
    
    set @columns = stuff((select ',['+c.name+']' [text()] from sys.tables t
    join sys.columns c on t.object_id = c.object_id
    where t.name = @tableName and c.name <> @pk for xml path('')),1,1,'')
    
    print 'insert into [' + @tableName + '] (' + @columns + ')
    select ' + @columns + '
    from [' + @tableName + '] 
    where ' + @pk + ' = ' + cast(@id as varchar)
    

    Update 2

    The actual thing that you wanted:

    declare @tableName varchar(100), @id int, @columns nvarchar(max), @pk nvarchar(20), @columnValues nvarchar(max)
    set @tableName = 'MyTable'
    set @pk = 'id'
    set @id = 45
    
    set @columns = stuff((select ',['+c.name+']' [text()] from sys.tables t
    join sys.columns c on t.object_id = c.object_id
    where t.name = @tableName and c.name <> @pk for xml path('')),1,1,'')
    
    set @columnValues = 'set @actualColumnValues = (select' +
    stuff((select ','','''''' + cast(['+c.name+'] as varchar(max)) + '''''''' [text()]' [text()] 
    from sys.tables t
    join sys.columns c on t.object_id = c.object_id
    where t.name = @tableName and c.name <> @pk for xml path('')),1,1,'')
    + 'from [' + @tableName + ']
    where ' + @pk + ' = ' + cast(@id as varchar) 
    + 'for xml path(''''))'
    
    --select @columnValues
    declare @actualColumnValues nvarchar(max), @columnValuesParams nvarchar(500)
    SET @columnValuesParams = N'@actualColumnValues nvarchar(max) OUTPUT';
    EXECUTE sp_executesql @columnValues, @columnValuesParams, @actualColumnValues OUTPUT;
    --SELECT stuff(@actualColumnValues, 1,1, '')
    
    declare @statement nvarchar(max)
    set @statement =
    'insert into [' + @tableName + '] (' + @columns + ')
    select ' + stuff(@actualColumnValues,1,1,'')
    
    print @statement
    

    What it does is this:
    It generates the insert statement and then it queries the actual data from the table and generates the select statement with that data. May not work correctly for some really complex datatypes but for varchars, datetimes and ints should work like a charm.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.