--sp_CreateInsertScript 'ntrp.tblreceiptpurposemaster','PurposeStatusId=556',1
CREATE PROC procCreateInsertScript (
@tablename NVARCHAR(256) -- table name
,@con NVARCHAR(400) -- condition to filter data
,@ignoreIdentityCol bit=0 --indicate if ignore columne with identity
)
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
/* xtype=173 'binary'*/WHEN a.xtype =173 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR('+CONVERT(NVARCHAR(4),a.length*2+2)+'),'+a.name +')'+' END'
/*xtype=104 'bit'*/WHEN a.xtype =104 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR(1),'+a.name +')'+' END'
/*xtype=175 'char'*/WHEN a.xtype =175 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'''N''''''+'+'REPLACE('+a.name+','''''''','''''''''''')' + '+'''''''''+' END'
/*xtype=61 'datetime'*/WHEN a.xtype =61 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'''N''''''+'+'CONVERT(NVARCHAR(23),'+a.name +',121)'+ '+'''''''''+' END'
/*xtype=106 'decimal'*/WHEN a.xtype =106 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR('+CONVERT(NVARCHAR(4),a.xprec+2)+'),'+a.name +')'+' END'
/*xtype=62 'float' */WHEN a.xtype =62 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR(23),'+a.name +',2)'+' END'
/*xtype=56 'int'*/WHEN a.xtype =56 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR(11),'+a.name +')'+' END'
/*xtype=60 'money'*/WHEN a.xtype =60 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR(22),'+a.name +')'+' END'
/*xtype=239 'nchar'*/WHEN a.xtype =239 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'''N''''''+'+'REPLACE('+a.name+','''''''','''''''''''')' + '+'''''''''+' END'
/*xtype=108 'numeric'*/WHEN a.xtype =108 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR('+CONVERT(NVARCHAR(4),a.xprec+2)+'),'+a.name +')'+' END'
/*xtype=231 'nvarchar'*/WHEN a.xtype =231 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'''N''''''+'+'REPLACE('+a.name+','''''''','''''''''''')' + '+'''''''''+' END'
/*xtype=59 'real'*/WHEN a.xtype =59 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR(23),'+a.name +',2)'+' END'
/*xtype=58 'smalldatetime'*/WHEN a.xtype =58 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'''N''''''+'+'CONVERT(NVARCHAR(23),'+a.name +',121)'+ '+'''''''''+' END'
/*xtype=52 'smallint'*/WHEN a.xtype =52 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR(12),'+a.name +')'+' END'
/* xtype=122 'smallmoney'*/WHEN a.xtype =122 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR(22),'+a.name +')'+' END'
/*xtype=127 'bigint'*/WHEN a.xtype =127 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR(6),'+a.name +')'+' END'
/*xtype=48 'tinyint'*/WHEN a.xtype =48 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR(6),'+a.name +')'+' END'
/*xtype=165 'varbinary'*/WHEN a.xtype =165 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'CONVERT(NVARCHAR('+CONVERT(NVARCHAR(4),a.length*2+2)+'),'+a.name +')'+' END'
/*xtype=167 'varchar'*/WHEN a.xtype =167 THEN 'CASE WHEN '+a.name+' is null THEN ''NULL'' ELSE '+'''N''''''+'+'REPLACE('+a.name+','''''''','''''''''''')' + '+'''''''''+' END'
ELSE '''NULL'''
END AS col
,a.colid
,a.name
FROM syscolumns a
WHERE a.id = object_id(@tablename)
and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36
and (columnproperty(a.id, a.name, 'IsIdentity') = 0 OR @ignoreIdentityCol=0)
) AS t
ORDER BY colid;
SELECT @sqlstr=@sqlstr+left(@colsStr,len(@colsStr)-1)+') '+left(@valueStr1,len(@valueStr1)-3)+')'' AS sql FROM '+@tablename + ' WHERE 1=1 AND ' + isnull(@con,'1=1');
PRINT @sqlstr;
EXEC( @sqlstr);
SET NOCOUNT OFF
END
No comments:
Post a Comment