August 13, 2013 at 12:22 pm
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?
August 13, 2013 at 1:30 pm
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
August 13, 2013 at 1:58 pm
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'
;
August 13, 2013 at 2:08 pm
August 13, 2013 at 5:44 pm
FYI - I wanted the FROM @tablename to be a variable. Not a variable in the WHERE clause.
August 13, 2013 at 5:53 pm
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.
August 13, 2013 at 6:40 pm
Both Sean and Luis code worked great. thanks.
August 13, 2013 at 7:18 pm
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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply