存储过程中的动态SQL

USE [DWH];
GO
 
/****** Object:  Table  [dbo].[imp_$Item Entry]  Script Date: 11/29/2019 6:46:45 PM ******/
SET ANSI_NULLS ON;
GO
 
SET QUOTED_IDENTIFIER ON;
GO
 
CREATE TABLE [dbo].[imp_$Item Entry]
(
    [TS] [BIGINT] NULL,
    [Entry No_] [INT] NOT NULL,
    [Item No_] [VARCHAR](20) NOT NULL,
    [Posting Date] [DATETIME] NOT NULL,
    [Entry Type] [INT] NOT NULL
) ON [PRIMARY];
GO
USE [DWH];
GO
 
/****** Object:  View [dbo].[dms_db_config]    Script Date: 11/29/2019 6:31:46 PM ******/
SET ANSI_NULLS ON;
GO
 
SET QUOTED_IDENTIFIER ON;
GO
 
-- =============================================
-- Author:        Zhu Yue
-- Create Date:   2019-11-27
-- updated Date:  2019-11-29
-- Description:   Get valid databases from DMS instance.
-- =============================================
 
CREATE VIEW [dbo].[dms_db_config]
AS
SELECT [name] AS [DB_Name],
       '796' + RIGHT([name], 5) AS [Dealer_Code],         -- combine Dealer code
       ROW_NUMBER() OVER (ORDER BY [database_id]) AS RN
FROM [master].[sys].[databases]
WHERE [compatibility_level] = 80
GO
USE [DWH]
GO
 
/****** Object:  StoredProcedure [dbo].[usp_imp_$Item Entry_tins]    Script Date: 11/29/2019 6:49:00 PM ******/
SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
 
-- =============================================
-- Author:        Zhu Yue
-- Create Date:   2019-11-27
-- updated Date:  2019-11-29
-- Description:   Get dealers parts data for current year month.
-- =============================================
CREATE PROCEDURE [dbo].[usp_imp_$Item Entry_tins]
    @DB_Name NVARCHAR(50),
    @Full_Object_Name NVARCHAR(100),
    @Dealer_Code NVARCHAR(20)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
 
    DECLARE @min_ts BIGINT,
            @sys_month VARCHAR(6) = CONVERT(VARCHAR(6), GETDATE(), 112), --Get current sys year month
            @cmd NVARCHAR(MAX),
            @sql NVARCHAR(MAX);
 
    -- Reture the minimum timestamp for current year month
    SET @cmd = N'
    SELECT @min_ts = MIN(CONVERT(BIGINT, [timestamp]))
    FROM ' + @Full_Object_Name + N'
    WHERE CONVERT(VARCHAR(6), CAST([Creation Date] AS DATE), 112) = ' + @sys_month;
 
    EXEC sp_executesql @cmd, N'@min_ts bigint output', @min_ts OUTPUT;
 
    IF (@min_ts IS NULL)
    BEGIN
        PRINT (CONVERT(VARCHAR(20), GETDATE(), 120) + ' Object:"' + @Full_Object_Name + '": No Available Data!');
    END;
    ELSE
    BEGIN
        EXECUTE (N'
        INSERT INTO [VGIC_DWH].[dbo].[imp_DMS_$Item Ledger Entry]
        (
            [TS],
            [Entry No_],
            [Item No_],
            [Posting Date],
            [Entry Type]
        )
        SELECT CONVERT(BIGINT, [timestamp]) [TS],
               [Entry No_],
               [Item No_],
               [Posting Date],
               [Entry Type]
          FROM ' + @Full_Object_Name + '
          WHERE CONVERT(BIGINT, [timestamp]) >= ' + @min_ts); -- Use minimum timestamp to retrive current year month data
    END;
 
END;
 
GO
USE [DWH];
GO
 
/****** Object:  StoredProcedure [dbo].[usp_DMS_Data_Consolidation]    Script Date: 11/29/2019 7:05:47 PM ******/
SET ANSI_NULLS ON;
GO
 
SET QUOTED_IDENTIFIER ON;
GO
  
-- =============================================
-- Author:        Zhu Yue
-- Create date:   2019-11-29
-- Update date:   2019-11-29
-- Description:   Dynamic SQL unction to retrieve all the data object with the input parameter
-- =============================================
CREATE PROCEDURE [dbo].[usp_DMS_Data_Consolidation]
    -- Add the parameters for the stored procedure here
    @Object NVARCHAR(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
 
    DECLARE @Object_Name NVARCHAR(100) = @Object,
            @Full_Object_Name NVARCHAR(100),
            @DB_Name NVARCHAR(50),
            @TB_Name NVARCHAR(100),
            @Dealer_Code NVARCHAR(100),
            @Tar_Tins NVARCHAR(50),
            @Tar_Usp NVARCHAR(50),
            @rc INT,
            @i INT = 1,
            @sql NVARCHAR(500);
 
    SELECT @rc = COUNT(*)
    FROM [DWH].[dbo].[dms_db_config];
 
    SET @Tar_Tins = N'[imp_' + @Object_Name + N']';
    SET @Tar_Usp = N'[usp_imp_' + @Object_Name + N'_tins]';
 
    EXECUTE (N'TRUNCATE TABLE [DWH].[dbo].' + @Tar_Tins);
 
    WHILE @i <= @rc
    BEGIN
 
        SELECT @DB_Name = [DB_Name],
               @Dealer_Code = [Dealer_Code]
        FROM [DWH].[dbo].[dms_db_config]
        WHERE RN = @i;
 
        SET @DB_Name = N'[' + @DB_Name + N']';
 
        SET @sql
            = N'SELECT @TB_Name = [TABLE_NAME] FROM ' + @DB_Name
              + N'.[INFORMATION_SCHEMA].[TABLES]
              WHERE [TABLE_NAME] LIKE ''%' + @Object_Name + N'''';
 
        EXEC sp_executesql @sql, N'@TB_Name NVARCHAR(100) output', @TB_Name OUTPUT;
 
        SET @Full_Object_Name = @DB_Name + N'.[dbo].' + N'[' + @TB_Name + N']';
 
        -- Dynamic SQL function to execute the corresponding USP
        EXECUTE (N'
                   EXECUTE [VGIC_DWH].[dbo].' + @Tar_Usp + N' @DB_Name = ''' + @DB_Name + ''',
                                                              @Full_Object_Name = ''' + @Full_Object_Name + ''',
                                                              @Dealer_Code =  ''' + @Dealer_Code + '''');
 
        SET @i = @i + 1;
 
    END;

END;
GO

你可能感兴趣的:(存储过程中的动态SQL)