2011-07-23 95 views
0

嗨,我是dojng一個存儲過程,其中包含一個遊標...遊標有一個動態的sql查詢..這個查詢有動態值..這是@ industry ..然後我把結果放在一個臨時table..but的問題是,當我執行程序的結果不來...存儲過程中的動態光標

這是我在SQLSERVER stroedproc

ALTER PROCEDURE GETARTISTDETAIL AS 
BEGIN 
    DECLARE @INDUSTRY VARCHAR(40) 
    DECLARE @ID INT 
    DECLARE @SQL1 VARCHAR(1000) 
    DECLARE @SQL VARCHAR(1000) 
    SET @INDUSTRY='''BollyWood'',''TollyWood'',''HollyWood''' 
CREATE TABLE #TEMPTBL([NAME] VARCHAR(20),[AGE] INT ,[MAILID] VARCHAR(20)) 
    --SET @SQL='SELECT ARTISTID FROM ARTIST WHERE INDUSTRY IN ('[email protected]+')' 
    DECLARE TEMPCRS CURSOR LOCAL SCROLL STATIC FOR SELECT ARTISTID FROM ARTIST WHERE INDUSTRY IN (@INDUSTRY) 
    --PRINT @SQL 
    --exec (@SQL) 
    OPEN TEMPCRS 
    FETCH NEXT FROM TEMPCRS INTO @ID 
    WHILE @@FETCH_STATUS=0 
    BEGIN 
    INSERT INTO #TEMPTBL SELECT [NAME],[AGE],[MAILID] FROM ARTIST WHERE [email protected] 
    FETCH NEXT FROM TEMPCRS INTO @ID 
    END 
    CLOSE TEMPCRS 
    DEALLOCATE TEMPCRS 
    SELECT * FROM #TEMPTBL 
    DROP TABLE #TEMPTBL 
END 
+0

爲什麼選擇ea你是否使用光標來做到這一點?除了動態的'IN'語句之外,它們是簡單的SELECT語句 – LittleBobbyTables

回答

2

你不需要光標要做到這一點,只需使用子查詢:

DECLARE @INDUSTRY VARCHAR(40) 
DECLARE @SQL VARCHAR(1000) 

CREATE TABLE #TEMPTBL([NAME] VARCHAR(20),[AGE] INT ,[MAILID] VARCHAR(20)) 

SET @INDUSTRY='''BollyWood'',''TollyWood'',''HollyWood''' 

SET @SQL='INSERT INTO #TEMPTBL SELECT [NAME],[AGE],[MAILID] 
      FROM ARTIST WHERE ARTISTID IN (
      SELECT ARTISTID FROM ARTIST WHERE INDUSTRY IN ('[email protected]+') 
     )' 

EXEC (@SQL) 

SELECT * FROM #TEMPTBL 
DROP TABLE #TEMPTBL