2016-12-30 148 views
0

我得到了一些錯誤,如:性能調優

消息205,級別16,狀態1,過程SearchUserswithLevels,第29個
所有查詢使用UNION合併,交叉或EXCEPT操作者必須在目標列表中有相同數量的表達式。

我附上我的表也是存儲過程

CREATE TABLE [dbo].[Users] 
(
    [Login_ID] [bigint] IDENTITY(1,1) NOT NULL, 
    [Login_Name] [nvarchar](100) NOT NULL, 
    [Login_Password] [nvarchar](100) NULL, 
    [CreatedBy] [bigint] NULL, 
    [ModifiedBy] [bigint] NULL, 
    [Referal_ID] [bigint] NULL, 
    [Name] [nvarchar](250) NULL, 
    [User_Address] [nvarchar](max) NULL, 
    [Phone] [nvarchar](14) NULL, 
    [Email] [nvarchar](250) NULL, 
    [BankName] [nvarchar](250) NULL, 
    [AccountName] [nvarchar](250) NULL, 
    [IFSC] [nvarchar](250) NULL, 
    [AccountNo] [nvarchar](250) NULL, 
    [DOB] [datetime] NULL, 
    [Created_Date] [datetime] NULL, 
    [Modified_Date] [datetime] NULL, 
    [Last_Login_Date_Time] [datetime] NULL, 
    [UnsuscribeEmail] [int] NULL, 
    [UnsuscribeSms] [int] NULL, 
    [IsBanned] [int] NULL, 
    [ISDeleted] [int] NULL, 
    [Roles] [nvarchar](max) NULL, 
    [IDPROF] [nvarchar](250) NULL, 
    [IDPROFNO] [nvarchar](250) NULL, 
    [PANCARDNO] [nvarchar](250) NULL 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 

我的存儲過程是這樣的:

CREATE PROCEDURE SearchUserswithLevels 
    (@SearchParameter NVARCHAR(250), 
     @Referal_ID BIGINT = NULL) 
AS 
BEGIN 
    DECLARE @CURRENTDATE AS DATETIME = GETDATE() 

    CREATE TABLE #temptable 
    (
     ID BIGINT, 
     Login_Name NVARCHAR(100) NOT NULL, 
     NAME NVARCHAR(250) NULL, 
     Levels INT, 
     Referal_ID BIGINT NULL, 
     User_Address NVARCHAR(max) NULL, 
     Phone NVARCHAR(14) NULL, 
     Email NVARCHAR(250) NULL, 
     BankName NVARCHAR(250) NULL, 
     AccountName NVARCHAR(250) NULL, 
     IFSC NVARCHAR(250) NULL, 
     AccountNo NVARCHAR(250) NULL, 
     DOB DATETIME NULL, 
     IDPROF NVARCHAR(250) NULL, 
     IDPROFNO NVARCHAR(250) NULL, 
     PANCARDNO NVARCHAR(250) NULL 
    ) 

    ;WITH Hierarchy (
     Login_ID, 
     Login_Name, 
     NAME, 
     Levels, 
     Referal_ID, 
     User_Address, 
     Phone, 
     Email, 
     BankName, 
     AccountName, 
     IFSC, 
     AccountNo, 
     DOB, 
     IDPROF, 
     IDPROFNO, 
     PANCARDNO 
    ) AS 
    (
     SELECT 
      Login_ID, 
      Login_Name, 
      NAME, 
      0, 
      Referal_ID, 
      User_Address, 
      Phone, 
      Email, 
      BankName, 
      AccountName, 
      IFSC, 
      AccountNo, 
      DOB, 
      IDPROF, 
      IDPROFNO, 
      PANCARDNO 
     FROM 
      Users AS FirtGeneration 
     WHERE 
      Referal_ID = @Referal_ID 

     UNION ALL 

     SELECT 
      NextGeneration.Login_ID, 
      NextGeneration.Login_Name, 
      NextGeneration.NAME, 
      Parent.Levels + 1, 
      Parent.Login_ID 
      User_Address, 
      NextGeneration.Phone, 
      NextGeneration.Email, 
      NextGeneration.BankName, 
      NextGeneration.AccountName, 
      NextGeneration.IFSC, 
      NextGeneration.AccountNo, 
      NextGeneration.DOB, 
      NextGeneration.IDPROF, 
      NextGeneration.IDPROFNO, 
      NextGeneration.PANCARDNO 
     FROM 
      Users AS NextGeneration 
     INNER JOIN 
      Hierarchy AS Parent ON NextGeneration.Referal_ID = Parent.Login_ID 
    ) 
    INSERT INTO #temptable 
     SELECT 
      Login_ID, Login_Name, NAME, 
      (Levels + 1) AS Levels, 
      Referal_ID, 
      User_Address, 
      Phone, Email, BankName, AccountName, 
      IFSC, AccountNo, DOB, IDPROF, IDPROFNO, 
      PANCARDNO 
     FROM 
      Hierarchy 
     --WHERE Levels = @Levels 
     ORDER BY 
      Login_ID 
     OPTION (MAXRECURSION 32767) 

    SELECT 
     TP.ID, TP.Login_Name, TP.NAME, 
     TP.Levels, TP.Referal_ID, TP.User_Address, 
     TP.Phone, TP.Email, TP.BankName, TP.AccountName, TP.IFSC, 
     TP.AccountNo, TP.DOB, TP.IDPROF, TP.IDPROFNO 
    FROM 
     #temptable as TP 
    WHERE 
     (TP.Email LIKE '%' + @SearchParameter + '%') 
     OR (TP.Phone LIKE '%' + @SearchParameter + '%') 
     OR (TP.NAME LIKE '%' + @SearchParameter + '%') 
     OR (TP.ID LIKE '%' + @SearchParameter + '%') 
END 
+2

你的問題標題是關於性能,以及您標記的性能以及。如果您遇到錯誤,性能不是問題! – HoneyBadger

+0

是的,低估了這個問題。誤導性的標題和錯字容易找到。這是垃圾問題。 – cybernetic87

回答

5

UNION ALL有這樣的:

Parent.Login_ID 
User_Address, 

這由Sql Se解釋rver as

Parent.Login_ID AS User_Address, 

這使得它成爲1字段,而不是你想要的2字段。添加一個逗號,你就完成了。

0

你缺少TP.PANCARDNO#temptable

INSERT INTO #temptable 
    SELECT Login_ID, 
     Login_Name, 
     NAME, 
     (Levels + 1) AS Levels, 
     Referal_ID, 
     User_Address, 
     Phone, 
     Email, 
     BankName, 
     AccountName, 
     IFSC, 
     AccountNo, 
     DOB, 
     IDPROF, 
     IDPROFNO, 
     PANCARDNO FROM Hierarchy 
    --WHERE Levels = @Levels 
    ORDER BY Login_ID 
    OPTION (MAXRECURSION 32767) 

    SELECT TP.ID, 
     TP.Login_Name, 
     TP.NAME, 
     TP.Levels, 
     TP.Referal_ID, 
     TP.User_Address, 
     TP.Phone, 
     TP.Email, 
     TP.BankName, 
     TP.AccountName, 
     TP.IFSC, 
     TP.AccountNo, 
     TP.DOB, 
     TP.IDPROF, 
     TP.IDPROFNO, 
     TP.PANCARDNO 
    FROM #temptable as TP