您好我已经开发了一个存储过程,我在其中声明了一个名为@TEMP_TABLE的表变量,但对于此行
SET @SQL1 = 'UPDATE PO_STAGE set PO_STAGE.ISVALID = ''N''
FROM ERP_ADMIN.'+@STAGE_TABLE+' PO_STAGE, @TEMP_TABLE TMP
where PO_STAGE.ERP_PO_ID = TMP.ERP_POID'
exec (@SQL1)
我收到“必须声明表变量”@TEMP_TABLE“。”错误。 请在这方面帮助我。下面是存储过程的代码。
CREATE PROCEDURE [ERP_ADMIN].[VALIDATE_PURCHASE_ORDER]
@STAGE_TABLE nvarchar(50)
-- @errorProperty nvarchar(50) output,
-- @errorValue nvarchar(120) output
AS
BEGIN
SET NOCOUNT ON;
DECLARE @rule INTEGER
DECLARE @approvalcheckingrecord VARCHAR(200)
DECLARE @APP_VALIDFROMDATE DATETIME,
@APP_VALIDTODATE DATETIME,
@numUpdated INTEGER,
@numInserted INTEGER,
@systodate VARCHAR(160),
@SQL1 [nvarchar](1200),
@SQL2 [nvarchar](1200),
@SOUCOL [nvarchar](200)
-- SELECT @systodate = CONVERT(VARCHAR(23), ERP_ADMIN.GETCURRENTDATE(), 110) +' '+ CONVERT(VARCHAR(23), ERP_ADMIN.GETCURRENTDATE(), 108)
--SELECT @systodate = CONVERT(VARCHAR(23), GETDATE(), 110) +' '+ CONVERT(VARCHAR(23), GETDATE(), 108)
SET @SQL1 = ''
SET @SQL1 = 'SELECT ERP_SOURCE_COLUMN
FROM [ERP_ADMIN].[ERP_STAGE_PROP_MAPPING] where [ERP_SOURCE_TABLE] = '''+@STAGE_TABLE+''' and [ERP_REPORT_COLOMNS] = ''Y'''
-- PRINT @SQL1
SET @SQL2 = 'DECLARE rpt_cursor CURSOR GLOBAL FOR ' + @SQL1
exec (@SQL2)
DECLARE @SOURCE_COLOMN nvarchar(80)
SET @SOUCOL = ''
OPEN rpt_cursor
WHILE (0 = 0)
--begin of while
BEGIN
FETCH NEXT FROM rpt_cursor INTO @SOURCE_COLOMN
IF (@@FETCH_STATUS = -1)
BREAK
IF (@SOUCOL = '')
SET @SOUCOL = 'convert(nvarchar(255),ISNULL('+@SOURCE_COLOMN+',''''))+'',''+'
ELSE
SET @SOUCOL = @SOUCOL + 'convert(nvarchar(255),ISNULL('+@SOURCE_COLOMN+',''''))+'',''+'
END
CLOSE rpt_cursor
DEALLOCATE rpt_cursor
PRINT @SOUCOL
DECLARE @TEMP_TABLE TABLE (
ERP_POID nvarchar(50),
ERRORCD nvarchar(50),
ERRORMSG nvarchar(120)
)
SET @SQL1 = ''
SET @SQL1 = 'SELECT ERP_SOURCE_COLUMN
FROM [ERP_ADMIN].[ERP_STAGE_PROP_MAPPING] where [ERP_SOURCE_TABLE] = '''+@STAGE_TABLE+''' and [ERP_REPORT_COLOMNS] = ''Y'''
SET @SQL1 = 'SELECT [ERP_SOURCE_TABLE]
,[ERP_SOURCE_COLUMN]
,[ERP_MANDATORY_CHECK]
,[ERP_DATA_CHECK]
,[ERP_ERROR_PROPERTY1]
,[ERP_ERROR_VALUE1]
,[ERP_ERROR_PROPERTY2]
,[ERP_ERROR_VALUE2]
FROM [ERP_ADMIN].[ERP_STAGE_PROP_MAPPING] where [ERP_SOURCE_TABLE] = '''+@STAGE_TABLE+''' and ([ERP_MANDATORY_CHECK] IN (''K'',''Y'') or [ERP_DATA_CHECK] IN (''D'',''N''))'
PRINT @SQL1
SET @SQL2 = 'DECLARE stage_cursor CURSOR GLOBAL FOR ' + @SQL1
/** DECLARE stage_cursor CURSOR LOCAL FOR
/****** Script for SelectTopNRows command from SSMS ******/
SELECT [ERP_SOURCE_TABLE]
,[ERP_SOURCE_COLUMN]
,[ERP_MANDATORY_CHECK]
,[ERP_DATA_CHECK]
,[ERP_ERROR_PROPERTY1]
,[ERP_ERROR_VALUE1]
,[ERP_ERROR_PROPERTY2]
,[ERP_ERROR_VALUE2]
FROM [ERP_ADMIN].[ERP_STAGE_PROP_MAPPING] where [ERP_SOURCE_TABLE] = '+@STAGE_TABLE+' and ([ERP_MANDATORY_CHECK] IN ('K','Y') or [ERP_DATA_CHECK] IN ('D','N')) **/
exec(@SQL2)
DECLARE @CUR_SOURCE_TABLE [nvarchar](120),
@CUR_SOURCE_COLUMN [nvarchar](120),
@CUR_MANDATORY_CHECK [nvarchar](20),
@CUR_DATA_CHECK [nvarchar](20),
@CUR_ERROR_PROPERTY1 [nvarchar](50),
@CUR_ERROR_VALUE1 [nvarchar](120),
@CUR_ERROR_PROPERTY2 [nvarchar](50),
@CUR_ERROR_VALUE2 [nvarchar](120)
OPEN stage_cursor
WHILE (0 = 0)
--begin of while
BEGIN
FETCH NEXT FROM stage_cursor INTO
@CUR_SOURCE_TABLE,
@CUR_SOURCE_COLUMN,
@CUR_MANDATORY_CHECK,
@CUR_DATA_CHECK,
@CUR_ERROR_PROPERTY1,
@CUR_ERROR_VALUE1,
@CUR_ERROR_PROPERTY2,
@CUR_ERROR_VALUE2
IF (@@FETCH_STATUS = -1)
BREAK
SET @SQL1 = ''
SET @SQL2 = ''
PRINT @CUR_SOURCE_COLUMN
-- IF(@CUR_SOURCE_COLUMN = 'ERP_PO_DOCNUMBER')
IF (@CUR_MANDATORY_CHECK = 'Y' OR @CUR_MANDATORY_CHECK = 'K')
BEGIN
-- SET @SQL1 = 'SELECT ''aaa'' as column1, ERP_PO_DOCNUMBER as column2
-- from PURCHASE_ORDER_STAGE where '+ @CUR_SOURCE_COLUMN +' IS NULL'
SET @SQL1 = 'SELECT ERP_PO_ID, '''+@CUR_ERROR_PROPERTY1+''' as column1,'+@SOUCOL+'''|1| ERR_01 '''''+@CUR_ERROR_VALUE1+'''+'' or null in input Worksheet'' as column2
from [ERP_ADMIN].'+@STAGE_TABLE+' where '+ @CUR_SOURCE_COLUMN +' IS NULL'
PRINT @SQL1
INSERT INTO @TEMP_TABLE
exec (@SQL1)
END
IF (LTRIM(RTRIM(@CUR_DATA_CHECK)) = 'D')
BEGIN
-- SET @SQL1 = 'SELECT ''aaa'' as column1, ERP_PO_DOCNUMBER as column2
-- from PURCHASE_ORDER_STAGE where '+ @CUR_SOURCE_COLUMN +' IS NULL'
SET @SQL1 = 'SELECT ERP_PO_ID, '''+@CUR_ERROR_PROPERTY2+''' as column1,'+@SOUCOL+'''|1| ERR_01 '''''+@CUR_ERROR_VALUE2+'''+'' in the input Worksheet'' as column2
from [ERP_ADMIN].'+@STAGE_TABLE+' where isdate('+ @CUR_SOURCE_COLUMN +') =0'
PRINT @SQL1
INSERT INTO @TEMP_TABLE
exec (@SQL1)
END
IF (LTRIM(RTRIM(@CUR_DATA_CHECK)) = 'N')
BEGIN
PRINT @CUR_SOURCE_COLUMN
SET @SQL1 = 'SELECT ERP_PO_ID, '''+@CUR_ERROR_PROPERTY2+''' as column1,'+@SOUCOL+'''|1| ERR_01 '''''+@CUR_ERROR_VALUE2+'''+'' in the input Worksheet'' as column2
from [ERP_ADMIN].'+@STAGE_TABLE+' where isnumeric('+ @CUR_SOURCE_COLUMN +') =0'
PRINT @SQL1
INSERT INTO @TEMP_TABLE
exec (@SQL1)
END
END
CLOSE stage_cursor
DEALLOCATE stage_cursor
SET @SQL1 = ''
SET @SQL1 = 'UPDATE PO_STAGE set PO_STAGE.ISVALID = ''N'' FROM ERP_ADMIN.'+@STAGE_TABLE+' PO_STAGE, @TEMP_TABLE TMP where PO_STAGE.ERP_PO_ID = TMP.ERP_POID'
PRINT @SQL1
exec (@SQL1)
/** DECLARE update_stage_cursor CURSOR LOCAL FOR
SELECT ERP_POID FROM @TEMP_TABLE
DECLARE @CURPOID [nvarchar](50)
OPEN update_stage_cursor
WHILE (0 = 0)
--begin of while
BEGIN
FETCH NEXT FROM update_stage_cursor INTO
@CURPOID
IF (@@FETCH_STATUS = -1)
BREAK
END
CLOSE update_stage_cursor
DEALLOCATE update_stage_cursor **/
-- SELECT A.ERP_PO_ID FROM ERP_ADMIN.PURCHASE_ORDER_STAGE A, @TEMP_TABLE B where ERP_PO_ID = B.ERP_POID
SELECT ERRORCD, ERRORMSG FROM @TEMP_TABLE
END
GO
答案 0 :(得分:1)
您应该将@TEMP_TABLE作为参数传递。要传递 Table 参数,您首先应创建表类型:
IF NOT EXISTS (SELECT 1 FROM sys.types WHERE NAME = 'TEMP_TABLE_TYPE')
CREATE TYPE TEMP_TABLE_TYPE AS TABLE(
ERP_POID nvarchar(50),
ERRORCD nvarchar(50),
ERRORMSG nvarchar(120)
);
并使用它:
exec sp_executesql @Sql, N'@TEMP_TABLE TEMP_TABLE_TYPE readonly', @TEMP_TABLE
对于epxplonation,您可以使用此链接Passing table variable into dynamic sql。
如果您发现这种方式不明显,我建议使用临时表而不是表变量:
CREATE TABLE #TEMP_TABLE (
ERP_POID nvarchar(50),
ERRORCD nvarchar(50),
ERRORMSG nvarchar(120)
);