2017-09-25 100 views
0

爵士,SQL遊標插入到臨時表中,提取從Temparory表

我建立使用光標作爲@AllRecords將值插入臨時表&然後獲取從該臨時表值的SQL查詢。但是當我從表中獲取值時(錯誤:@AllRecords附近的錯誤語法),它在最後一條語句中顯示錯誤。下面是我的代碼:

DECLARE @ColName varchar(20)=null, 
     @Query varchar(MAX)=null, 
     @DepartmentName varchar(50)=null, 
     @deptt_code varchar(4)=null, 
     @DistrictId varchar(4)='0001', 
     @Deptt_Id char(4)=null, 
     @stYear varchar(4)=null, 
     @cYear varchar(4)=null, 
     @yr varchar(9)='2017-2018', 
     @tno int 

BEGIN 
     set @stYear = SUBSTRING(@yr,0,5) 
     set @cYear = SUBSTRING(@yr,6,4) 

--DECLARE & SET COUNTER 
DECLARE @counter int 
SET @counter = 1 

--CREATE DYNAMIC TABLE WITH COLs 
DECLARE @AllRecords table 
(
    department_name varchar(50), 
    project_name varchar(100), 
    department_code varchar(4) 
) 

--*** Declare Cursor 
DECLARE cur_FetchDepartmentName CURSOR READ_ONLY 
FOR 
    select deptt_code,deptt_name+'('+ RTRIM(LTRIM(deptt_short))+')' as dept_name from m_Department 
    where deptt_code in (select distinct department_code from t_Project_Details where [email protected] 
    and [email protected]) 

OPEN cur_FetchDepartmetName 

fetch next from cur_FetchDepartmetName into 
@deptt_code, @DepartmentName 

--LOOP UNTIL RECORDS ARE AVAILABLE 
while @@FETCH_STATUS=0 
    BEGIN 
     if(@tno=0) 
      BEGIN 
       set @tno=1 
       insert into @AllRecords values(@DepartmentName,@deptt_code) 
       fetch next from cur_FetchDepartmetName into 
       @deptt_code,@DepartmentName 
      END 
     else 
      BEGIN 
       set @[email protected]+1 
       insert into @AllRecords values(@DepartmentName,@deptt_code) 
       fetch next from cur_FetchDepartmetName into 
       @deptt_code,@DepartmentName 
      END 

    END 
     --CLOSE CURSOR 
     CLOSE cur_FetchDepartmetName 
     DEALLOCATE cur_FetchDepartmetName 

    select department_name, department_code from @AllRecords 
+1

當你設置你的第一個變量,你必須' BEGIN'沒有關聯的'END' – HoneyBadger

+0

這必須是不必要地使用遊標的最好例子。請停下來,喘口氣,並將其重寫爲一個簡單的INSERT..SELECT語句。我會說要使用ROW_NUMBER()或IDENTITY()來獲得@tNo,但我沒有看到任何意義,你不再使用它們了? –

回答

0

相反的回答是什麼錯誤在這個解決方案,我想提供一個更好的解決問題的辦法。在這個例子中使用遊標是完全不必要的,查詢可以更容易地在沒有它的情況下編寫。這是一個簡單的INSERT..SELECT語句,最後可以很容易地完成記錄設置@tno的計數。

BEGIN 

set @stYear = SUBSTRING(@yr,0,5); 
set @cYear = SUBSTRING(@yr,6,4); 

--CREATE DYNAMIC TABLE WITH COLs 
DECLARE @AllRecords table 
(
    department_name varchar(50), 
    project_name varchar(100), --what's the use of this column? 
    department_code varchar(4) 
); 

INSERT INTO @AllRecords (department_code, department_name) 
select deptt_code,deptt_name+'('+ RTRIM(LTRIM(deptt_short))+')' as dept_name from m_Department 
    where deptt_code in (select distinct department_code from t_Project_Details where [email protected] 
    and [email protected]); 


SELECT @tNo = COALESCE(@tno,0) + COUNT(*) FROM @AllRecords; 

select department_name, department_code from @AllRecords; 

END 

請查看這篇關於光標和如何避免它們:

Cursors and How to Avoid Them

+0

先生,'project_name varchar(100)'這個列是用於另一個項目表,它具有項目詳細信息並且也在相應的部門下上傳。 –

+0

先生其實我必須取得每個部門,並從項目表中取回對應於該部門的項目 –

+0

@SumeetKumar無論問題是什麼,遊標都可能不需要 –

0

您的SQL查詢如下:

BEGIN 
     DECLARE @ColName VARCHAR(20)= NULL, @Query VARCHAR(MAX)= NULL, @DepartmentName VARCHAR(50)= NULL, @deptt_code VARCHAR(4)= NULL, @DistrictId VARCHAR(4)= '0001', @Deptt_Id CHAR(4)= NULL, @stYear VARCHAR(4)= NULL, @cYear VARCHAR(4)= NULL, @yr VARCHAR(9)= '2017-2018', @tno INT; 
     SET @stYear = SUBSTRING(@yr, 0, 5); 
     SET @cYear = SUBSTRING(@yr, 6, 4); 

--DECLARE & SET COUNTER 
     DECLARE @counter INT; 
     SET @counter = 1; 

--CREATE DYNAMIC TABLE WITH COLs 
     DECLARE @AllRecords TABLE 
     (department_name VARCHAR(50), 
      project_name VARCHAR(100), 
      department_code VARCHAR(4) 
     ); 

--*** Declare Cursor 
     DECLARE cur_FetchDepartmentName CURSOR READ_ONLY 
     FOR 
      SELECT deptt_code, 
        deptt_name+'('+RTRIM(LTRIM(deptt_short))+')' AS dept_name 
      FROM m_Department 
      WHERE deptt_code IN 
      (
       SELECT DISTINCT 
         department_code 
       FROM t_Project_Details 
       WHERE district_id = @DistrictId 
         AND financial_year = @yr 
      ); 
     OPEN cur_FetchDepartmetName; 
     FETCH NEXT FROM cur_FetchDepartmetName INTO @deptt_code, @DepartmentName; 

--LOOP UNTIL RECORDS ARE AVAILABLE 
     WHILE @@FETCH_STATUS = 0 
      BEGIN 
       IF(@tno = 0) 
        BEGIN 
         SET @tno = 1; 
         INSERT INTO @AllRecords 
         (department_name, 
          department_code 
         ) 
           SELECT @DepartmentName, 
             @deptt_code; 
         FETCH NEXT FROM cur_FetchDepartmetName INTO @deptt_code, @DepartmentName; 
       END; 
        ELSE 
        BEGIN 
         SET @tno = @tno + 1; 
         INSERT INTO @AllRecords 
         (department_name, 
          department_code 
         ) 
           SELECT @DepartmentName, 
             @deptt_code; 
         FETCH NEXT FROM cur_FetchDepartmetName INTO @deptt_code, @DepartmentName; 
       END; 
      END; 
     --CLOSE CURSOR 
     CLOSE cur_FetchDepartmetName; 
     DEALLOCATE cur_FetchDepartmetName; 

     select department_name, department_code from @AllRecords 
    END;