Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SELECT with a variable Tablename


SELECT with a variable Tablename

Author
Message
bgrossnickle
bgrossnickle
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 342
I am rying to run a TSQL SELECT Count(*) that has a @Tablename variable in it.

My Tablename name needs to be variable and I will build it on the fly. All I want to know if if there is data in the table.

--Tried this but it did not work because of the @Database
Declare @Database varchar(20)
Set @Database = 'TDSTEP'
Declare @RowCount int
SELECT @RowCount = Count(*) FROM @Database
Print @RowCount

--Tried this but it did not work because of the @RowCount
Declare @Database varchar(20)
Set @Database = 'TDSTEP'
Declare @RowCount int
declare @SQL as varchar(max)
set @SQL = 'SELECT @RowCount = Count(*) FROM ' + @Database
exec (@SQL)

How can i get a variable TableName and yet interigate the number of rows returned?
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16548 Visits: 17002
bgrossnickle (8/13/2013)
I am rying to run a TSQL SELECT Count(*) that has a @Tablename variable in it.

My Tablename name needs to be variable and I will build it on the fly. All I want to know if if there is data in the table.

--Tried this but it did not work because of the @Database
Declare @Database varchar(20)
Set @Database = 'TDSTEP'
Declare @RowCount int
SELECT @RowCount = Count(*) FROM @Database
Print @RowCount

--Tried this but it did not work because of the @RowCount
Declare @Database varchar(20)
Set @Database = 'TDSTEP'
Declare @RowCount int
declare @SQL as varchar(max)
set @SQL = 'SELECT @RowCount = Count(*) FROM ' + @Database
exec (@SQL)

How can i get a variable TableName and yet interigate the number of rows returned?


Do you need this to use a variable for some reason?

You can do this with something like this.


Declare @Database varchar(20)
Set @Database = 'YourTableNameHere'
Declare @RowCount int
declare @SQL as varchar(max)
set @SQL = 'SELECT Count(*) FROM ' + @Database

declare @MyCount table(MyRowCount int)

insert @MyCount
exec (@SQL)

select * from @MyCount



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Bill Talada
Bill Talada
Old Hand
Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)

Group: General Forum Members
Points: 379 Visits: 1810
declare @TableName varchar(128);
set @TableName = 'sysdiagrams';

if exists
(
select i.rowcnt
from sys.tables t
join sys.sysindexes i on t.object_id = i.id
where indid in (1,0)
and t.name = @TableName
)
print 'yes'
;
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8506 Visits: 18113
Have you tried something like this?


Declare @Database varchar(20)
Set @Database = 'TDSTEP'
Declare @RowCount int
declare @SQL as nvarchar(max)
set @SQL = ' SELECT @RowCountOUT = Count(*) FROM ' + @Database
exec sp_executesql @SQL, N'@RowCountOUT int OUTPUT', @RowCountOUT=@RowCount OUTPUT;

SELECT @RowCount




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
bgrossnickle
bgrossnickle
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 342
FYI - I wanted the FROM @tablename to be a variable. Not a variable in the WHERE clause.
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8506 Visits: 18113
bgrossnickle (8/13/2013)
FYI - I wanted the FROM @tablename to be a variable. Not a variable in the WHERE clause.

But you got a different way of doing things that will give you what you need.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
bgrossnickle
bgrossnickle
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 342
Both Sean and Luis code worked great. thanks.
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
Could you use something like this?


USE [TDStep]
GO

SELECT TableName=OBJECT_NAME(OBJECT_ID), st.row_count
FROM sys.dm_db_partition_stats st
WHERE index_id < 2
ORDER BY st.row_count DESC





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search