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

SPLIT FUNCTION

----------------------SPLIT names  using STRING FUNCTIONS--------------------------


/****** Object: UserDefinedFunction [dbo].[Split] Script Date: 08/06/2012 16:16:27 ******/

SET

ANSI_NULLS ON

GO

SET

QUOTED_IDENTIFIER ON

GO

-- select distinct mls_mail_to from gpc_mails where MLS_MESSAGE_FROM='NC' and mls_mail_status is null

create function [dbo].[Split] (@String varchar(8000), @Delimiter char(1))

Returns

@Results Table (Items varchar(8000))

As

Begin

Declare @Index int

Declare @Slice varchar(8000)

Select @Index = 1

If @String Is NULL Return

While @Index != 0

Begin

Select @Index = CharIndex(@Delimiter, @String)

If (@Index != 0)

Select @Slice = left(@String, @Index - 1)

else

Select @Slice = @String

Insert into @Results(Items) Values (@Slice)

Select @String = right(@String, Len(@String) - @Index)

If Len(@String) = 0 break

End

Return

End

 --select * from dbo.Split('1,vagalla,SURESH,reddy',',,')

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...