Introduction
Sometimes, you want to export some SQL scripts of some records from some existing tables in your SQL Server database so that you can insert those data into a similar table of another database. There are lots of ways to do that. Here, I will share you a stored procedure to do that. Hope it helps.
Using the Code
First, please create the below stored procedure [dbo].[sp_CreateInsertScript]
in your SQL Server database.
[dbo].[sp_CreateInsertScript]
content:
CREATE PROC [dbo].[sp_CreateInsertScript] (
@tablename NVARCHAR(256)
,@con NVARCHAR(400)
,@ignoreIdentityCol bit=0
,@isDebug bit=0
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @sqlstr NVARCHAR(MAX);
DECLARE @valueStr1 NVARCHAR(MAX);
DECLARE @colsStr NVARCHAR(MAX);
SELECT @sqlstr='SELECT ''INSERT '+@tablename;
SELECT @valueStr1='';
SELECT @colsStr='(';
SELECT @valueStr1='VALUES (''+';
IF RTRIM(LTRIM(@con))=''
SET @con='1=1';
SELECT @valueStr1=@valueStr1+col+'+'',''+'
,@colsStr=@colsStr+'['+name +'],'
FROM (
SELECT
CASE
WHEN sc.xtype in (173,165) THEN 'CASE WHEN ['+sc.name+'] is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR('+CONVERT(NVARCHAR(4),sc.[length]*2+2)+'),['+sc.name +'])'+' END'
WHEN sc.xtype =104 THEN 'CASE WHEN ['+sc.name+'] is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR(1),['+sc.name +'])'+' END'
WHEN sc.xtype in(58,61) THEN 'CASE WHEN ['+sc.name+'] is null THEN ''NULL'' ELSE '+'''N''''''+'+'CONVERT(NVARCHAR(23),'+sc.name +',121)'+ '+'''''''''+' END'
WHEN sc.xtype in (36,175,167,231,239) THEN 'CASE WHEN ['+sc.name+'] is null THEN ''NULL'' ELSE '+'''N''''''+'+'REPLACE(['+sc.name+'],'''''''','''''''''''')' + '+'''''''''+' END'
WHEN sc.xtype in(106,108) THEN 'CASE WHEN ['+sc.name+'] is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR('+CONVERT(NVARCHAR(4),sc.xprec+2)+'),['+sc.name +'])'+' END'
WHEN sc.xtype in (59,62) THEN 'CASE WHEN ['+sc.name+'] is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR(23),'+sc.name +',2)'+' END'
WHEN sc.xtype in (48,52,56,127,122,60) THEN 'CASE WHEN ['+sc.name+'] is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR(23),['+sc.name +'])'+' END'
ELSE '''NULL'''
END AS col
,sc.colid
,sc.name
FROM syscolumns AS sc
WHERE sc.id = object_id(@tablename)
AND sc.xtype <>189
AND sc.xtype <>34
AND sc.xtype <>35
AND (columnproperty(sc.id, sc.name, 'IsIdentity') = 0 OR @ignoreIdentityCol=0)
) AS t
ORDER BY colid;
SET @colsStr=left(@colsStr,len(@colsStr)-1)+') ';
SET @valueStr1=left(@valueStr1,len(@valueStr1)-3)+')''';
SELECT @sqlstr=@sqlstr+@colsStr+@valueStr1+' AS sql FROM '+@tablename + ' WHERE 1=1 AND ' + isnull(@con,'1=1');
IF @isDebug=1
BEGIN
PRINT '1.columns string: '+ @colsStr;
PRINT '2.values string: '+ @valueStr1
PRINT '3.'+@sqlstr;
END
EXEC( @sqlstr);
SET NOCOUNT OFF
END
GO
Example
Next, I do an example to help you understand how to use it. We assume you have a table Country
as below in your database and you want get insert
SQL statements from some records in this table. These records should be items that their continent_name
is North America
.
Example table
script:
CREATE TABLE [dbo].[Country](
[geoname_id] [varchar](50) NULL,
[locale_code] [varchar](50) NULL,
[continent_code] [varchar](50) NULL,
[continent_name] [varchar](50) NULL,
[country_iso_code] [varchar](50) NULL,
[country_name] [varchar](50) NULL
) ON [PRIMARY]
According to your requirements, you can call the stored procedure you just created to generate SQL scripts like this:
exec sp_CreateInsertScript '[dbo].[Country]','[continent_name]=''North America'''
After that, you will get some insert
SQL statement records:
By now, you can copy the records or save the output to a query or txt file by commands in right-click menu so that you use it in another database.
Thanks!