我正在尝试一个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为null时我想要所有表(它意味着“和sto_sat_cari_kod&lt;&gt;''和@urtkod像'%'+ sto_sat_cari_kod +'%'”将忽略),我研究“案件何时”,但它不起作用或我做错了。
答案 0 :(得分: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
您也可以查看''
....
答案 1 :(得分: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 Functions