SQL - 创建批处理 - 2100 LIMIT

时间:2014-09-30 15:56:07

标签: sql sql-server-2012 batch-processing

我在查询中发送了一个电子邮件列表,但是我得到一个错误,说它无法处理超过2100,我肯定有更多可能是这个数量的三倍。如何批处理并处理此批处理?

sql = """with x as ( \
                            select \
                                job_id, \
                                avg_runtime, \
                                j.id, \
                                row_number() over (partition by ja.job_id order by ja.id desc) rn \
                            from job_activity as ja   \
                            join job as j on ja.job_id = j.id  \
                            where \
                                j.name in ({0})  and \
                                ja.avg_runtime <> 0 and \
                                ja.avg_runtime is not null ) \
                            select \
                                job_id, \
                                avg_runtime, \
                                id \
                            from x where rn = 1 ";"""



            params = ",".join(map(lambda x: "?", flagged_job_names))
            dataSet = data.Engine.execute(sql.format(params), flagged_job_names)
            dataSet.fetchall()

2 个答案:

答案 0 :(得分:0)

以下是我们的工作:

  1. 以逗号分隔的字符串
  2. 发送电子邮件列表
  3. 使用转弯下方的tvf到相当于一个表
  4. 使用常规联接声明
  5. 下面的伪代码:

    DECLARE @emails varchar(max);
    SET @emails = 'foo@foo.com;bar@foo.com';
    
    SELECT *
    FROM table1 t
       INNER JOIN dbo.tvf_splitParam(@emails, ',') tvf ON (t.email = tvf.value)
    
    
    
    
    
    ALTER FUNCTION [dbo].[tvf_SplitParam](
        @RepParam nvarchar(max), 
        @Delim char(1)= ','
    )
    RETURNS @VALUES TABLE (value nvarchar(max))AS
       BEGIN
       DECLARE @chrind INT
       DECLARE @Piece nvarchar(max)
       SELECT @chrind = 1
       WHILE @chrind > 0
          BEGIN
             SELECT @chrind = CHARINDEX(@Delim,@RepParam)
             IF @chrind > 0
                SELECT @Piece = LEFT(@RepParam,@chrind - 1)
             ELSE
                SELECT @Piece = @RepParam
             INSERT @VALUES(value) VALUES(@Piece)
             SELECT @RepParam = RIGHT(@RepParam,LEN(@RepParam) - @chrind)
             IF LEN(@RepParam) = 0 BREAK
          END
       RETURN
    END
    

答案 1 :(得分:0)

对于SQL Server 2012,您可以使用table valued parameters

-- How to create an array for storing emails ? (SQL2008+)
CREATE TYPE dbo.StringArray AS TABLE ( 
    Value NVARCHAR(100) NOT NULL 
);
GO

-- How to create a procedure having this array as parameter ?
CREATE PROCEDURE dbo.DoSomething (
    @emails dbo.StringArray READONLY
)
AS
BEGIN 
    SELECT  *
    FROM    dbo.Person p
    WHERE   p.Email IN (SELECT  v.Value FROM @emails v)
END;
GO

-- How to call this procedure ?
DECLARE @param dbo.StringArray
INSERT  @param 
VALUES  ('email1@yahoo.com'), ('email2@google.com'), ('email3@yuhuu.com')

EXEC dbo.DoSomething @emails = @param

注意:Erland Sommarskog编译了一个用于在SQL Server here中存储/传递数组的解决方案的综合列表。