Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SELECT with a variable Tablename Expand / Collapse
Author
Message
Posted Tuesday, August 13, 2013 12:22 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 13, Visits: 66
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?
Post #1483894
Posted Tuesday, August 13, 2013 1:30 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:57 PM
Points: 12,004, Visits: 11,035
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)
Post #1483933
Posted Tuesday, August 13, 2013 1:58 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 12:12 PM
Points: 110, Visits: 721

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'
;
Post #1483948
Posted Tuesday, August 13, 2013 2:08 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 5:51 PM
Points: 2,768, Visits: 5,933
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1483954
Posted Tuesday, August 13, 2013 5:44 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 13, Visits: 66
FYI - I wanted the FROM @tablename to be a variable. Not a variable in the WHERE clause.
Post #1484033
Posted Tuesday, August 13, 2013 5:53 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 5:51 PM
Points: 2,768, Visits: 5,933
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1484036
Posted Tuesday, August 13, 2013 6:40 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 13, Visits: 66
Both Sean and Luis code worked great. thanks.
Post #1484040
Posted Tuesday, August 13, 2013 7:18 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 11:59 PM
Points: 3,596, Visits: 5,110
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!
Post #1484050
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse