473,416 Members | 1,740 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,416 software developers and data experts.

Quandary: System Variables Resetting

NeoPa
32,556 Expert Mod 16PB
I'm looking to use values set for both @@ERROR and @@ROWCOUNT after a query is run.

Unfortunately, it seems that any SQL Statement resets @@ERROR, and any SET statement resets @@ROWCOUNT.

How then, do I save the values of both at the point immediately after the query has been run?

I started by saving @@ERROR into a local variable using :
Expand|Select|Wrap|Line Numbers
  1. DECLARE @SaveErr AS int
  2.  
  3. DELETE FROM dbo.MyTable
  4.  
  5. SET @SaveErr = @@ERROR
At this point the SET line has cleared the value in @@ROWCOUNT. If I save @@ROWCOUNT first it seems I lose the value of @@ERROR.

I expect I'm approaching this all from the wrong direction, so any advice is welcomed. I'd certainly like to understand how to manage this though, even if bypassing the issue may be the preferred approach.
Jul 30 '10 #1

✓ answered by ck9663

Why not save the two values at the same time?

Expand|Select|Wrap|Line Numbers
  1.  
  2. DECLARE @SaveErr int, @AffectedRows int
  3.  
  4. DELETE FROM dbo.MyTable
  5.  
  6. SET @SaveErr = @@ERROR, @AffectedRows = @@rowcount
  7.  
  8.  
Happy Coding!!!

~~ CK

**Edit**
b0010100: Should last line be?:

Expand|Select|Wrap|Line Numbers
  1. SELECT @SaveErr = @@ERROR, @AffectedRows = @@rowcount
ck9663: b0010100 is correct, it should've been a SELECT, not SET...

8 1313
code green
1,726 Expert 1GB
This is quite a dilemma.
So accessing either system variable emptys both of them?

There is probably a correct way to do this.
In the meantime what happens when you try this?
Expand|Select|Wrap|Line Numbers
  1. DECLARE @BothErr AS VARCHAR
  2.  
  3. DELETE FROM dbo.MyTable 
  4.  
  5. SET @BothErr = 'Error: '+ @@ERROR + 'Rowcount '+ @@ROWCOUNT 
  6.  
Jul 30 '10 #2
NeoPa
32,556 Expert Mod 16PB
That would complain about using + with data types that have no default conversion. Using CAST or CONVERT on the int values would work. I would hope somewhere there is a solution that doesn't involve converting both items to a string though. I can't help feeling I must be approaching it in a way that an experienced T-SQL coder wouldn't. I can't explain better until I am a more experienced T-SQL coder ;)

Thanks for responding. Your efforts are appreciated :)
Jul 30 '10 #3
Jerry Winston
145 Expert 100+
How does this work out for you:

Expand|Select|Wrap|Line Numbers
  1. DECLARE @errTable TABLE (eCode INT, eRow INT)
  2. INSERT INTO @errTable (eCode,eRow) VALUES (@@ERROR, @@ROWCOUNT);
  3.  
  4.  
Jul 30 '10 #4
ck9663
2,878 Expert 2GB
Why not save the two values at the same time?

Expand|Select|Wrap|Line Numbers
  1.  
  2. DECLARE @SaveErr int, @AffectedRows int
  3.  
  4. DELETE FROM dbo.MyTable
  5.  
  6. SET @SaveErr = @@ERROR, @AffectedRows = @@rowcount
  7.  
  8.  
Happy Coding!!!

~~ CK

**Edit**
b0010100: Should last line be?:

Expand|Select|Wrap|Line Numbers
  1. SELECT @SaveErr = @@ERROR, @AffectedRows = @@rowcount
ck9663: b0010100 is correct, it should've been a SELECT, not SET...
Jul 30 '10 #5
Jerry Winston
145 Expert 100+
@CK

Should last line be?:

Expand|Select|Wrap|Line Numbers
  1. SELECT @SaveErr = @@ERROR, @AffectedRows = @@rowcount
Jul 30 '10 #6
NeoPa
32,556 Expert Mod 16PB
ck9663: Why not save the two values at the same time?
Because I didn't know how to :D

In truth, I did explore that a bit but didn't stumble across the correct syntax. I'm buried in the books online for much of the time at the mnoment, and I'm generally quite impressed with what it can teach me, but I still find some things hard to discover there.

Thanks for this tip. I can't test it till Monday now, but frankly I know your work well enough to know that's just a formality.

Thanks also to b0010100. What you suggested may well work. I think I prefer the less involved method where possible though. I'm sure as I get more familiar with it all things will occur to me more naturally, but for now I'm just trying to get up to speed.
Jul 30 '10 #7
ck9663
2,878 Expert 2GB
b0010100 is correct, it should've been a SELECT, not SET...

thanks man...

~~ CK
Jul 31 '10 #8
NeoPa
32,556 Expert Mod 16PB
Thanks CK. I've updated your Best Answer post to reflect that for anyone else reading this later.

Thanks again b0010100. Your input was important :)
Jul 31 '10 #9

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

Similar topics

2
by: Kumar | last post by:
Need urgent help... I have installed DB2UDB version 7.2 on Windows 2000. When I try to open Command center or Command window it gives me the following error:...
1
by: Steve Adams | last post by:
Is there any way to access system variables from C under Windows 2000? I'd like to use the system variable %SystemRoot% in the remove(file) call: ...
1
by: Robin.Liu | last post by:
How can I get or set values of system variables using .NET? Any comment be appreciated! Robin.Liu lbhappy@263.net
4
by: Israel Ordonez Jr | last post by:
Hi everybody, I am having a problem with an ASP.NET application i am working on. I new to ASP.NET so I'm not sure if I'm doing this right. I am working on a website that has an oil price listed...
8
by: Just Me | last post by:
With VB6 I wanted to be consistent in how I named variables so I developed the following doc. The last column is what I named the variable. I tried to be consistent wit the Win32 document but it...
1
by: ezmiller | last post by:
Here is what I want to do. I have a series of xml files that reference dtd file and xsl files, but those xml files are viewed on differnet computers. What I am wondering is whether it is possible...
2
by: Sean Campbell | last post by:
Hello all! I have been looking in the Framework documentation for an answer and it has not shown itself. Could someone tell me if it is possible with VB or C# to add and modify Windows System...
2
by: cheesywillie | last post by:
How do i get a string variable to be included into a system command? For example: #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int main(int nNumberofArgs,...
5
by: Jake G | last post by:
I need a javascript that will display all system variables (e.g. HTTP_REFERER). I am not a programmer, so if you could post all of the html and stuff that would be great. Thanks, Jake
4
by: Krishna | last post by:
Environment variable set up is the most confusing part for me all the time. Please help me with the following questions: When I install python in a new system, I will go to environment variables...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.