简体   繁体   English

表值作为SQL Server存储过程的参数

[英]Table values as parameters for SQL Server stored procedure

I have table1 :col1, col2, col3 and table2: col1, col2, col3 我有table1:col1,col2,col3和table2:col1,col2,col3
My goal is to get all records 我的目标是获取所有记录

where     
t2.col1 like t1.col1 and    
t2.col2 like t1.col2 and    
t2.col3 like t1.col3   

........................................ ......................................................

One variant is the inner join method 一种变体是内部联接方法

select * from t2 inner join t1 on    
t2.col1 like t1.col1  and  
t2.col2 like t1.col2  and  
t2.col3 like t1.col3     

........................................ ......................................................

Another variant is a stored procedure based on the 'where' clause: 另一个变体是基于'where'子句的存储过程:

select *  from t2     
where t2.col1 like parameter1  and      
t2.col2 like parameter2  and     
t2.col3 like parameter3    

Then I call the procedure in VBA and I use a for next loop to go through all values/parameters from an excel table1 然后我在VBA中调用该过程,并使用for下一个循环遍历excel table1中的所有值/参数
........................................ .........................................................
Execution time for the join method is slower(~20, 30%) than vba+sp method, but unfortunately, for a big set of parameters, excel freeze. join方法的执行时间比vba + sp方法要慢(〜20,30%),但是不幸的是,对于大量参数,excel冻结。
........................................ .........................................................
Is possible to apply loop method and go thru table1 values, as parameters for the stored procedure, inside sql server, in a sql script, no vba or c++ or perl etc. ? 是否可以应用循环方法并遍历table1的值作为sql服务器内部,sql脚本中的存储过程的参数,而不使用vb​​a或c ++或perl等?

I am a user with no access to db/tables design. 我是无法访问数据库/表设计的用户。

Thank you 谢谢

在此处输入图片说明

First of all, your two queries in the question are not equivalent: 首先,您在问题中的两个查询不相同:

select * from t2 inner join t1 on    
t1.col1 like t2.col1  and  
t1.col2 like t2.col2  and  
t1.col3 like t2.col3     

Here you have t1 like t2 在这里你有t1 like t2

select *  from t2     
where t2.col1 like parameter1  and      
t2.col2 like parameter2  and     
t2.col3 like parameter3    

Here it is other way around t2 like t1 . 这里是围绕t2 like t1另一种方式, t2 like t1

End result would be different. 最终结果会有所不同。

Based on the sample data it looks like it should be t2 like t1 . 根据样本数据,看起来应该t2 like t1一样是t2 like t1


You can try to re-write the query using CROSS APPLY instead of JOIN , but it is unlikely to make any difference in performance. 您可以尝试使用CROSS APPLY而不是JOIN来重写查询,但是这不会对性能产生任何影响。

SELECT *
FROM
    t1
    CROSS APPLY
    (
        SELECT
        FROM t2
        WHERE
            t2.col1 like t1.col1
            and t2.col2 like t1.col2
            and t2.col3 like t1.col3
    ) AS A
;

This query structure mimics your stored procedure approach where for each row from t1 you select a set of rows from t2 . 此查询结构模仿您的存储过程方法,其中对于t1中的每一行,您要从t2选择一组行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM