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?
Use the INSERT statement to insert data values into tables. The INSERT statement first adds a new row to an existing table, and then inserts the values that you specify into the row. You specify values by using a SET clause or VALUES clause. You can also insert the rows resulting from a query.
A stored procedure is a set of SQL code specifically written for performing a task. We can write a stored procedure and execute it with a single line of SQL code. One of the tasks that you can perform with the stored procedures is to insert rows in a table.
From the right-click menu, go to Tasks >> Generate Scripts... In the Generate and Publish Scripts pop-up window, press Next to choose objects screen. Now, the choose objects screen, choose Select specific database objects and choose the tables you want to script. You can even select all the tables and other objects.
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.
This stored proc works great for me:
http://vyaskn.tripod.com/code.htm#inserts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With