Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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 Server:有人能简化这个逻辑吗_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

SQL Server:有人能简化这个逻辑吗

SQL Server:有人能简化这个逻辑吗,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我的问题如下 declare @row_id int = 2 declare @last_row_id int =(select MAX(Row_ID) from dbo.Source) create table #source ( Row_ID float null, [Document] [nvarchar](255) NULL, [ITEMCode] [nvarchar](255) NULL, [Text] [nvarcha

我的问题如下

declare @row_id int = 2
declare @last_row_id int  =(select MAX(Row_ID) from dbo.Source)

create table #source  (
    Row_ID float null,
      [Document] [nvarchar](255) NULL,    
      [ITEMCode] [nvarchar](255) NULL,    
      [Text] [nvarchar](255) NULL)

while(@row_id<=(@last_row_id))
begin

declare @Document nvarchar(255)
declare @itemcode nvarchar(255)

select @itemcode=ITEMCode,@Document=Document from dbo.Source where Row_ID=@row_id


if ((@itemcode='' or @itemcode is null ) )
select @itemcode=ITEMCode,@Document=Document from #source where Row_ID=@row_id-1

insert into #source
select Row_ID,@Document,@itemcode,[Text]
from dbo.Source where Row_ID=@row_id

print @row_id

set @row_id= @row_id+1

end

select * from #source
drop table #source
输出应为:

 Row_ID  Document  ITEMCode Text    
   2       10223     20235  aaaa
   3       10223     20235  bbbb
   4       10223     20235  cccc
   5       10278    202475  xxxx
   6       10278    202475  yyyy
   7       10278    202475  yyy
您可以使用:

输出:

Row_ID      Document ITEMCode Text
----------- -------- -------- ----
2           10223    20235    aaaa
3           10223    20235    bbbb
4           10223    20235    cccc
5           10278    202475   xxxx
6           10278    202475   yyyy
7           10278    202475   yyy
如果Document和ITEMCode实际上是整数,而不是字符串,则上面的脚本可以工作,但通常最好更改这两行:

Document = ISNULL(NULLIF(r.Document, ''), f.Document),
ITEMCode = ISNULL(NULLIF(r.ITEMCode, ''), f.ITEMCode),
像这样:

Document = ISNULL(NULLIF(r.Document, 0), f.Document),
ITEMCode = ISNULL(NULLIF(r.ITEMCode, 0), f.ITEMCode),

尽管上面指定的查询可能有效,但我在excel中编写了一个宏,要求在不到5分钟的时间内快速获得输出

为什么在源代码中没有任何内容之前进行选择?当其中有任何内容时,我不进行选择。在dbo.Source的第一条记录中,ITEMCode不是空的,也不是空的。因此,该指令不会第一次执行。当循环进行到第二次时,如果源代码中有数据,那么如果您描述此代码的总体用途,您可能会得到更好的响应。代码很难读懂,我想我现在明白了。所以,您从源代码的当前行或上一行获取数据,对吗?如果是这样,就不需要WHILE循环。您可以改为自联接源,将行与行1匹配。有意义吗?当dbo.Source中的Row_Id看起来是int时,为什么Source中的Row_Id是float?我赞同切兰的建议,即你给出一个你想要实现的想法。您应该真正尝试避免RBAR操作,而使用集合操作-
Document = ISNULL(NULLIF(r.Document, ''), f.Document),
ITEMCode = ISNULL(NULLIF(r.ITEMCode, ''), f.ITEMCode),
Document = ISNULL(NULLIF(r.Document, 0), f.Document),
ITEMCode = ISNULL(NULLIF(r.ITEMCode, 0), f.ITEMCode),