Tuesday, 25 August 2015

Script to create Store Procedure by Table name sql server

CREATE PROC [dbo].[GENERATEQUERY]
@TBL VARCHAR(20)=''
AS
BEGIN
DECLARE @CR  VARCHAR(200)='';
select @CR +='CREATE PROCEDURE '+TABLE_SCHEMA+'.Pr'+upper(left(@TBL,1))+right(@TBL,len(@TBL)-1)+'IU'
from INFORMATION_SCHEMA.TABLES
where TABLE_NAME=@TBL

declare @schema varchar(10);
select @schema =TABLE_SCHEMA
from INFORMATION_SCHEMA.TABLES
where TABLE_NAME=@TBL

declare @db varchar(10);
select @db =TABLE_CATALOG
from INFORMATION_SCHEMA.TABLES
where TABLE_NAME=@TBL


declare @par nvarchar(1000)='';
select @par+='@'+COLUMN_NAME+' '+DATA_TYPE +case
    when CHARACTER_MAXIMUM_LENGTH is null
    then ','
    when CHARACTER_MAXIMUM_LENGTH is not null
    then '('+CAST( REPLACE(CHARACTER_MAXIMUM_LENGTH,'-1','max') as varchar)+'),'
    end +CHAR(10)
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME=@TBL
    AND COLUMN_NAME NOT IN  ('CreatedOn','UpdatedOn','UpdatedBy','UserId');

SET @par +='@CmdType char(1)'+char(10);
SET @par =REPLACE(@par,'@CreatedBy','@UserId')

declare @beb varchar(200) ='AS' +CHAR(10) +'BEGIN'+CHAR(10)+'SET ANSI_NULLS ON;'+CHAR(10)+'SET QUOTED_IDENTIFIER ON;'+CHAR(10)+'SET NOCOUNT ON;'+CHAR(10)+CHAR(10);
SET @beb+='BEGIN TRAN'+CHAR(10);
SET @beb+='BEGIN TRY'+CHAR(10) +CHAR(10)+'IF(@CmdType=''I'')'+char(10)+'BEGIN'+char(10);

-- insert start
declare @insert varchar(1000) ='INSERT INTO '+@schema+'.'+@TBL+'(';

select @insert+=c.name+','
from INFORMATION_SCHEMA.COLUMNS Cl
join sys.columns C
    on c.is_identity=0 and Cl.TABLE_NAME=OBJECT_NAME(object_id) and cl.COLUMN_NAME=c.name
where cl.TABLE_NAME= @TBL and c.name <> 'Createdon';

SET @insert = LEFT(@insert,LEN(@insert)-1);
SET @insert+=') VALUES ('

select @insert+='@'+COLUMN_NAME+','
from INFORMATION_SCHEMA.COLUMNS cl
join sys.columns C
    on c.is_identity=0 and Cl.TABLE_NAME=OBJECT_NAME(object_id) and cl.COLUMN_NAME=c.name
where cl.TABLE_NAME= @TBL and c.name <> 'Createdon';

SET @insert = LEFT(@insert,LEN(@insert)-1);
SET @insert =REPLACE(@insert,'@CreatedBy','@UserId')
SET @insert =REPLACE(@insert,'@UpdatedBy','@UserId')
SET @insert =REPLACE(@insert,'@UpdatedOn','GETDATE()')
SET @insert+=');'+CHAR(10)+CHAR(10)+'END'+char(10)+'ELSE'+char(10)+'BEGIN'+char(10);

--insert end

-- update start

declare @update varchar(1000) ='UPDATE '+@schema+'.'+@TBL+' SET ';


select @update+=c.name+'=@'+c.name+','
from INFORMATION_SCHEMA.COLUMNS Cl
join sys.columns C
    on c.is_identity=0 and Cl.TABLE_NAME=OBJECT_NAME(object_id) and cl.COLUMN_NAME=c.name
where cl.TABLE_NAME= @TBL and c.name not like 'Create%';

set @update =left(@update,len(@update)-1);

declare @identityCol varchar(50)='';
select @identityCol=COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS cl
join sys.columns C
    on c.is_identity=1 and Cl.TABLE_NAME=OBJECT_NAME(object_id) and cl.COLUMN_NAME=c.name
where cl.TABLE_NAME= @TBL;

if(len(@identityCol) > 1)
begin
set @update +=' WHERE ';

set @update+=@identityCol+'=@'+@identityCol;

end


SET @update =REPLACE(@update,'@UpdatedBy','@UserId')
SET @update =REPLACE(@update,'@UpdatedOn','GETDATE()')
set @update +=char(10)+'END'+char(10)+char(10);
-- updaate end

DECLARE @END VARCHAR(50) ='COMMIT TRAN'+CHAR(10)+'END TRY'+CHAR(10);
DECLARE @CATCH VARCHAR(500) ='BEGIN CATCH'+CHAR(10)+'ROLLBACK TRAN'+CHAR(10)+CHAR(10)+CHAR(5)+CHAR(5)+CHAR(5)+'INSERT INTO ADMIN.TBLERRORlOG (CustomeError) VALUES (NULL);'+CHAR(10)+CHAR(10)+'END CATCH'+CHAR(10)+'END';


print @CR+CHAR(10)+@par+CHAR(10)+@beb+CHAR(10)+@insert+CHAR(10)+@update+CHAR(10)+@END+CHAR(10)+@CATCH+char(10)+'Go';

END

No comments:

Post a Comment