Click here to Skip to main content
16,012,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When try to save this procedure I am getting the error

MUST DECLARE A SCALAR VARIABLE "@code_value"


SQL
ALTER PROCEDURE p_trans
	(
	@date datetime,
	@ref varchar(30)= null,
	@dr_amt money=0,
	@cr_amt money =0,
	@posted_to integer,
	@id char(1),
	@doc_no integer = null,
	@narr varchar(150) = null
	)
AS
	DECLARE @ll_doc integer
	DECLARE @ret integer
	DECLARE @code_value integer
	
	
	/* Get the selected cash/ bank account:
	The user makes a selection from selection.aspx and 
	tbl_section is update with the code_value
	*/
	SELECT @code_value = Selection 
	FROM tbl_selection
	
	BEGIN TRANSACTION
	
	IF isnull(@doc_no,0)=0
	--- INSERT---
	
	BEGIN
	--- Safe Guard: Check if transaction with same ref# exists. If so do not insert -----
	
		SELECT @ret=count(*) 
		FROM tr_header 
		WHERE ref=@ref
		
		IF @ret>0
		BEGIN
		--- raise error(5300,1,16)
			GOTO doerror
		END
		
		SELECT @ll_doc=isnull(max(doc_no),0)+1 
		FROM tr_header
		
		IF @@ERROR != 0
		BEGIN
			GOTO doerror
		END
	END
	
	ELSE
	---------- UPDATE----------
	BEGIN
		
		SET @ll_doc=@doc_no
		
		DELETE FROM transactions 
		WHERE doc_no=@doc_no
	
		IF @@ERROR != 0
		BEGIN
			GOTO doerror
		END
	END
	BEGIN
	INSERT INTO tr_header (id, date, ref, doc_no, narr)
	VALUES (@id,isnull(@date,getdate()),@ref,@ll_doc,@narr)
	
	IF @@ERROR != 0
	BEGIN
		GOTO doerror
	END
	INSERT INTO transactions (doc_no, dr_amount, cr_amount, code_value, sr_no, posted_to)
	VALUES (@ll_doc,isnull(@dr_amt,0),isnull(@cr_amt,0),@code_vaue,1,@posted_to)

	IF @@ERROR!=0
	BEGIN
		GOTO doerror
	END
	
	INSERT INTO transactions (doc_no, dr_amount, cr_amount, code_value, sr_no, posted_to)
	VALUES (@ll_doc,isnull(@cr_amt,0),isnull(@dr_amt,0),@posted_to,2,@code_value)		
	
	IF @@ERROR!=0
	BEGIN
		GOTO doerror
	END
	END
	
	COMMIT TRANSACTION
	
	Select 0
	
	GOTO doreturn
	
	doerror:
		ROLLBACK TRANSACTION
	doreturn:
		Return 0
		
	Select -100


[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]
Posted
Updated 1-Jul-13 21:59pm
v2
Comments
OriginalGriff 2-Jul-13 3:59am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
Zoltán Zörgő 2-Jul-13 4:34am    
Is this statement right?
SELECT @code_value = Selection FROM tbl_selection
If you don't specify a WHERE, the result can be ambiguous.
Where exactly is the error in the code?

1 solution

Hi friend,

 You are using wrong variable name in second Insert command you are using @code_vaue instead of @code_value.
 
Share this answer
 
Comments
Zoltán Zörgő 2-Jul-13 4:38am    
Good catch! My 5!
Phani Bharadwaj 2-Jul-13 4:44am    
Thanks u...

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900