SQL Clone
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
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 344
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
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26708 Visits: 17557
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 Modens 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
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1212 Visits: 2001
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
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17038 Visits: 19122
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
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 344
FYI - I wanted the FROM @tablename to be a variable. Not a variable in the WHERE clause.
Luis Cazares
Luis Cazares
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17038 Visits: 19122
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
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 344
Both Sean and Luis code worked great. thanks.
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)

Group: General Forum Members
Points: 7539 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