Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 迭代变量列表以构建tmp_Sql_Variables_Loops_Rows - Fatal编程技术网

Sql 迭代变量列表以构建tmp

Sql 迭代变量列表以构建tmp,sql,variables,loops,rows,Sql,Variables,Loops,Rows,在比较两个查询时,我使用了大量的数字作为变量。查询运行起来相当耗时,因此我希望能够自动完成这项工作,以便对两个查询返回的具有不同行的所有变量进行排序。我愿意接受任何建议 @key使用的号码位于另一台服务器上。我有第三个查询返回这些数字 我将在下面发布这两个问题 DECLARE @SubscriberID bigint DECLARE @key int set @key = 128604576 SELECT TOP(1) @SubscriberID = M.WarehouseID

在比较两个查询时,我使用了大量的数字作为变量。查询运行起来相当耗时,因此我希望能够自动完成这项工作,以便对两个查询返回的具有不同行的所有变量进行排序。我愿意接受任何建议

@key使用的号码位于另一台服务器上。我有第三个查询返回这些数字

我将在下面发布这两个问题

  DECLARE @SubscriberID bigint
  DECLARE @key int
  set @key = 128604576

  SELECT TOP(1) @SubscriberID = M.WarehouseID
  FROM vwCache_Full V (NOLOCK)
  LEFT JOIN Latest m (NOLOCK)
  ON m.WarehouseID = v.PersonKey 
  WHERE V.memberid = @key   
  ORDER BY
  V.tapeid DESC, V.linekeyid DESC 


  SELECT TOP 1
        AddrLn1, AddrLn2 
  FROM Latest 
  WHERE 
        WarehouseID = @SubscriberID 
  ORDER BY 
        TapeID DESC, LineKeyID DESC
--下一个查询

  SELECT TOP 1 m.AddrLn1, m.AddrLn2
  FROM Latest m (NOLOCK)
  INNER JOIN Latest p ON m.WarehouseID = p.WarehouseID 
  WHERE m.SSN = @key
  ORDER BY m.TapeID Desc
在这里找到了另一个有助于解决此问题的答案。。。使用

DECLARE @RowsToProcess  int
DECLARE @CurrentRow     int
DECLARE @SelectCol1     int

DECLARE @table1 TABLE (RowID int not null primary key identity(1,1), col1 int )  
INSERT into @table1 (col1) SELECT col1 FROM table2
SET @RowsToProcess=@@ROWCOUNT

SET @CurrentRow=0
WHILE @CurrentRow<@RowsToProcess
BEGIN
    SET @CurrentRow=@CurrentRow+1
    SELECT 
        @SelectCol1=col1
        FROM @table1
        WHERE RowID=@CurrentRow

    --do your thing here--
DECLARE@RowsToProcess int
声明@CurrentRow int
声明@SelectCol1 int
声明@table1 TABLE(RowID int not null主键标识(1,1),col1 int)
插入到@table1(col1)中从表2中选择col1
设置@RowsToProcess=@@ROWCOUNT
设置@CurrentRow=0
而@CurrentRow