Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your specific case I think you can do this:</p> <pre><code>CREATE PROCEDURE dbo.GenerateSingleInsert @table NVARCHAR(511), -- expects schema.table notation @pk_column SYSNAME, -- column that is primary key @pk_value INT -- change data type accordingly AS BEGIN SET NOCOUNT ON; DECLARE @cols NVARCHAR(MAX), @vals NVARCHAR(MAX), @valOut NVARCHAR(MAX), @valSQL NVARCHAR(MAX); SELECT @cols = N'', @vals = N''; SELECT @cols = @cols + ',' + QUOTENAME(name), @vals = @vals + ' + ' + REPLICATE(CHAR(39),3) + ',' + REPLICATE(CHAR(39),3) + ' + ' + REPLICATE(CHAR(39),2) + '+' + 'RTRIM(' + CASE WHEN system_type_id IN (40,41,42,43,58,61) THEN 'CONVERT(CHAR(8), ' + QUOTENAME(name) + ', 112) + '' '' + CONVERT(CHAR(14), ' + QUOTENAME(name) + ', 14)' ELSE 'REPLACE(' + QUOTENAME(name) + ','''''''','''''''''''')' END + ') + ' + REPLICATE(CHAR(39),2) FROM sys.columns WHERE [object_id] = OBJECT_ID(@table) AND system_type_id &lt;&gt; 189 -- can't insert rowversion AND is_computed = 0; -- can't insert computed columns SELECT @cols = STUFF(@cols, 1, 1, ''), @vals = REPLICATE(CHAR(39), 4) + ' + ' + STUFF(@vals, 1, 13, '') + REPLICATE(CHAR(39), 2); SELECT @valSQL = N'SELECT @valOut = ' + @vals + ' FROM ' + @table + ' WHERE ' + QUOTENAME(@pk_column) + ' = ''' + RTRIM(@pk_value) + ''';'; EXEC sp_executesql @valSQL, N'@valOut NVARCHAR(MAX) OUTPUT', @valOut OUTPUT; SELECT SQL = 'INSERT ' + @table + '(' + @cols + ') SELECT ' + @valOut; END GO </code></pre> <p>So let's try it out:</p> <pre><code>CREATE TABLE dbo.splunge ( ID INT, dt DATETIME, rv ROWVERSION, t NVARCHAR(MAX) ); INSERT dbo.splunge(ID, dt, t) SELECT 1, GETDATE(), 'foo' UNION ALL SELECT 2, GETDATE(), 'bar' UNION ALL SELECT 3, GETDATE(), 'O''Brien'; EXEC dbo.GenerateSingleInsert N'dbo.splunge', N'ID', 1; SQL ------------- INSERT dbo.splunge([ID],[dt],[t]) SELECT '1','20120517 10:07:07:330','foo' EXEC dbo.GenerateSingleInsert N'dbo.splunge', N'ID', 2; SQL ------------- INSERT dbo.splunge([ID],[dt],[t]) SELECT '2','20120517 10:07:07:330','bar' EXEC dbo.GenerateSingleInsert N'dbo.splunge', N'ID', 3; SQL ------------- INSERT dbo.splunge([ID],[dt],[t]) SELECT '3','20120517 10:07:07:330','O''Brien' </code></pre> <p>If there is an IDENTITY column you may need to set SET IDENTITY_INSERT ON for the TEST table, and verify that there is no collision. Probably about 500 caveats I should mention, I haven't tested all data types, etc.</p> <p>However in the more general case there is a <em>lot</em> more to it than this. Vyas K has a pretty robust stored procedure that should demonstrate how complicated it can get:</p> <p><a href="http://vyaskn.tripod.com/code/generate_inserts_2005.txt" rel="noreferrer">http://vyaskn.tripod.com/code/generate_inserts_2005.txt</a></p> <p>You are probably far better off using a tool like <a href="http://www.red-gate.com/products/sql-development/sql-data-compare/" rel="noreferrer">Red-Gate's SQL Data Compare</a> to pick a specific row and generate an insert for you. <a href="http://bertrandaaron.wordpress.com/2012/04/20/re-blog-the-cost-of-reinventing-the-wheel/" rel="noreferrer">As I've blogged about</a>, paying for a tool is not just about the money, it's about the hours of troubleshooting and bug-fixing that someone else has already done for you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload