0
我的查詢是這樣的,但它不會返回任何內容 @ value0,@ value1,@ value2,@ value3,@ field0 changable字段,它們可能爲空值或值取決於來自窗體的參數。 例如當我把這些值,它什麼也沒有返回
@page = 1,@ size = 20,@ sort = N'Id',@ ts = N'DESC',@ field0 = N'Name',@ value2 = N'2000-1-1',@ value3 = null,@ value1 = null,@ value0 = null 我做錯了什麼?分頁查詢沒有任何返回
@page INT,
@size INT,@sort nvarchar(50) ,
@ts nvarchar(50) ,
@totalrow INT OUTPUT,
@value0 nvarchar(50),
@value1 nvarchar(50),
@value2 nvarchar(50),
@value3 nvarchar(50),
@field0 nvarchar(50)
AS
BEGIN
DECLARE @offset INT
DECLARE @newsize INT
DECLARE @sql NVARCHAR(MAX)
IF(@page=0)
begin
SET @offset = @page;
SET @newsize = @size
end
ELSE
begin
SET @offset = @page+1;
SET @newsize = @size-1
end
SET NOCOUNT ON;
SET @sql = '
WITH OrderedSet AS
(
SELECT Ks.Id,c.Name,c.CId,Ks.RegisterDate,ROW_NUMBER()
OVER (ORDER BY ' + @sort + ' '[email protected]+') AS ''Index''
FROM Ks LEFT OUTER JOIN Car c On c.CId=Ks.CId WHERE
AND (Ks.RegisterDate >'''[email protected]+''' OR '''[email protected]+''' IS NULL)
AND (Ks.RegisterDate <'''[email protected]+''' OR '''[email protected]+''' IS NULL)
AND (Ks.RegisterDate ='''[email protected]+''' OR '''[email protected]+''' IS NULL)
AND ('[email protected]+' LIKE ''%'[email protected]+'%'' OR @value0 IS NULL)
)
SELECT * FROM OrderedSet WHERE [Index] BETWEEN ' +
CONVERT(NVARCHAR(12), @offset) + ' AND ' + CONVERT(NVARCHAR(12), (@offset + @newsize))
EXECUTE (@sql)
SET @totalrow = (SELECT COUNT(*) FROM [dbo].[Ks])
print @sql
,但在這種情況下,@ value1和@值2不是在同一時間爲null,則只能得到一個條件不能同時 – sakir
所以只是更改邏輯以在兩者之間追加'AND'(或'OR')條件並附加條件,例如'... DECLARE @Ks_RegisterDate_Condition NVARCHAR(1000);設置@Ks_RegisterDate_Condition =''...'然後,當@Ks_RegisterDate_Condition <>''時,if變成SET @Ks_RegisterDate_Condition =''然後N'和'else''結束+ N'(Ks.RegisterDate>'''+ @ value2 +''''' –
我想你應該加上+ sign.SET @Ks_RegisterDate_Condition = + – sakir