473,396 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using SET statement in stored proc.

Hi I am using the following statement in stored proc.
SET @A=@B+@C-@D
But it doesn't return any value...
how should I write the above statement...
Apr 25 '09 #1
14 4237
NeoPa
32,556 Expert Mod 16PB
That seems fine to me. What values do you have in the other variables? Are any Null?
Apr 25 '09 #2
The values in other variables are not NULL I have checked this By printing each and every value....
Apr 27 '09 #3
NeoPa
32,556 Expert Mod 16PB
That's good, but it doesn't give me much to work with.

How about you set up a test Stored Procedure which uses that statement, but also PRINTs all the values. That way you can post in the Stored Procedure as well as the results, and I may be able to determine from that what is going amiss.
Apr 27 '09 #4
The above three values i.e @B,@C,@D I m reading from a table...
@C & @D r fine but @B has a value 0 in it...so while reading I think so there is a problem and it does not calculate it...

So do u know how can I check if value in @B is 0....
I want to check after reading from table and not in select Query
Apr 28 '09 #5
NeoPa
32,556 Expert Mod 16PB
Did you not understand my previous post?

I'll include it again, and if there's any part of it that you find confusing please explain and I'll see what I can do to clarify. Simply ignoring it will not allow us to progress.
@NeoPa
Apr 28 '09 #6
Do u mean I should post the test proc. here
Apr 28 '09 #7
The Test SP
Expand|Select|Wrap|Line Numbers
  1. Create Procedure TestPro
  2. @CUSTID AS CHAR(12)
  3. AS
  4. DECLARE @OPBALANCE AS NUMERIC(16,2)
  5. DECLARE @DEPOSIT AS NUMERIC(16,2)
  6. DECLARE @WITHDRAWAL AS NUMERIC(16,2)
  7. DECLARE @CLBALANCE AS NUMERIC(16,2)
  8.  
  9. SET @OPBALANCE=(SELECT OPBALANCE FROM GDAYBALANCE WHERE CUSTID=@CUSTID)
  10. SET @DEPOSIT =(SELECT DEPOSIT FROM GDAYBALANCE WHERE CUSTID=@CUSTID)
  11. SET @WITHDRAWAL=(SELECT WITHDRAWAL FROM GDAYBALANCE WHERE CUSTID=@CUSTID)
  12.  
  13. SET @CLBALANCE=@OPBALANCE+@DEPOSIT-@WITHDRAWAL
  14. PRINT(@CLBALANCE)
Apr 28 '09 #8
Now in the above SP the Opbalance field in my table is 0
But it may not be Zero all the time...
So after retreiving from table I wish to check if its 0 the assign 0 to @Opbalance bcoz it does not calaculate then...
Apr 28 '09 #9
NeoPa
32,556 Expert Mod 16PB
@SnehaAgrawal
Yes. But from post #4 I need a version which PRINTs all the variables involved. I also need to see what the values produced are.

Your code should be something like :
Expand|Select|Wrap|Line Numbers
  1. Create Procedure TestPro
  2. @CUSTID AS CHAR(12)
  3. AS
  4. DECLARE @OPBALANCE AS NUMERIC(16,2)
  5. DECLARE @DEPOSIT AS NUMERIC(16,2)
  6. DECLARE @WITHDRAWAL AS NUMERIC(16,2)
  7. DECLARE @CLBALANCE AS NUMERIC(16,2)
  8.  
  9. SET @OPBALANCE = (SELECT OPBALANCE FROM GDAYBALANCE WHERE CUSTID=@CUSTID)
  10. SET @DEPOSIT = (SELECT DEPOSIT FROM GDAYBALANCE WHERE CUSTID=@CUSTID)
  11. SET @WITHDRAWAL = (SELECT WITHDRAWAL FROM GDAYBALANCE WHERE CUSTID=@CUSTID)
  12.  
  13. PRINT @OPBALANCE
  14. PRINT @DEPOSIT
  15. PRINT @WITHDRAWAL
  16.  
  17. SET @CLBALANCE = @OPBALANCE + @DEPOSIT - @WITHDRAWAL
  18. PRINT @CLBALANCE
Apr 28 '09 #10
One thing I forgot to mention tht I am using Dynamic sql the Queries are as as follows
Expand|Select|Wrap|Line Numbers
  1. SET @SQLSTRING=N'SELECT @DEPOSITOUT=DEPOSIT FROM '+@globalDBName+' ..GDayBalance WHERE CustID=@CustID'
  2.                 SET @PARAMDEFINITION=N'@CUSTID char(12),@DEPOSITOUT NUMERIC(16,2)  OUTPUT'
  3.                 SET @INTVARIABLE=@CUSTID
  4.                 EXECUTE SP_EXECUTESQL @SQLSTRING,@PARAMDEFINITION,@CUSTID=@INTVARIABLE,@DEPOSITOUT=@DEPOSIT OUTPUT
  5.  
  6.                 SET @SQLSTRING=N'SELECT @WithdrawalOUT=Withdrawal FROM '+@globalDBName+' ..GDayBalance WHERE CustID=@CustID'
  7.                 SET @PARAMDEFINITION=N'@CUSTID char(12),@WithdrawalOUT NUMERIC(16,2)  OUTPUT'
  8.                 SET @INTVARIABLE=@CUSTID
  9.                 EXECUTE SP_EXECUTESQL @SQLSTRING,@PARAMDEFINITION,@CUSTID=@INTVARIABLE,@WithdrawalOUT=@Withdrawal OUTPUT
  10.  
  11.                 SET @SQLSTRING=N'SELECT @OPBALANCEOUT=OPBALANCE  FROM '+@globalDBName+' ..GDayBalance WHERE CustID=@CustID'
  12.                 SET @PARAMDEFINITION=N'@CUSTID char(12),@OPBALANCEOUT NUMERIC(16,2)  OUTPUT'
  13.                 SET @INTVARIABLE=@CUSTID
  14.                 EXECUTE SP_EXECUTESQL @SQLSTRING,@PARAMDEFINITION,@CUSTID=@INTVARIABLE,@OPBALANCEOUT=@OPBALANCE OUTPUT
And u were asking as wht are the PRINTS VALUES
It returns nothing in Opbalance PRINT Output
It returns 2000 in Deposit PRINT Output
It returns 500 in Withdrawal PRINT Output
As it returns nothing in Opbalance it calculates nothing in CLBalance
and it prints nothing I mean no value
I m sorry if I m bothering u but its really imp. for me
The proc. very lenghty so I m not posting the whole proc.
Apr 29 '09 #11
ck9663
2,878 Expert 2GB
I don't know why you need to create dynamic sql to get those values. Nevertheless, the only reason I can't think of why you don't have value is because your SELECT statement did not return any rows.

Try printing out your last sqlstring and paste it in your query analyzer and execute it.


---- CK
Apr 29 '09 #12
NeoPa
32,556 Expert Mod 16PB
@SnehaAgrawal
I'm sure that's true, but we can treat it as irrelevant at this stage. Switching from one to another half way through is not good policy. Also, preparing a simpler version that shows the problem (as you did first) is a very good idea.
@SnehaAgrawal
Yes. It would be better to post them as shown (as this can give clues that you may miss when simply relaying them on), but I think this has told us enough to determine at least why it's losing the calculated value.

It seems that the code for setting @OPBALANCE is evaluating to Null. Null perpetuates in SQL arithmetic expressions, so if @OPBALANCE is Null, then @CLBALANCE will be also.
Apr 29 '09 #13
Hi I got the solution u were right @Opbalance was NULL I just checked it using IF-Else it worked...thnks
May 1 '09 #14
NeoPa
32,556 Expert Mod 16PB
Pleased to hear it :)

Good luck with your project.
May 1 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sam | last post by:
Guys, I have a question regarding oracle. I have a stored procedure executing a number of statements including delete , select and lots of inserts in one big procedure and main transaction I...
5
by: Warren Wright | last post by:
Hi group, I have a select statement that if run against a 1 million record database directly in query analyzer takes less than 1 second. However, if I execute the select statement in a stored...
7
by: Alex Vorobiev | last post by:
hi there, i am using sql server 7. below is the stored procedure that is giving me grief. its purpose it two-fold, depending on how it is called: either to return a pageset (based on page...
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
2
by: Mike Hutton | last post by:
I have a rather odd problem. I have a SP which uses temp. tables along the way, and then returns a table of results: CREATE PROCEDURE dbo.usp_myproc( @pNameList VARCHAR(6000) ) AS
3
by: comp_databases_ms-sqlserver | last post by:
This post is related to SQL server 2000 and SQL Server 2005 all editions. Many of my stored procedures create temporary tables in the code. I want to find a way to find the query plan for these...
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
2
by: elpeak | last post by:
I have a stored proc that contains 10 other stored procs. When i pass in the variable i want it to find the stored proc that matches and execute. I am having issues w/ the case statement and am...
6
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.