| Author |
Topic |
|
Yellowdog
Starting Member
34 Posts |
Posted - 2008-12-19 : 12:09:48
|
| What is wrong with this stored procedure?create procedure [dbo].[get_count] (@varTable varchar(40) = null)asselect count(*) from @varTableI keep getting this errorMsg 1087, Level 15, State 2, Procedure get_count, Line 3Must declare the table variable "@varTable".I know it is something really easy but I cannot seem to figure it out....I just need to take counts of multiple tables, what is going on? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Yellowdog
Starting Member
34 Posts |
Posted - 2008-12-19 : 12:44:34
|
| Well I am going to be calling it from inside of C# and was looking for a way to be able to get counts on multiple tables by just passing it the table name.Rather than passing a count statement from c# I was looking for a way to just pass the table name and call a stored procedure.Thanks for the help, I will look into using dynamic sql unless anyone has a better way to go about this. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Yellowdog
Starting Member
34 Posts |
Posted - 2008-12-19 : 13:18:22
|
| I wish I could, my dba wants it called in a stored procedure, I am guessing to try and teach me something...So I am in the process of writing a long if statement to with a lot of table names... Any good articles on passing strings to sql using a SqlParameter()?Thanks again for your help with this. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-12-19 : 14:16:41
|
| [code]create procedure [dbo].[get_count] ( @varTable sysname = null )asif object_id(@varTable,'U') is not null begin exec ( N'select [Table_name] = N'''+@varTable+''', [Rowcount]= count(*) from '+@varTable ) endgoexec [get_count] N'dbo.authors'goexec [get_count] N'dbo.publishers'godrop procedure [dbo].[get_count][/code]Results:[code]Table_name Rowcount ----------- ----------- dbo.authors 23(1 row(s) affected)Table_name Rowcount -------------- ----------- dbo.publishers 8(1 row(s) affected)[/code]CODO ERGO SUM |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2008-12-19 : 20:40:14
|
| If all you need is a raw count of all the rows in a table, here's a shortcut that works as a stored procedure and requires no dynamic SQL:CREATE PROCEDURE RowCount @table varchar(128) ASSET NOCOUNT ONSELECT @table TableName, rows FROM sysindexes WHERE id=object_id(@table) AND indid<2Usage: exec RowCount 'myTable'An additional benefit is that this procedure doesn't actually query the table and is the fastest way to get the total row count. |
 |
|
|
Yellowdog
Starting Member
34 Posts |
Posted - 2008-12-22 : 11:09:44
|
quote: Originally posted by robvolk If all you need is a raw count of all the rows in a table, here's a shortcut that works as a stored procedure and requires no dynamic SQL:CREATE PROCEDURE RowCount @table varchar(128) ASSET NOCOUNT ONSELECT @table TableName, rows FROM sysindexes WHERE id=object_id(@table) AND indid<2Usage: exec RowCount 'myTable'An additional benefit is that this procedure doesn't actually query the table and is the fastest way to get the total row count.
I was sure there was a way that I was not aware of and I thank you for this but I cannot create the procedureMsg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'RowCount'.Msg 137, Level 15, State 2, Line 3Must declare the scalar variable "@table". not sure what the problem is here...RowCount comes up as a keyword, am I supposed to put the name of the procedure here or is that correct?I am guessing that the second error is a result of the first but again I am still trying to get a handle on this.As it is now I have set up a basic if statement in a procedure that gets passed a table name and It simply runs a select count statement based on the table name but this seems less than efficient.Thanks again for all your help |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-12-22 : 11:15:01
|
quote: Originally posted by Yellowdog
quote: Originally posted by robvolk If all you need is a raw count of all the rows in a table, here's a shortcut that works as a stored procedure and requires no dynamic SQL:CREATE PROCEDURE RowCount (@table varchar(128)) ASSET NOCOUNT ONSELECT @table TableName, rows FROM sysindexes WHERE id=object_id(@table) AND indid<2Usage: exec RowCount 'myTable'An additional benefit is that this procedure doesn't actually query the table and is the fastest way to get the total row count.
I was sure there was a way that I was not aware of and I thank you for this but I cannot create the procedureMsg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'RowCount'.Msg 137, Level 15, State 2, Line 3Must declare the scalar variable "@table". not sure what the problem is here...RowCount comes up as a keyword, am I supposed to put the name of the procedure here or is that correct?I am guessing that the second error is a result of the first but again I am still trying to get a handle on this.As it is now I have set up a basic if statement in a procedure that gets passed a table name and It simply runs a select count statement based on the table name but this seems less than efficient.Thanks again for all your help
No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Yellowdog
Starting Member
34 Posts |
Posted - 2008-12-22 : 11:25:36
|
| No dice...Using SQL 2005 if that makes any difference. |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-12-22 : 11:35:13
|
RowCount is reserved. Change RowCount to MYROWCOUNT CREATE PROCEDURE MyRowCount (@table varchar(128)) ASSET NOCOUNT ONSELECT @table TableName, rows FROM sysindexes WHERE id=object_id(@table) AND indid<2Usage: exec MyRowCount 'myTable' |
 |
|
|
Yellowdog
Starting Member
34 Posts |
Posted - 2008-12-22 : 11:53:26
|
Works like a charm!seems a little too easy. Thanks again for all of your help.On another note any easier way to pass variables from C# to sql? I have been using thissnip SqlParameter[] param = new SqlParameter[1];param[0] = new SqlParameter("@download_status_id", 6);SqlParameter[] sales = new SqlParameter[1];sales[0] = new SqlParameter("@varTable", "downloaded_sales_data");SqlParameter[] service = new SqlParameter[1]; service[0] = new SqlParameter("@varTable","downloaded_service_data");then passing the the variable in an execute statement.There is a lot more code here.Any other tricks you all may know of? |
 |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2008-12-22 : 16:34:24
|
| That's the way to do it.--Gail ShawSQL Server MVP |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2008-12-22 : 17:25:08
|
quote: RowCount is reserved
    I knew that! I swear!Funny part is I spent quite a bit of time creating a server policy in SQL 2008 to prevent us from naming objects after reserved words. |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-12-22 : 17:40:28
|
quote: Originally posted by robvolk
quote: RowCount is reserved
    I knew that! I swear!Funny part is I spent quite a bit of time creating a server policy in SQL 2008 to prevent us from naming objects after reserved words.
Don't let that reserved word stuff get in your way.CREATE PROCEDURE [RowCount] ... CODO ERGO SUM |
 |
|
|
Yellowdog
Starting Member
34 Posts |
Posted - 2008-12-23 : 10:26:30
|
quote: Originally posted by GilaMonster That's the way to do it.--Gail ShawSQL Server MVP
ThanksI was hoping there was a way to get it done with a smaller amount of code but I guess there is only one way to skin this cat.thanks again |
 |
|
|
|