database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
ㆍMS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
MS-SQL Q&A 5960 게시물 읽기
No. 5960
질문 하나만 더올릴게요 ㅠㅠ
작성자
송기백(keiback)
작성일
2011-10-08 15:56
조회수
6,425

DECLARE @SQL nvarchar(4000)

 Set @SQL = 'SELECT
    stu.pid as userid
    , stu.name
    , ll.LevelID
    , (SELECT Content FROM CodeMaster WHERE CodeID = ll.step) as Step
    , (SELECT TOP 1 MarkingID FROM Marking WHERE StudentProgressID = sp.ProgressId) as MarkingID
    , (SELECT TOP 1 CreateDate FROM Marking WHERE StudentProgressID = sp.ProgressId) as MarkingDate
    , sp.UpdateDate as UpdateDate
    , (SELECT Content FROM CodeMaster WHERE CodeID = ll.part2) as part2
    , ei.AdminID as AdminID
   FROM Student stu
    INNER JOIN StudentProgress sp ON sp.StudentID = stu.pid
    INNER JOIN LearningLevel ll ON sp.LearningLevelId = ll.LevelId
    LEFT JOIN EditInfo ei ON sp.ProgressID = ei.ProgressID
   WHERE ll.stage = (SELECT CodeID FROM CODEMASTER WHERE Content = ''BLACK'' AND topCode = 1)
     AND sp.ResultID Is Not Null AND sp.type = 1 '

 IF @SearchGubun = 'A' BEGIN
  -- 첨삭 안된것
  IF @EditYN = 'N' BEGIN
   SET @SQL = @SQL + ' AND (SELECT TOP 1 MarkingID FROM Marking WHERE StudentProgressID = sp.ProgressId) IS NULL'
  END
  -- 첨삭된놈
  IF @EditYN = 'E' BEGIN
   SET @SQL = @SQL + ' AND (SELECT TOP 1 MarkingID FROM Marking WHERE StudentProgressID = sp.ProgressId) IS NOT NULL'
  END
 END
 -- 미선택
 ELSE IF @SearchGubun = 'N' BEGIN
  -- 전체
  IF @EditYN = 'A' BEGIN
   SET @SQL = @SQL + ' AND ei.AdminID IS NULL'
  END
  -- 첨삭 안된것
  IF @EditYN = 'N' BEGIN
   SET @SQL = @SQL + ' AND ei.AdminID IS NULL'
   SET @SQL = @SQL + ' AND (SELECT TOP 1 MarkingID FROM Marking WHERE StudentProgressID = sp.ProgressId) IS NULL'
  END
  -- 첨삭된놈
  IF @EditYN = 'E' BEGIN
   SET @SQL = @SQL + ' AND ei.AdminID IS NULL'
   SET @SQL = @SQL + ' AND (SELECT TOP 1 MarkingID FROM Marking WHERE StudentProgressID = sp.ProgressId) IS NOT NULL'
  END
 END
 -- 선생님명 검색
 ELSE BEGIN
  -- 전체
  IF @EditYN = 'A' BEGIN
   SET @SQL = @SQL + ' AND ei.AdminID = @SearchGubun'
  END
  -- 첨삭 안된것
  IF @EditYN = 'N' BEGIN
   SET @SQL = @SQL + ' AND (SELECT TOP 1 MarkingID FROM Marking WHERE StudentProgressID = sp.ProgressId) IS NULL'
  END
  -- 첨삭된놈
  IF @EditYN = 'E' BEGIN
   SET @SQL = @SQL + ' AND ei.AdminID = @SearchGubun'
   SET @SQL = @SQL + ' AND (SELECT TOP 1 MarkingID FROM Marking WHERE StudentProgressID = sp.ProgressId) IS NOT NULL'
  END
 END

 

----------------------------------------------------------------
-- 커서를 사용
----------------------------------------------------------------
-- 결과를입력할임시테이블
Create table #temp(userid int, username nvarchar(50), LevelID nvarchar(max), Step nvarchar(max), MarkingID nvarchar(max), MarkingDate varchar(max), UpdateDate varchar(max),
part2 varchar(max), AdminID nvarchar(50))
 
 
-- 커서
DECLARE @userid int, @username nvarchar(20), @LevelID int,  @Step nvarchar(20), @MarkingID int, @MarkingDate datetime, @UpdateDate datetime, @part2 varchar(50),@AdminID varchar(50)
DECLARE @CNT INT
DECLARE MYCUR CURSOR FOR
  exec (@SQL)
OPEN MYCUR
FETCH NEXT FROM MYCUR INTO @userid,  @username,  @LevelID, @Step, @MarkingID , @MarkingDate , @UpdateDate , @part2 , @AdminID
WHILE (@@FETCH_STATUS=0)
 
BEGIN      
       SET @CNT= ISNULL((SELECT COUNT(*)  FROM #temp WHERE userid=@userid),0)

  
       IF @CNT=0 BEGIN
             INSERT INTO #temp(userid , username , LevelID, Step , MarkingID , MarkingDate, UpdateDate, part2, AdminID )
   values (@userid , @username , @LevelID,  @Step , isnull(@MarkingID, '0') , isnull(@MarkingDate,''), ISNULL(@UpdateDate,''), @part2, ISNULL(@AdminID,'') )
       END
       ELSE
       BEGIN
             UPDATE #temp
             SET LevelID= LevelID+','+convert(varchar,@LevelID) ,
    Step= Step+','+convert(varchar,@Step) ,
   MarkingID= MarkingID+','+convert(varchar,isnull(@MarkingID, '0')) ,
   MarkingDate= MarkingDate+','+convert(varchar,isnull(@MarkingDate,'')),
   UpdateDate = UpdateDate + ',' + Convert(varchar,isnull(@Updatedate,'')),
   part2 = part2 + ',' + Convert(varchar,isnull(@part2,'')),
   AdminID = AdminID + ',' + Convert(varchar,isnull(@AdminID,''))
            WHERE userid=@userid      
  END
 
 
       FETCH NEXT FROM MYCUR INTO @userid,  @username,  @LevelID, @Step, @MarkingID , @MarkingDate , @UpdateDate , @part2 , @AdminID
END
 
 
CLOSE MYCUR
DEALLOCATE MYCUR
 
 
--결과가입력된임시테이블조회
SELECT * FROM #temp

 

 

다른게 아니라 맨위에 변수 @SQL에 조건에 해당하는 쿼리를 넣고

 

아래 DECLARE MYCUR CURSOR FOR 부분에 넣으려고 합니다 근데 에러가 나네요 ㅠ

혹시 exec가 안먹는거같아서 For다음에 IF문을 넣은 쿼리를 넣어봤는데 (문자열로 한것 말고) 안되더군요 ㅠㅠ

이유아시는 분이나 혹시 하는방법이있다면 댓글 부탁 드립니다.ㅠㅠㅠ

[Top]
No.
제목
작성자
작성일
조회
6244Sql Server 2008 백업 문의 입니다. [1]
홍길동
2011-10-19
7789
5962오라클의 v$parameter와 같은 기능 [1]
된동이
2011-10-13
6488
5961group by 사용...? [1]
초보임돠
2011-10-11
6297
5960질문 하나만 더올릴게요 ㅠㅠ
송기백
2011-10-08
6425
5959쿼리 질문입니다 고수님들 도와주세요 [2]
송기백
2011-10-07
7133
5956GROUP BY 관련 질문입니다. [1]
최명근
2011-10-04
6064
5955sql 저장프로시져들만 백업 및 복원방법? [1]
있나없는거같은데
2011-10-01
9993
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.022초, 이곳 서비스는
	PostgreSQL v16.4로 자료를 관리합니다