请求验证这个可以说是复杂的SQL脚本

时间:2011-05-24 06:50:21

标签: sql sql-server tsql sql-server-2008

如果某些SQL专家可以查看此脚本并验证它是否可行或是否可以进行改进(性能,易用性等),我将不胜感激。因此,考虑以下因素:

Leads {
    LeadId INT,
    SourceId TINYINT,
    PersonId INT,
    PhoneId INT,
    EmailId INT,
    AddressId INT,
    ImporterId SMALLINT,
    ImportedDateTime DATETIME2(7)
}

Duplicates {
    DuplicateId INT,
    SourceId TINYINT,
    LeadId INT,
    ImporterId SMALLINT,
    DuplicatedDateTime DATETIME2(7)
}

此脚本将如何执行:

--  Outside variables provided as part of a stored procedure
DECLARE @SourceId TINYINT;
DECLARE @ImporterId SMALLINT;

PRINT 'Deleting the CSVTemp table if it exists';
IF ((SELECT CASE WHEN OBJECT_ID('CSVTemp') IS NOT NULL THEN 1 ELSE 0 END) = 1)
BEGIN
    DROP TABLE [CSVTemp];
END

PRINT 'Creating the CSVTemp table';
CREATE TABLE [CSVTemp](
    [FirstName] NVARCHAR(48),
    [LastName] NVARCHAR(48),
    [Phone] BIGINT,
    [Email] VARCHAR(96),
    [Street] VARCHAR(64),
    [Zip] INT
);

PRINT 'Performing a BULK insert into CSVTemp';
BULK INSERT [CSVTemp] FROM '{File}.csv' WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n');

PRINT 'Adding IDENTITY column to CSVTemp';
ALTER TABLE [CSVTemp] ADD [Id] INT IDENTITY(1,1) NOT NULL;

PRINT 'Adding PK constraint to CSVTemp';
ALTER TABLE [CSVTemp] ADD CONSTRAINT [PK_CSVTemp] PRIMARY KEY CLUSTERED(
    [Id] ASC
) WITH(
    PAD_INDEX = OFF,
    STATISTICS_NORECOMPUTE = OFF,
    IGNORE_DUP_KEY = OFF,
    ALLOW_ROW_LOCKS = ON,
    ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY];

PRINT 'Counting CSVTemp rows';
DECLARE @Count INT = (SELECT COUNT(1) FROM [CSVTemp]);

PRINT 'Declaring internal variables';
DECLARE @I INT = 0;

PRINT 'Looping through rows in CSVTemp';
WHILE (@I < (@Count + 1))
BEGIN
    BEGIN TRANSACTION
        DECLARE @FirstName NVARCHAR(48);
        DECLARE @LastName NVARCHAR(48);
        DECLARE @Phone BIGINT;
        DECLARE @Email VARCHAR(96);
        DECLARE @Street VARCHAR(64);
        DECLARE @Zip INT;

        SELECT  @FirstName = [FirstName],
                @LastName = [LastName],
                @Phone = [Phone],
                @Email = [Email],
                @Street = [Street],
                @Zip = [Zip] FROM [CSVTemp] WHERE ([Id] = @I);

        DECLARE @LeadId INT = (
            SELECT  [Leads].[LeadId]
            FROM    [People].[Person]
                    JOIN [Management].[Leads] ON ([Leads].[PersonId] = [Person].[PersonId])
                    JOIN [Communication].[Phones] ON ([Leads].[PhoneId] = [Phones].[PhoneId])
            WHERE   (([Person].[FirstName] LIKE @FirstName) OR ([Person].[LastName] LIKE @LastName))
                    AND ([Phones].[PhoneId] = @Phone)
        );

        IF (@LeadId IS NOT NULL)
        BEGIN
            INSERT INTO [Management].[Duplicates]([SourceId], [LeadId], [ImporterId]) VALUES(@SourceId, @LeadId, @ImporterId);
        END
        ELSE
        BEGIN
            INSERT INTO [People].[Person]([FirstName], [LastName]) VALUES(@FirstName, @LastName);

            DECLARE @PersonId INT = @@IDENTITY;

            INSERT INTO [Communication].[Phones]([PhoneTypeId], [Number]) VALUES(6, @Phone);

            DECLARE @PhoneId INT = @@IDENTITY;

            INSERT INTO [Communication].[Emails]([Address]) VALUES(@Email);

            DECLARE @EmailId INT = @@IDENTITY;

            INSERT INTO [Location].[Addresses]([PostalCode], [Street]) VALUES(@Zip, @Street);

            DECLARE @AddressId INT = @@IDENTITY;

            INSERT INTO [Management].[Leads]([SourceId], [PersonId], [PhoneId], [EmailId], [AddressId], [ImporterId]) VALUES(@SourceId, @PersonId, @PhoneId, @EmailId, @AddressId, @ImporterId);
        END
    COMMIT

    SET @I = (@I + 1);
END

PRINT 'Deleting CSVTemp table';
DROP TABLE [CSVTemp];

更新

@ Will / @ Mitch,我不知道你们是否还在,但我最终通过转换WHILE循环来使用CURSOR来完成脚本。我通过循环10.5k行来测试脚本,花了3-5秒,这对我很好。可悲的是,我以为我完全了解@Will对套装的看法,但我无法想出改进它的方法,所以我会保留原样。如果有人愿意给我一个关于@Will正在谈论的内容的示例脚本,我将不胜感激,如果没有,那么感谢@Will和@Mitch到目前为止的帮助。

无论如何,这是现在使用WHILE的更新CURSOR循环。

DECLARE @Id INT = 0;
DECLARE C1 CURSOR READ_ONLY FOR (SELECT [Id] FROM [CSVTemp]);

OPEN C1;
    FETCH NEXT FROM C1 INTO @Id;

    WHILE (@@FETCH_STATUS = 0)
    BEGIN
        BEGIN TRANSACTION
            DECLARE @FirstName NVARCHAR(48);
            DECLARE @LastName NVARCHAR(48);
            DECLARE @Phone BIGINT;
            DECLARE @Email VARCHAR(96);
            DECLARE @Street VARCHAR(64);
            DECLARE @Zip INT;

            SELECT  @FirstName = [FirstName],
                    @LastName = [LastName],
                    @Phone = [Phone],
                    @Email = [Email],
                    @Street = [Street],
                    @Zip = [Zip] FROM [CSVTemp] WHERE ([Id] = @Id);

            DECLARE @LeadId INT = (
                SELECT  [Leads].[LeadId]
                FROM    [People].[Person]
                        JOIN [Management].[Leads] ON ([Leads].[PersonId] = [Person].[PersonId])
                        JOIN [Communication].[Phones] ON ([Leads].[PhoneId] = [Phones].[PhoneId])
                WHERE   (([Person].[FirstName] LIKE @FirstName) AND ([Person].[LastName] LIKE @LastName))
                        AND ([Phones].[Number] = @Phone)
            );

            IF (@LeadId IS NOT NULL)
            BEGIN
                INSERT INTO [Management].[Duplicates]([SourceId], [LeadId], [ImporterId]) VALUES (@SourceId, @LeadId, @ImporterId);
            END
            ELSE
            BEGIN
                INSERT INTO [People].[Person]([FirstName], [LastName]) VALUES(@FirstName, @LastName);

                DECLARE @PersonId INT = SCOPE_IDENTITY();

                INSERT INTO [Communication].[Phones]([PhoneTypeId], [Number]) VALUES(6, @Phone);

                DECLARE @PhoneId INT = SCOPE_IDENTITY();

                INSERT INTO [Communication].[Emails]([Address]) VALUES(@Email);

                DECLARE @EmailId INT = SCOPE_IDENTITY();

                INSERT INTO [Location].[Addresses]([PostalCode], [Street]) VALUES(@Zip, @Street);

                DECLARE @AddressId INT = SCOPE_IDENTITY();

                INSERT INTO [Management].[Leads]([SourceId], [PersonId], [PhoneId], [EmailId], [AddressId], [ImporterId]) VALUES(@SourceId, @PersonId, @PhoneId, @EmailId, @AddressId, @ImporterId);
            END
        COMMIT

        FETCH NEXT FROM C1 INTO @Id;
    END
CLOSE C1;
DEALLOCATE C1;

1 个答案:

答案 0 :(得分:2)

请勿使用@@IDENTITY。使用SCOPE_IDENTITY():

  

SCOPE_IDENTITY和@@ IDENTITY返回   最后的标识值   在当前的任何表中生成   会话。但是,SCOPE_IDENTITY   返回仅在其中插入的值   目前的范围; @@ IDENTITY不是   限于特定范围。

     

例如,有两个表,T1和   T2和INSERT触发器被定义   在T1上。当一行插入T1时,   触发器触发并插入一行   T2。这个场景说明了两个   范围:T1上的插入和   通过触发器插入T2。

     

假设T1和T2都有   身份栏,@ @ IDENTITY和   SCOPE_IDENTITY将返回不同的内容   INSERT末尾的值   关于T1的声明。 @@ IDENTITY会   返回最后一个标识列值   插入到任何范围内   本届会议。这是价值   插入T2。 SCOPE_IDENTITY()会   返回插入的IDENTITY值   T1。这是最后一次插入   发生在同一范围内。该   SCOPE_IDENTITY()函数将返回   如果函数是null值   在任何INSERT语句之前调用   进入一个标识列出现在   范围。