Click here to Skip to main content
14,665,492 members
Rate this:
Please Sign up or sign in to vote.
Hi
I have in issue in update @temp table column from another table column value.
Once run Stored procedure, below is error

" Msg 137, Level 16, State 1, Procedure usp_Report_Statement, Line 43 [Batch Start Line 7] Must declare the scalar variable "@Statement".

Pls advice me
Thank you in advance
Maideen

What I have tried:

GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[usp_Report_Statement]
	@Action VARCHAR(50)=NULL,@sid varchar(25) = NULL,@nric varchar(30) = NULL,@NAME VARCHAR(100) NULL
AS
BEGIN

SET NOCOUNT ON;
   DECLARE @Statement TABLE
    (
		[id] [bigint] IDENTITY(1,1) NOT NULL,
		[RCNO] VARCHAR(10) NULL,
		[RCDATE] DATE NULL,
		[SID] [varchar](25) NULL,
		[NAME] [varchar](50) NULL,
		[NRIC] [varchar](25) NULL,
		[COURSECODE] [varchar](20) NULL,
		[COURSEFEEORI] [varchar](10) NULL,
		[AMOUNT] NUMERIC (18,2) NULL,
		[MODE] VARCHAR(20) NULL,
		[RCVDFOR] VARCHAR(50) NULL,
		[CHQAMT] NUMERIC(18,2) NULL,
		[STATUS] VARCHAR(15) NULL,
		[LOCATION] VARCHAR(10) NULL,
		[TAGID] VARCHAR(5) NULL,
		[INTAKEM] VARCHAR(25) NULL,
		[INTAKEY] VARCHAR(5) NULL

    )
		INSERT INTO @Statement (RCNO,RCDATE,SID,NAME,COURSECODE,AMOUNT,RCVDFOR,STATUS,LOCATION,TAGID) 
		SELECT RCNO,RCDATE,SID,NAME,COURSECODE,AMOUNT,RCVDFOR,STATUS,LOCATION,'A'
		FROM [dbo].[US_ReceiptDetails] where [sid]=@sid AND [NAME] = @NAME 

		INSERT INTO @Statement (RCNO,RCDATE,SID,NAME,COURSECODE,AMOUNT,RCVDFOR,STATUS,LOCATION,TAGID) 
		SELECT RCNO,RCDATE,SID,NAME,COURSECODE,AMOUNT,RCVDFOR,STATUS,LOCATION,'A'
		FROM [dbo].[US_RefundDetails] where [sid]=@sid AND [NAME] = @NAME

		UPDATE @Statement SET INTAKEM = A.INTAKEM, INTAKEY = A.INTAKEY FROM [dbo].[AD_StudentRecord] A
		WHERE @Statement.SID = A.SID

		SELECT * FROM @Statement
	END
Posted
Updated 29-Oct-18 23:59pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You are getting the error on the line
UPDATE @Statement SET INTAKEM = A.INTAKEM, INTAKEY = A.INTAKEY FROM [dbo].[AD_StudentRecord] A
		WHERE @Statement.SID = A.SID

If you want to update a table (actual table, table variable or temporary table) with values from one or more other tables, then you must JOIN the tables. E.g.
UPDATE s SET INTAKEM = A.INTAKEM, INTAKEY = A.INTAKEY FROM [dbo].[AD_StudentRecord] A
INNER JOIN @Statement s ON s.SID = A.SID
Note that I have given @Statement an ALIAS (s) - as far as I know that is necessary when updating via a join (but I'm happy to be corrected if I'm wrong)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100