列名称“#Results”无效

时间:2015-03-14 07:53:14

标签: sql-server

alter PROCEDURE sp_Get_CustInfoSerach2    
      (@PageIndex INT = 1
      ,@PageSize INT = 10
      ,@RecordCount INT OUTPUT
      ,@ColumnName VARCHAR(50)=null
      ,@Value VARCHAR(50)=null
      ,@ddlValue VARCHAR(50)=null
      ,@txtValue VARCHAR(50)=null
      ,@status varchar(30))
AS
BEGIN
      SET NOCOUNT ON;
      DECLARE @cmd AS NVARCHAR(max)
      if @txtValue IS NULL  
      Begin
        SET @Value = ''''+@ddlValue+ ''''
        End                
        else if @ddlValue IS NULL  
        begin
        SET @Value = ''''+@txtValue+ ''''
        end
      SET @cmd = 'SELECT ROW_NUMBER() OVER
      (
            ORDER BY C_Register.UserId desc 
      )AS RowNumber

      ,C_Register.UserId, C_Register.Name, C_Register.UserName, C_Register.Status, 
                    Packages.PackagePeriod, Packages.PackageName, C_Register.ActivationDate, 
                      Receive_Payment.OldExpiryDate, Receive_Payment.Balance, Receive_Payment.PyingAmount, 
                      Receive_Payment.LastPaidDate, C_Register.LastUpdateTime, 
                      Area.AreaName, C_Register.MobNo, Employee.EmpName, C_Register.Address,C_Register.CreatedDate

     INTO'+ #Results+'
      FROM C_Register INNER JOIN Receive_Payment ON C_Register.UserId = Receive_Payment.UserId
                       INNER JOIN Area ON C_Register.AreaId = Area.AreaId
                       INNER JOIN Employee ON Receive_Payment.EmpId = Employee.EmpId
                       INNER JOIN Packages ON Receive_Payment.PackageId = Packages.PackageId
     where      C_Register.AccountExpiry= Receive_Payment.OldExpiryDate And C_Register.Status = '+@status+'And
     ' +  @ColumnName + ' = ' + @Value 

      SELECT @RecordCount = COUNT(*)
      FROM #Results

      SELECT * FROM #Results
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1

      DROP TABLE #Results
      EXEC(@cmd)
END

抛出错误:

  

列名称'#Results'无效。

如何解决?

2 个答案:

答案 0 :(得分:0)

如果我正确理解你的问题,你应该在从临时表中选择之前先执行动态sql

Alter PROCEDURE sp_Get_CustInfoSerach2
(
@PageIndex INT = 1 ,
@PageSize INT = 10 ,
@RecordCount INT OUTPUT ,
@ColumnName VARCHAR(50)=null ,
@Value VARCHAR(50)=null ,
@ddlValue VARCHAR(50)=null ,
@txtValue VARCHAR(50)=null ,
@status varchar(30)
) 
AS 
BEGIN 
SET NOCOUNT ON; 
DECLARE @cmd AS NVARCHAR(max)

If @txtValue IS NULL
Begin 
SET @Value = ''''+@ddlValue+ '''' 
End
Else 
if @ddlValue IS NULL
Begin 
SET @Value = ''''+@txtValue+ '''' 
End 
SET @cmd = 'SELECT ROW_NUMBER() OVER ( ORDER BY C_Register.UserId desc )AS RowNumber,
  C_Register.UserId, C_Register.Name, C_Register.UserName, C_Register.Status, 
                Packages.PackagePeriod, Packages.PackageName, C_Register.ActivationDate, 
                  Receive_Payment.OldExpiryDate, Receive_Payment.Balance, Receive_Payment.PyingAmount, 
                  Receive_Payment.LastPaidDate, C_Register.LastUpdateTime, 
                  Area.AreaName, C_Register.MobNo, Employee.EmpName, C_Register.Address,C_Register.CreatedDate

 INTO #Results
  FROM C_Register INNER JOIN Receive_Payment ON C_Register.UserId = Receive_Payment.UserId
                   INNER JOIN Area ON C_Register.AreaId = Area.AreaId
                   INNER JOIN Employee ON Receive_Payment.EmpId = Employee.EmpId
                   INNER JOIN Packages ON Receive_Payment.PackageId = Packages.PackageId
 where      C_Register.AccountExpiry= Receive_Payment.OldExpiryDate And C_Register.Status = '+@status+'And
 ' +  @ColumnName + ' = ' + @Value 

  /*First Execute above dynamic Sql query */
  EXEC(@cmd)
 /* From the above execute statemnet the query will executed and temporary table will created on the fly */

  SELECT @RecordCount = COUNT(*)
  FROM #Results


  SELECT * FROM #Results
  WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1

  DROP TABLE #Results

答案 1 :(得分:0)

您的查询中的问题是创建临时表,这不是在dynamic query内创建临时表的正确方法。

但即使您在temp table内创建dynamic query,也只能accessed session dynamic query。如果您尝试在动态查询中创建的动态查询之外选择临时值,则会收到错误消息,指出对象不存在。

由于您知道动态查询结果中存在的列数,因此您可以在动态查询之外创建temp table,并通过动态查询创建insert记录。

尝试更改您的程序。

ALTER PROCEDURE Sp_get_custinfoserach2 (@PageIndex   INT = 1, 
                                        @PageSize    INT = 10, 
                                        @RecordCount INT output, 
                                        @ColumnName  VARCHAR(50)=NULL, 
                                        @Value       VARCHAR(50)=NULL, 
                                        @ddlValue    VARCHAR(50)=NULL, 
                                        @txtValue    VARCHAR(50)=NULL, 
                                        @status      VARCHAR(30)) 
AS 
  BEGIN 
      SET nocount ON; 

      DECLARE @cmd AS NVARCHAR(max) 

      IF @txtValue IS NULL 
        BEGIN 
            SET @Value = '''' + @ddlValue + '''' 
        END 
      ELSE IF @ddlValue IS NULL 
        BEGIN 
            SET @Value = '''' + @txtValue + '''' 
        END 
/*create a temp as same structure of your dynamic query select statement*/
      CREATE TABLE #result 
        ( 
           rownum         INT, 
           userid         INT, 
           NAME           VARCHAR(100), 
           username       VARCHAR(100), 
           status         VARCHAR(15), 
           packageperiod  VARCHAR(15), 
           packagename    VARCHAR(100), 
           activationdate DATETIME, 
           oldexpirydate  DATETIME, 
           balance        NUMERIC(22, 4), 
           pyingamount    NUMERIC(22, 4), 
           lastpaiddate   DATETIME, 
           lastupdatetime DATETIME, 
           areaname       VARCHAR(100), 
           mobno          INT, 
           empname        VARCHAR(100), 
           address        VARCHAR(5000), 
           createddate    DATETIME 
        ) 

      SET @cmd = 
       ' Insert into #result 
         SELECT ROW_NUMBER() OVER (ORDER BY C_Register.UserId desc )AS RowNumber,
         C_Register.UserId, C_Register.Name, C_Register.UserName,   
        C_Register.Status, Packages.PackagePeriod, Packages.PackageName, 
        C_Register.ActivationDate,Receive_Payment.OldExpiryDate, 
        Receive_Payment.Balance, Receive_Payment.PyingAmount, 
        Receive_Payment.LastPaidDate, C_Register.LastUpdateTime, 
        Area.AreaName, C_Register.MobNo, Employee.EmpName,
        C_Register.Address,C_Register.CreatedDate       
        FROM C_Register 
        INNER JOIN Receive_Payment 
                ON C_Register.UserId = Receive_Payment.UserId
        INNER JOIN Area 
                ON C_Register.AreaId = Area.AreaId
        INNER JOIN Employee 
                ON Receive_Payment.EmpId = Employee.EmpId
        INNER JOIN Packages 
                ON Receive_Payment.PackageId = Packages.PackageId
         where      C_Register.AccountExpiry= Receive_Payment.OldExpiryDate And C_Register.Status = ' + @status + ' And ' + @ColumnName + ' = ' + @Value 

    SELECT @RecordCount = Count(*) 
    FROM   #results 

    SELECT * 
    FROM   #results 
    WHERE  rownumber BETWEEN( @PageIndex - 1 ) * @PageSize + 1 AND( ( 
                            ( @PageIndex - 1 ) * @PageSize + 1 ) + 
                                 @PageSize ) - 1 

    DROP TABLE #results 

    EXEC(@cmd) 
END 

注意:我已将通用datatypes提供给temp表格列,请根据您的datetypes更改schema