我遇到了 EXECUTE sp_executesql 的问题并坚持使用它。我的查询如下
DECLARE @TABLE_NAME VARCHAR(5)='#T' DECLARE @TABLE_ID INTEGER=0 DECLARE @QRY NVARCHAR(MAX) ='' DECLARE @TABLE VARCHAR(3) DECLARE CRSR_WAGES CURSOR FOR SELECT Code,Name , ROW_NUMBER() OVER (ORDER BY U_Prrty, DocEntry) Slno, COUNT(Code) OVER (PARTITION BY OBJECT) ColNos FROM [@CCS_WAGE] WHERE U_DISPP = 'Y' AND U_DEDWT='E' OPEN CRSR_WAGES FETCH NEXT FROM CRSR_WAGES INTO @WAGECOD,@WAGE_DESC,@SI_NO,@COL_NOS WHILE @@FETCH_STATUS=0 BEGIN SET @TABLE=@TABLE_NAME+CONVERT(VARCHAR(2),@TABLE_ID) SELECT @TABLE AS 'TABLE' SET @QRY='(SELECT empno, amt AS '+'['+@WAGE_DESC+']' SET @QRY=@QRY+' INTO '+@TABLE SET @QRY=@QRY+' FROM CCS_PYMONTHRESULT WHERE wgcod='''+@WAGECOD+''')' SELECT @QRY EXECUTE sp_executesql @QRY SET @TABLE_ID=@TABLE_ID+1 DECLARE @STAR AS NVARCHAR(50)='SELECT * FROM ' + @TABLE SELECT @STAR EXECUTE sp_executesql @STAR FETCH NEXT FROM CRSR_WAGES INTO @WAGECOD,@WAGE_DESC,@SI_NO,@COL_NOS END CLOSE CRSR_WAGES DEALLOCATE CRSR_WAGES
但问题是当执行此错误消息时,就像这个无效的对象名称'#T0'。无效的对象名称“#T1”。无效的对象名称“#T2”。