0

我正在尝试一个 sql 查询

alter procedure [dbo].[sp_E_GetProducts]
@skip as INT = 0,
@take as INT = 999999,
@search as NVARCHAR(50) = '',
@urtkod as NVARCHAR(50) = ''
as
begin
; with MyTable as ( 
select sto_kod, sto_isim, sto_sat_cari_kod, ROW_NUMBER() OVER (ORDER BY sto_kod) as row from MikroDB_V14_AKCAY2010.dbo.STOKLAR where
(sto_isim like '%' + @search + '%' or sto_kod like '%' + @search + '%') and sto_sat_cari_kod <> '' and  @urtkod like '%' + sto_sat_cari_kod + '%'

)

select * from MyTable where row between @skip and @take
end

当我将参数设置为非空但当@urtkod 为空时,它的工作原理我想获取所有表(这意味着“和 sto_sat_cari_kod <> '' 和 @urtkod 像 '%' + sto_sat_cari_kod + '%'”将忽略),我研究“情况下”,但它没有用,或者我做错了。

4

2 回答 2

2
alter procedure [dbo].[sp_E_GetProducts]
@skip as INT = 0,
@take as INT = 999999,
@search as NVARCHAR(50) = '',
@urtkod as NVARCHAR(50) = ''
as
begin
    If (@urtkod is null)
    Begin
        ; with MyTable as ( 
        select sto_kod, sto_isim, sto_sat_cari_kod, ROW_NUMBER() OVER (ORDER BY sto_kod) as row from MikroDB_V14_AKCAY2010.dbo.STOKLAR where
        (sto_isim like '%' + @search + '%' or sto_kod like '%' + @search + '%') and sto_sat_cari_kod <> '' and  @urtkod like '%' + sto_sat_cari_kod + '%'

        )
    End
    Else
    Begin
        ; with MyTable as ( 
        select sto_kod, sto_isim, sto_sat_cari_kod, ROW_NUMBER() OVER (ORDER BY sto_kod) as row from MikroDB_V14_AKCAY2010.dbo.STOKLAR where
        (sto_isim like '%' + @search + '%' or sto_kod like '%' + @search + '%') and sto_sat_cari_kod <> '' --and  @urtkod like '%' + sto_sat_cari_kod + '%'

        )
    End
select * from MyTable where row between @skip and @take
end

你也可以检查''......

于 2013-08-14T10:01:53.393 回答
0

更改以下部分

and  @urtkod like '%' + sto_sat_cari_kod + '%'

and     (@urtkod like '%' + sto_sat_cari_kod + '%' OR @urtkod IS NULL)

使用 SQL SERVER 和ISNULL

你可以把它改成

and     (ISNULL(@urtkod,'') like '%' + sto_sat_cari_kod + '%')

也可以看看RDBMS 之间不同版本的SQL NULL 函数

于 2013-08-14T10:02:01.447 回答