如何减少tsql脚本中的插入数量?

时间:2017-12-14 15:55:51

标签: sql-server tsql

我只需要使用一个插页填写用户表。表格中有4列:

FirstName    LastName   Password    Email

现在我只能使用两个插件来完成它。首先,插入FirstName和LastName,然后根据其名称生成密码和电子邮件。因此,在第一次插入后,我将密码列和电子邮件列填充为NULL。这就是我需要从表中删除不必要的行的原因 以下代码:

DROP TABLE Users

CREATE TABLE Users(
    FirstName nvarchar(10) ,
    LastName nvarchar(10),
    Password nvarchar(128),
    Email nvarchar(30),
    )

INSERT INTO Users (FirstName, LastName) VALUES ('Adams','Armstrong'),
                                            ('John','Barnes'),
                                            ('Mark','Barnetty'),
                                            ('Mike','Ra'),
                                            ('Harry','Radley'),
                                            ('Nick','Kade'),
                                            ('Brian','Kael'),
                                            ('Lea','Fahim'),
                                            ('Julia','Dacey'),
                                            ('Anna','Octavio'),
                                            ('Kate','Yale')

INSERT INTO Users (FirstName, LastName, Password, Email) 
SELECT FirstName, LastName, Password, Email =
    CASE 
    WHEN Email = 0 THEN
    LOWER(LEFT(FirstName,1) + LastName) + '@hotmail.com'
    WHEN Email = 1 THEN
    LOWER(LEFT(FirstName,1) + LastName) + '@gmail.com'
    WHEN Email = 2 THEN
    LOWER(LEFT(FirstName,1) + LastName) + '@mail.ru'
    END
    FROM (SELECT  FirstName, LastName, REPLACE(LOWER(CONVERT(NVARCHAR(128), NEWID())), '-', '') as Password,
    CAST(RAND(CHECKSUM(NEWID())) * 3 as INT) as Email from Users) as random

DELETE FROM Users WHERE Password IS NULL and  Email IS NULL

SELECT * FROM Users

如何仅使用一个插入填充Users表?
附:这张表已经适合另一张了。

4 个答案:

答案 0 :(得分:2)

试试这个:

    INSERT INTO Users (FirstName, LastName, Password, Email) 
    SELECT FirstName, LastName
        , [Password] = REPLACE(LOWER(CONVERT(NVARCHAR(128), NEWID())), '-', '')
        , Email =
                CASE CAST(RAND(CHECKSUM(NEWID())) * 3 as INT)
                WHEN  0 THEN LOWER(LEFT(FirstName,1) + LastName) + '@hotmail.com'
                WHEN  1 THEN LOWER(LEFT(FirstName,1) + LastName) + '@gmail.com'
                WHEN  2 THEN LOWER(LEFT(FirstName,1) + LastName) + '@mail.ru'
                END
from (VALUES ('Adams','Armstrong'),
                                            ('John','Barnes'),
                                            ('Mark','Barnetty'),
                                            ('Mike','Ra'),
                                            ('Harry','Radley'),
                                            ('Nick','Kade'),
                                            ('Brian','Kael'),
                                            ('Lea','Fahim'),
                                            ('Julia','Dacey'),
                                            ('Anna','Octavio'),
                                            ('Kate','Yale')) a(FirstName, LastName)

答案 1 :(得分:1)

您可以使用CTE

DROP TABLE Users

CREATE TABLE Users(
    FirstName nvarchar(10) ,
    LastName nvarchar(10),
    Password nvarchar(128),
    Email nvarchar(30),
    )


;WITH Names AS (
    SELECT U.FirstName, U.LastName FROM (
    VALUES ('Adams','Armstrong'),
            ('John','Barnes'),
            ('Mark','Barnetty'),
            ('Mike','Ra'),
            ('Harry','Radley'),
            ('Nick','Kade'),
            ('Brian','Kael'),
            ('Lea','Fahim'),
            ('Julia','Dacey'),
            ('Anna','Octavio'),
            ('Kate','Yale')
    ) AS U (FirstName, LastName)
), RandomUsers AS (
SELECT 
      FirstName
    , LastName
    , REPLACE(LOWER(CONVERT(NVARCHAR(128), NEWID())), '-', '') as Password
    , CASE CAST(RAND(CHECKSUM(NEWID())) * 3 as INT)
        WHEN  0 THEN
        LOWER(LEFT(FirstName,1) + LastName) + '@hotmail.com'
        WHEN  1 THEN
        LOWER(LEFT(FirstName,1) + LastName) + '@gmail.com'
        WHEN  2 THEN
        LOWER(LEFT(FirstName,1) + LastName) + '@mail.ru'
        END AS Email
FROM Names
)
INSERT INTO Users(FirstName, LastName, Password, Email) 
SELECT FirstName, LastName, Password, Email 
FROM RandomUsers
WHERE Password IS NOT NULL AND Email IS NOT NULL 

SELECT * FROM Users

答案 2 :(得分:0)

我重写了你的查询。试试这个

DROP TABLE Users

CREATE TABLE Users(
    FirstName nvarchar(10) ,
    LastName nvarchar(10),
    Password nvarchar(128),
    Email nvarchar(30),
    )

;WITH CTE
AS
(
    SELECT FirstName = 'Adams',LastName = 'Armstrong',Email = CAST(RAND(CHECKSUM(NEWID())) * 3 as INT),[Password] = REPLACE(LOWER(CONVERT(NVARCHAR(128), NEWID())), '-', '')
    UNION ALL
    SELECT FirstName = 'John',LastName = 'Barnes',Email = CAST(RAND(CHECKSUM(NEWID())) * 3 as INT),[Password] = REPLACE(LOWER(CONVERT(NVARCHAR(128), NEWID())), '-', '')
    UNION ALL
    SELECT FirstName = 'Mark',LastName = 'Barnetty',Email = CAST(RAND(CHECKSUM(NEWID())) * 3 as INT),[Password] = REPLACE(LOWER(CONVERT(NVARCHAR(128), NEWID())), '-', '')
)
INSERT INTO Users (FirstName, LastName, Password, Email) 
SELECT 
    FirstName, 
    LastName, 
    [Password],
    Email =
    CASE 
        WHEN Email = 0 THEN
        LOWER(LEFT(FirstName,1) + LastName) + '@hotmail.com'
        WHEN Email = 1 THEN
        LOWER(LEFT(FirstName,1) + LastName) + '@gmail.com'
        WHEN Email = 2 THEN
        LOWER(LEFT(FirstName,1) + LastName) + '@mail.ru'
        END
    FROM CTE
        WHERE [Password] IS NOT NULL
            OR
            Email IS NOT NULL

SELECT * FROM Users

我只包含前3条记录,您可以添加更多联合以获取更多记录。

另外,我删除了Delete语句,因为它已经在Where条件

中被过滤了

答案 3 :(得分:0)

这是一个使用CROSS APPLY而不需要CTE的干净解决方案:

INSERT INTO Users (FirstName, LastName, Password, Email)
SELECT 
    Names.FirstName, 
    Names.LastName, 
    REPLACE(LOWER(CONVERT(NVARCHAR(128), NEWID())), '-', ''),
    CASE 
        WHEN EmailInfo.EmailType = 0 THEN EmailInfo.EmailName + '@hotmail.com'
        WHEN EmailInfo.EmailType = 1 THEN EmailInfo.EmailName + '@gmail.com'
        WHEN EmailInfo.EmailType = 2 THEN EmailInfo.EmailName + '@mail.ru'
    END
FROM (
        VALUES 
        ('Adams','Armstrong'),
        ('John','Barnes'),
        ('Mark','Barnetty'),
        ('Mike','Ra'),
        ('Harry','Radley'),
        ('Nick','Kade'),
        ('Brian','Kael'),
        ('Lea','Fahim'),
        ('Julia','Dacey'),
        ('Anna','Octavio'),
        ('Kate','Yale')
    ) Names (FirstName, LastName)
    CROSS APPLY (
        SELECT 
            LOWER(LEFT(FirstName,1) + LastName) AS EmailName,
            CAST(RAND(CHECKSUM(NEWID())) * 3 as INT) AS EmailType
    ) EmailInfo