473,386 Members | 1,773 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.

variable scoping problem

Greetings

I have been trying to write a script that will list out the size of each
user table in a specified DB. I am running into a scoping problem when
trying to format my display. I am sure there are several ways to derive the
results. My method won't allow me to

select count(*) from @tab_var
as it barks that I must declare the variable @tab_var.

-- start of script Should be able to copy and paste into QA
use fin_temp2

declare @tab_var varchar(50)
declare @tab_count int
declare @count int
declare @count_val int

select
@count = 0

declare c cursor for
select name from [Fin_Temp2].dbo.sysobjects
where type = N'U' order by name

open c

fetch c into
@tab_var
-- set @count_val = 'select count(*) from ' + @tab_var
-- select count(*) from @tab_var
set @count_val = exec ('select count(*) from ' + @tab_var) -- <<<===
select @count,@tab_var,@count_val -- <<<=== What I would
like to do so results would be displayed on one line

while @@fetch_status = 0
begin
set @count = @count + 1
fetch c into
@tab_var
select @count, @tab_var
end
close c
deallocate c
Jul 20 '05 #1
3 1657
How about.....

create a table for the results (temp or persistant).

add an 'insert ( x , y ) ' before your "select x , y from ..." in the exec
command string

select back the results table.
"Robert" <ro***********@boeing.com> wrote in message
news:I6********@news.boeing.com...
Greetings

I have been trying to write a script that will list out the size of each
user table in a specified DB. I am running into a scoping problem when
trying to format my display. I am sure there are several ways to derive
the
results. My method won't allow me to

select count(*) from @tab_var
as it barks that I must declare the variable @tab_var.

-- start of script Should be able to copy and paste into QA
use fin_temp2

declare @tab_var varchar(50)
declare @tab_count int
declare @count int
declare @count_val int

select
@count = 0

declare c cursor for
select name from [Fin_Temp2].dbo.sysobjects
where type = N'U' order by name

open c

fetch c into
@tab_var
-- set @count_val = 'select count(*) from ' + @tab_var
-- select count(*) from @tab_var
set @count_val = exec ('select count(*) from ' + @tab_var) -- <<<===
select @count,@tab_var,@count_val -- <<<=== What I would
like to do so results would be displayed on one line

while @@fetch_status = 0
begin
set @count = @count + 1
fetch c into
@tab_var
select @count, @tab_var
end
close c
deallocate c

Jul 20 '05 #2
[posted and mailed, please reply in news]

Robert (ro***********@boeing.com) writes:
set @count_val = exec ('select count(*) from ' + @tab_var) -- <<<===


Use sp_executesql. For an example, see
http://www.sommarskog.se/dynamic_sql.html#sp_executesql.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
"Robert" <ro***********@boeing.com> wrote in message news:<I6********@news.boeing.com>...
Greetings

I have been trying to write a script that will list out the size of each
user table in a specified DB. I am running into a scoping problem when
trying to format my display. I am sure there are several ways to derive the
results. My method won't allow me to

select count(*) from @tab_var
as it barks that I must declare the variable @tab_var.

-- start of script Should be able to copy and paste into QA
use fin_temp2

declare @tab_var varchar(50)
declare @tab_count int
declare @count int
declare @count_val int

select
@count = 0

declare c cursor for
select name from [Fin_Temp2].dbo.sysobjects
where type = N'U' order by name

open c

fetch c into
@tab_var
-- set @count_val = 'select count(*) from ' + @tab_var
-- select count(*) from @tab_var
set @count_val = exec ('select count(*) from ' + @tab_var) -- <<<===
select @count,@tab_var,@count_val -- <<<=== What I would
like to do so results would be displayed on one line

while @@fetch_status = 0
begin
set @count = @count + 1
fetch c into
@tab_var
select @count, @tab_var
end
close c
deallocate c


I'm not sure if the following might help you or hinder you, but it's a
query I've used from time to time:

--Start Query
SELECT object_name(id) ,rowcnt as RowCount,dpages * 8 as
TableSize,(dpages * 8)/1024 as TableSizeMB
FROM sysindexes
WHERE indid IN (1,0)
AND OBJECTPROPERTY(id, 'IsUserTable') = 1

order by TableSize desc
--End Query

which gives you the name of the table, the row count (although it's a
bit encoded, and you'd have to decipher it later if you need to see
the actual numbers, rather than just get a feel for "big" and
"small"), and the amount of space the table is occupying (first in KB,
then in MB), and it's order by the amount of space being occupied.

HTH,

Damien
Jul 20 '05 #4

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

Similar topics

7
by: Michael G | last post by:
I am a little surprised that the following that $x is visible outside of the scope in which it is (?)defined(?) (not sure if that is the correct term here). I am trying to find where in the php...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
1
by: Luxore | last post by:
Hello, I am trying to create threaded python project and I'm running into some weird Python variable scoping. I am using the "thread" module (I know, it's old and I should be using...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
5
by: John Kelsey | last post by:
Back in the "old" C/C++ days, I used to declare static variables inside functions. Something like... // just a silly example to demonstrate the technique int foo(void) { static int NextVal =...
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
10
by: John Passaniti | last post by:
(Note: This is not the same message I posted a week or so ago. The problem that prevented my previous attempt to work was a silly error in the template system I was using. This is a problem...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: ray | last post by:
Hi, all, foreach($array as $k =$v) { $foo = ...; } echo $foo; Is it allowed to access the $foo variable that is created within the loop from outside of the loop? I think it isn't allowed,...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.