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

Re-display result set without re-running query in Query Analyzer?

I hope I am not asking about something that has been done before, but I
have searched and cannot find an answer. What I am trying to do is to
run a query, and then perform some logic on the rowcount and then
possibly display the result of the query. I know it can be done with
ADO, but I need to do it in Query Analyzer. The query looks like this:

select Var
from DB
where SomeCriteria

if @@Rowcount = 0
select 'n/a'
else if @@Rowcount = 1
select -- this is the part where I need to redisplay the result
from the above query
else if @@Rowcount > 1
-- do something else

The reason that I want to do it without re-running the query is that I
want to minimize impact on the DB, and the reason that I can't use
another program is that I do not have a develpment environment where I
need to run the queries. I would select the data into a temp table, but
again, I am concerned about impacting the DB. Any suggestions would be
greatly appreciated. I am really hoping there is something as simple as
@@resultset, or something to that effect.

Apr 9 '06 #1
6 2097
(jo******@gmail.com) writes:
I hope I am not asking about something that has been done before, but I
have searched and cannot find an answer. What I am trying to do is to
run a query, and then perform some logic on the rowcount and then
possibly display the result of the query. I know it can be done with
ADO, but I need to do it in Query Analyzer. The query looks like this:

select Var
from DB
where SomeCriteria

if @@Rowcount = 0
select 'n/a'
else if @@Rowcount = 1
select -- this is the part where I need to redisplay the result
from the above query
else if @@Rowcount > 1
-- do something else
I hope you know that @@rowcount is volatile, so the above logic would
have be replaced with:

SELECT @rowc = @@rowcont
IF @rowc = 0
...
The reason that I want to do it without re-running the query is that I
want to minimize impact on the DB, and the reason that I can't use
another program is that I do not have a develpment environment where I
need to run the queries. I would select the data into a temp table, but
again, I am concerned about impacting the DB. Any suggestions would be
greatly appreciated. I am really hoping there is something as simple as
@@resultset, or something to that effect.


There is no such thing in SQL Server, but judging from the logic
above, you only want one row, and in this case you could bounce the
data over variables:

SELECT @col1 = col1, @col2 = col2, ...
...
IF @rowc = 1
SELECT col1 = @col1, col2 = @col2, ...

If the result set have multiple rows, you will have to re-run the query
or use a temp table. Or just skip the n/a thing.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 9 '06 #2
I appreciate the advice on @@rowset and I will be sure to set it to a
variable. The problem is that quite often there are multiple rows
returned, so I am forced to either re-run the query (which I don't want
to do because it may bog down the DB). Or I could select into a temp
table, but again I am concerned about a performance hit.

Apr 9 '06 #3
just.an.imbecile (jo******@gmail.com) writes:
I appreciate the advice on @@rowset and I will be sure to set it to a
variable. The problem is that quite often there are multiple rows
returned, so I am forced to either re-run the query (which I don't want
to do because it may bog down the DB). Or I could select into a temp
table, but again I am concerned about a performance hit.


If your main concern is to keep down the load, then just skip extras
with the 'n/a' stuff, and just run the SELECT right away.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 9 '06 #4
I actually got it working. I was using QuickKeys to automate a process
and found a way around re-running the query. However, for future
reference, are you saying that if you run a query and then re-run the
same query shortly therafter then the second time it doesn't have as
much of an impact on the DB?

Apr 10 '06 #5
just.an.imbecile (jo******@gmail.com) writes:
I actually got it working. I was using QuickKeys to automate a process
and found a way around re-running the query. However, for future
reference, are you saying that if you run a query and then re-run the
same query shortly therafter then the second time it doesn't have as
much of an impact on the DB?


I didn't really say that. But the impact on the system may be somewhat
smaller the second time, because all data would likely to be in the cache.

The impact of bouncing the data over a temp table, rather than running
the query twice is likely to have less impact.

Then again, it depends very much on the query. A query than retrieves
a couple of rows using indexes and runs sub-second, is nothing to bother
about. A query that has to scan a 10-million row table is nothing to take
lightly in the production environment.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 10 '06 #6
first time through select into a temp table.

then you can play with the temp table without having to go back to main
query.

Apr 10 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Aspersion | last post by:
I'm building an ASP page that has a lot of text and graphics. There is a calculation facility on the page. The user enters several numbers in a form and presses a button to see the calculated...
1
by: NDS Ltd | last post by:
hi, someone on this group made me a query which worked a treat. i have tried changing it but instead of returning the expected 700 records it returns half a million.. with lots of duplicates. ...
2
by: David Gordon | last post by:
Hi Folks, I wonder if anyone can help me with the following (perhaps trivial) problem: <xml> <node name="a" type="a"/> <node name="b" type=""/> <node name="c"/> <node name="d" type="b"/>...
2
by: Ben Fidge | last post by:
Is it possible to sort the result of an XPath "select" query? I need to return a subset of a large xml file, and XPath is the way to go. I'd like to sort this subset afterwards? This seems...
2
by: Roberta | last post by:
Hello, When I use the following code to print a report for a certain client, it works fine: DoCmd.OpenReport "rpt_MNMTSingleCover_Summary_Recommendations", acPreview, "", "!=" But I want...
12
by: briansmccabe | last post by:
Does anyone have a good approach to displaying in PHP a simple COUNT query that is performed on a table in a MySQL db? Thanks
5
tolkienarda
by: tolkienarda | last post by:
Hi all how would i go about reseting the internal pointer in a mysql_query result I use a loop to display the results of a colom in a select box here is the loop $i=1; while ($i<=$row)...
3
by: rupeshjain | last post by:
Is it possible or not: declare @query varchar(500) declare @var varchar(50) @query='select U_Id from tblUser...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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,...

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.