Saturday, 17 August 2013

SCRIPT by PROCEDURE

---------------------------PRODUCE SCRIPT through PROCEDURE------------------------

alter PROC usp_get_inserts(@table_name varchar(50)) as
BEGIN
--usp_get_inserts '[TABLE NAME]'
declare @cols varchar(1000) =''
declare @ins varchar(1000)=''
declare @cols_type varchar(max)=''
select @cols=@cols+','+c.name ,@cols_type= @cols_type
+ '+'',''+' +case when t.name in('int','money') then 'cast('+ c.name + ' as varchar(10))' else 'char(39) +'+c.name + '+ CHAR(39)' end
from sys.columns c
INNER JOIN sys.types t ON c.system_type_id = t.system_type_id
where object_id=object_id(@table_name)
set @cols = right(@cols,len(@cols)-1)
set @cols_type = right(@cols_type,len(@cols_type)-5)
set @ins= 'INSERT INTO '+@table_name+'(' + @cols + ') VALUES('
exec('select '''+ @ins + '''+'+ @cols_type + '+'')'' from ' + @table_name)
END

No comments:

Post a Comment

EMP, DEPT Sample script

/****** Object:  Table [dbo].[DEPT]    Script Date: 19-05-2016 06:58:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET A...