将现有存储过程复制到不同的数据库和表

时间:2019-08-13 11:31:45

标签: sql-server stored-procedures view datatables

我正在尝试将以下过程复制到我的表中,但是机器人能够处理许多定义的变量,需要帮助

已复制了大多数过程,但无法获得所需的输出。下面是不同数据库和表上的SP

USE [GPC_Acquisition]
GO

/****** Object:  StoredProcedure [dbo].[usp_GPC_ACQ_PSSQLQry]    
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- Description : This Procedure populates the GPC_ACQ_PSSQLQry table with SQL query for each of the different peoplesoft record names, these query will be used in the 
-- SSIS package Source to create the output file 
-- for a given Destination table Name like GPC_REP_CustromerMaster. AliasFieldName are used if there are duplicate Fields in the Source table Ex : like Name1
-- Return : None
--execute [dbo].[usp_GPC_ACQ_PSSQLQry] 2,1,'GPC_REP_CustomerMaster'
CREATE procedure [dbo].[usp_GPC_ACQ_PSSQLQry]
(
    @inFileTypeID INT,
    @inACQID INT,
    @inTblName as Varchar(50)
)
AS
Declare @sqlStrVal as varchar(max),
        @lPSRecName as varchar(50),
        @ltblName as varchar(100), --='GPC_Customer_Master',
        @lRcdCnt as INT,
        @i as INT =1,
        @lSrcTblName as varchar(50),
        @lACQID INT,
        @lFileTypeID INT,
        @lSqlWCond  varchar(200)


        SET @ltblName = @inTblName
        SET @lACQID = @inACQID
        SET @lFileTypeID = @inFileTypeID;


        IF OBJECT_ID('tempdb..#StgDefs') IS NOT NULL
        DROP TABLE #StgDefs;

        IF OBJECT_ID('tempdb..#PSTblNames') IS NOT NULL
        DROP TABLE #PSTblNames;

-- Creating the List of Fields from StageMetaData Def table for the FileType and AcquisitionID
    with SrcDef
    as
    (
    Select CASE WHEN LEN(SrcFieldNameAlias) >0  THEN SrcFieldNameAlias ELSE  [SrcFieldName] eND [SrcFieldName] ,[SrcFieldDesc],[SequenceNo],
    sdf.FileTypeID,sdf.ACQMasterID,[STG_tableName] from [GPC_ACQ_SrcTblMetaDataDefs]  sdf
    inner join GPC_ACQ_FileType ft on 
    ft.ACQMasterID= sdf.ACQMasterID and ft.FileTypeID= sdf.FileTypeID 
    where  ft.FileTypeID=@lFileTypeID
    ),
    --Selecting the Field list required for the SQL String by linking Stage Field and Dest MetaData Field for the required Desttablename
    --,FileType and ACQID
    CTE_Stg 
    as
    (
    SELECT  ddf.[ACQMasterID],ddf.[FileTypeID],DDF.[slno],
    [SrcFieldName] ,CASE WHEN LEN(DestAliasFieldName) > 0 THEN DestAliasFieldName ELSE DestFieldName END AS DestFieldName,ddf.[DestTableName],ddf.[PeoplesoftRecordName],
    ddf.Defaultvalues,sdf.[STG_tableName]
    FROM SrcDef sdf
    right outer join [vw_GPC_DestMetaDataDefs] ddf on
    sdf.SrcFieldName=CASE WHEN LEN(DestAliasFieldName) > 0 THEN DestAliasFieldName ELSE DestFieldName END
    and sdf.FileTypeID= ddf.FileTypeID and sdf.ACQMasterID= ddf.ACQMasterID
    where  [DestTableName]=@ltblName
    ) 

-- Moving data to Temp table 
-- added distinct as there can be duplicate Fieldnames for diff PS Rec Names
select distinct * into #StgDefs from CTE_Stg


-- Selecting the PSRecord name and Sequence No into Temp table for the Selected Table Name
    Select distinct [PeoplesoftRecordName],DestSequenceNo into #PSTblNames from [vw_GPC_DestMetaDataDefs] where DestTableName=@ltblName 

-- Getting the Source tableName for the Select query 
    select @lSrcTblName=STG_tableName from [dbo].[GPC_ACQ_FileType] where FileTypeID = @lFileTypeID and ACQMasterID = @lACQID

    select @lRcdCnt=count(distinct PeoplesoftRecordName) from #StgDefs

-- Clearing the Existing SQls for the Detination table
    Delete from GPC_ACQ_PSSQLQry where DestTablename=@ltblName


-- Looping Thru the Field List from the Temp table to build the Dynamic Select statement for each of the PSrecordname
--These SQL Strings will be assigned to Variables in the SSIS package
While (@i<=@lRcdCnt)
BEGIN

       SET @sqlStrVal =''
       SET @lSqlWCond =''
       SELECT @lPSRecName= [PeoplesoftRecordName]      from #PSTblNames where DestSequenceNo=@i

       select  @sqlStrVal=@sqlStrVal + case when [SrcFieldName] is null then '''' + DestFieldName  + ''' as '  + DestFieldName else DestFieldName end + ',' 
       from #StgDefs where PeoplesoftRecordName=@lPSRecName  ORDER BY slno

       --SET @sqlStrVal ='Select ' + @sqlStrVal + ' From ' + @lSrcTblName 
       if (@ltblName='GPC_REP_CustomerContact') 
       BEGIN
            SET @sqlStrVal= CASE WHEN CharIndex('GPC_SUB',@sqlStrVal) =0 THEN  @sqlStrVal + 'GPC_SUB,' ELSE  @sqlStrVal END
            SET @sqlStrVal= CASE WHEN  CharIndex('GPC_ACQ_CD',@sqlStrVal) =0 THEN @sqlStrVal + 'GPC_ACQ_CD,' ELSE @sqlStrVal END
            SET @sqlStrVal= CASE WHEN  CharIndex('CUST_ID',@sqlStrVal) =0 THEN @sqlStrVal + 'CUST_ID,' ELSE @sqlStrVal END
       END
          -- remove the last ','
       SELECT @sqlStrVal=LEFT(@sqlStrVal, len(@sqlStrVal)-1)

       -- For Non CRC
       --SET @sqlStrVal ='Select [Slno],FM.FileExecMasterID,CRCID,v.[ACQMasterID],FM.FileTypeID,''' +  @lPSRecName + ''' PSName, ' + @sqlStrVal + ' From ' + @lSrcTblName  +  ' V INNER JOIN  [dbo].[GPC_FileExecMaster] FM on FM.[ACQMasterID]= v.[ACQMasterID]  and FM.[FileExecMasterID]= V.[FileExecMasterID] WHERE V.DataMappingErrorTypeID IS NULL '
       -- For CRC
       SET @sqlStrVal ='Select [Slno],FM.FileExecMasterID,CRCID,v.[ACQMasterID],FM.FileTypeID,UpdateStatus,''' +  @lPSRecName + ''' PSName, ' + @sqlStrVal + ' From ' + @lSrcTblName  +  ' V INNER JOIN  [dbo].[GPC_FileExecMaster] FM on FM.[ACQMasterID]= v.[ACQMasterID]  and FM.[FileExecMasterID]= V.[FileExecMasterID] WHERE V.DataMappingErrorTypeID IS NULL '

       SET @lSqlWCond =CASE WHEN @lPSRecName='PS_GPC_CUS_EXM_STG' THEN  ' AND (LEN(LTRIM(RTRIM(TAX_EXEMPT_CERT))) > 0 OR LEN(LTRIM(RTRIM(ISSUING_AUTHORITY))) >0 OR LEN(LTRIM(RTRIM(AUTHORITY_TYPE))) > 0) '
                            WHEN @lPSRecName='PS_GPC_CUS_EX_DTL' THEN ' AND (LEN(LTRIM(RTRIM(TAX_EXEMPT_CERT))) > 0 OR LEN(LTRIM(RTRIM(TAX_EXEMPT_FLAG))) >0 OR LEN(LTRIM(RTRIM(EXEMPT_CATEGORY))) > 0)'
                            WHEN @lPSRecName='PS_GPC_CUS_SIC_CD' THEN ' AND (LEN(LTRIM(RTRIM(SIC_CD_QUAL))) > 0 OR LEN(LTRIM(RTRIM(SIC_CODE))) >0)'
                            WHEN @lPSRecName='PS_GPC_CUS_TEAM_ST' THEN ' AND (LEN(LTRIM(RTRIM(SUPPORT_TEAM_CD))) > 0)'
                            WHEN @lPSRecName='PS_GPC_CUS_VAT_RG' THEN ' AND (LEN(LTRIM(RTRIM(VAT_RGSTRN_ID))) > 0 Or LEN(LTRIM(RTRIM(COUNTRY_2CHAR))) > 0)'
                            WHEN @lPSRecName='PS_GPC_CUS_VAT_EX' THEN ' AND (LEN(LTRIM(RTRIM(COUNTRY_CODE))) > 0 OR LEN(LTRIM(RTRIM(VAT_EXCPTN_TYPE))) >0 OR LEN(LTRIM(RTRIM(VAT_EXCPTN_CERTIF))) > 0)'                         
                            WHEN @lPSRecName='PS_GPC_CUS_CG_LNK' THEN ' AND (LEN(LTRIM(RTRIM(CUST_GRP_TYPE))) > 0 OR LEN(LTRIM(RTRIM(CUSTOMER_GROUP))) >0 OR LEN(LTRIM(RTRIM(DEFAULT_TAX_GRP))) > 0)'
                            WHEN @lPSRecName='PS_GPC_CUS_ID_NBR' THEN ' AND (LEN(LTRIM(RTRIM(STD_ID_NUM_QUAL))) > 0 OR LEN(LTRIM(RTRIM(STD_ID_NUM))) >0)' 
                            WHEN @lPSRecName='PS_GPC_CONTACT_STG' THEN ' AND (LEN(LTRIM(RTRIM(CUST_Name))) > 0 )' 
                            WHEN @lPSRecName='PS_GPC_CONTCT_CUST' THEN ' AND (LEN(LTRIM(RTRIM(CUST_Name))) > 0 )' 
                            WHEN @lPSRecName='PS_GPC_CUS_CNTCT' THEN ' AND (LEN(LTRIM(RTRIM(CUST_Name))) > 0 )' 
                            WHEN @lPSRecName='PS_GPC_CUS_CONT_PH' THEN ' AND (LEN(LTRIM(RTRIM(CUST_Name))) > 0 )' END


        SET @sqlStrVal = CONCAT(@sqlStrVal,@lSqlWCond)


       INSERT INTO GPC_ACQ_PSSQLQry(ACQMasterID,FileTypeID,PSRecName,DestSequenceNo,SQLStr,DestTableName)
       values(@lACQID,@lFileTypeID,@lPSRecName,@i,@sqlStrVal,@ltblName)

       SET @i=@i +1

END 


GO

这是我要为其创建SP的数据库以及表和列的名称 第1个表格名称:[AFG_LOB_DESTTBLMETADATA_DEFS]

列名:[Dest],[lob_master_id],[Division_ID],[File_Type_Id],[Sl_No],[Seq_No],[Dest_field_name],[Dest_Field_Alias],[Dest_Field_desc],[Dest_Data_Type],[Sec_Length],[Is_Required],[Create_Dt],[Create_By],[Update_Dt],[Effective_Start_Date],[Effective_End_Date],[Is_Active]

第二个表名称:[AFG_LOB_SrcTblmetaDataDefs]

列名:[SrctbMetaDataDefsID],[LOB_Master_ID],[Division_ID],[File_Type_ID],[Sl_No],[SequenceNo],[SrcFieldName],[SrcFieldNameAlias],[SrcFieldDesc],[SrcDataType],[SecLength],[IsRequired],[CreatedDt],[CreateBy],[Updated_Dt],[EffectiveStartDate],[EffectiveEndDate],[IsActive]

1 个答案:

答案 0 :(得分:0)

在SQL Server中没有专门的工具可以做到这一点。这就是我的做法。

  1. 在SSMS中打开存储过程的整个文本的副本。

  2. 根据需要手动更改USE语句,表和列,变量名以及存储过程的名称。

  3. 执行脚本以在另一个数据库上创建新的存储过程。

  4. 另存为...以将脚本保存到新文件。