基于select和check记录插入多个记录存在+ Sql

时间:2015-10-07 14:39:12

标签: sql sql-server

您好我已经创建了一个SQL查询,如下所示。基本上我需要实现两件事。 1.仅当correlationidssage表中不存在correlationid时才插入记录。 2.插入两个记录,其中MessageTypeId = 1,MessageTypeId = 2.

我已经尝试并实现了第二个,但不知道如何做第一个

    bool specialChk(string a) 
{
        for (int i = 0; i < a.length(); i++)
        {
            if (a[i] == '`' || a[i] == '~' || a[i] == '!' || a[i] == '@' || a[i] == '#' || a[i] == '$' || a[i] == '%' || a[i] == '^' || a[i] == '&' || a[i] == '*' || a[i] == '(' || a[i] == ')' || a[i] == '-' || a[i] == '_' || a[i] == '=' || a[i] == '+' || a[i] == '[' || a[i] == ']' || a[i] == '{' || a[i] == '}' || a[i] == ';' || a[i] == ':' || a[i] == '\'' || a[i] == '"' || a[i] == ',' || a[i] == '<' || a[i]== '>' || a[i] == '.' || a[i] == '/' || a[i] == '?' || a[i] == '|' || a[i] == '\\')
                return true;
        }
    }

bool digitChk(string a)
{
    for (int i = 0; i < a.length(); i++)
    {
        if (a[i] == '1' || a[i] == '2' || a[i] == '3' || a[i] == '4'|| a[i] == '5' || a[i] == '6' || a[i] == '7' || a[i] == '8' || a[i] == '9' || a[i] == '0')
            return true;
    }

}

int symbolChk(string a) // combining symbol checks
{
    if (specialChk(a) && digitChk(a))
        return 2;
    else if (specialChk(a) || digitChk(a))
        return 1;
    else
        return 0;
}

此查询与基于select执行多次插入以及检查记录是否存在有点不同。由于这是一个联盟,所以检查是非常棘手的。

1 个答案:

答案 0 :(得分:0)

在插入中,您可以:

select * from
(Select ActivityID,1 as MessageTypeId,NULL as RequestMessage  ,NULL as ResponseMessage,1 as IsMatched ,GETDATE() as CreatedDate  from account.Activity a
join account.ActivityType b on b.ActivityTypeID=a.ActivityTypeID
join ACCOUNT.TransactionType c on c.TransactionTypeID=a.TransactionTypeID
where a.ActivityTypeID=3
and ExecutingBroker is null
and a.active=1 
and TradeDate is null
and DateforGTDOrders > GETDATE()
union
Select ActivityID,2 as MessageTypeId,NULL as RequestMessage  ,NULL as ResponseMessage,1 as IsMatched ,GETDATE() as CreatedDate  from account.Activity a
join account.ActivityType b on b.ActivityTypeID=a.ActivityTypeID
join ACCOUNT.TransactionType c on c.TransactionTypeID=a.TransactionTypeID
where a.ActivityTypeID=3
and ExecutingBroker is null
and a.active=1 
and TradeDate is null
and DateforGTDOrders > GETDATE()) ab
where not exits ( select 1 from MESSAGING.CorrelationMessage a where a.CorrelationId = ab.CorrelationId and ab.MessageTypeId = a.MessageTypeId)

我没有尝试过这个查询,但是请试一试,我知道这不是最优化的方法。但是,你需要调整它。这只是一个想法。