2013-08-28 114 views
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 

回答

1

當您連接一個null爲一個字符串,其結果是null

我在SQL syntax中看到了太多的錯誤,我正在猜測這個更正。例如,如果@value(n) is not null其他2 @value(n)之一必須是NULL

我強烈建議你看看@sql值,然後再糾正錯誤。

建議的方式來解決這個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 

       DECLARE @Ks_RegisterDate_Condition NVARCHAR(1000) 

       IF (@value1 IS NULL AND @value2 IS NULL AND @value3 IS NULL) 
       BEGIN 
        SET @Ks_RegisterDate_Condition = N'KsRegisterDate IS NULL' 
       END 
       ELSE 
       BEGIN 
        IF (@value1 IS NOT NULL) 
        BEGIN 
         SET @Ks_RegisterDate_Condition = N'(Ks.RegisterDate ='''[email protected]+'''' 
        END 
        IF (@value2 IS NOT NULL) 
        BEGIN 
         SET @Ks_RegisterDate_Condition = N'(Ks.RegisterDate >'''[email protected]+'''' 
        END 
        IF (@value3 IS NOT NULL) 
        BEGIN 
         SET @Ks_RegisterDate_Condition = N'(Ks.RegisterDate >'''[email protected]+'''' 
        END 
       END 

       IF (NOT (@field0 IS NULL OR @value0 IS NULL)) 
       BEGIN 
        SET @Ks_RegisterDate_Condition += + N'AND ' + @field0+N' LIKE ''%'[email protected]+N'%''' 
       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 ' + 
      @Ks_RegisterDate_Condition + ' 
     ) 
     SELECT * FROM OrderedSet WHERE [Index] BETWEEN ' + 
CONVERT(NVARCHAR(12), @offset) + ' AND ' + CONVERT(NVARCHAR(12), (@offset + @newsize)) 

        print @sql 
        EXECUTE (@sql) 

        SET @totalrow = (SELECT COUNT(*) FROM [dbo].[Ks]) 
+0

,但在這種情況下,@ value1和@值2不是在同一時間爲null,則只能得到一個條件不能同時 – sakir

+1

所以只是更改邏輯以在兩者之間追加'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 +''''' –

+0

我想你應該加上+ sign.SET @Ks_RegisterDate_Condition = + – sakir