Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stupidist question ever?

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)
as
select count(*) from @varTable


I keep getting this error
Msg 1087, Level 15, State 2, Procedure get_count, Line 3
Must 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

Posted - 2008-12-19 : 12:18:54
You need to use dynamic SQL for this, but you really shouldn't do this in the first place. Why do you need the table name to be dynamic? This is a very bad idea.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-19 : 12:51:39
You shouldn't use dynamic SQL for this. You might as well use inline SQL in your C# code rather than a stored procedure like this.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-19 : 13:34:47
Show your DBA what you are planning to do then as the dynamic talbe approach defeats the purpose of a stored procedure. The IF statement approach would be fine, but then that requires a lot of code.


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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
)
as
if object_id(@varTable,'U') is not null
begin
exec ( N'select [Table_name] = N'''+@varTable+''', [Rowcount]= count(*) from '+@varTable )
end
go

exec [get_count] N'dbo.authors'
go
exec [get_count] N'dbo.publishers'
go

drop 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
Go to Top of Page

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) AS
SET NOCOUNT ON
SELECT @table TableName, rows FROM sysindexes WHERE id=object_id(@table) AND indid<2


Usage: 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.
Go to Top of Page

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) AS
SET NOCOUNT ON
SELECT @table TableName, rows FROM sysindexes WHERE id=object_id(@table) AND indid<2


Usage: 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 procedure

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'RowCount'.
Msg 137, Level 15, State 2, Line 3
Must 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
Go to Top of Page

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)) AS
SET NOCOUNT ON
SELECT @table TableName, rows FROM sysindexes WHERE id=object_id(@table) AND indid<2


Usage: 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 procedure

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'RowCount'.
Msg 137, Level 15, State 2, Line 3
Must 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.
Go to Top of Page

Yellowdog
Starting Member

34 Posts

Posted - 2008-12-22 : 11:25:36
No dice...

Using SQL 2005 if that makes any difference.
Go to Top of Page

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)) AS
SET NOCOUNT ON
SELECT @table TableName, rows FROM sysindexes WHERE id=object_id(@table) AND indid<2

Usage: exec MyRowCount 'myTable'
Go to Top of Page

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 this

snip
 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?


Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2008-12-22 : 16:34:24
That's the way to do it.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

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.
Go to Top of Page

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
Go to Top of Page

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 Shaw
SQL Server MVP



Thanks

I 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
Go to Top of Page
   

- Advertisement -