Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Attempt at a general counting SP
Message
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Miscellaneous
Thread ID:
00758191
Message ID:
00758267
Views:
18
Michael,

Thanks. I think that you are right in that I am complicating things in my attempt to simplify. As for this SP, functionality wins over elegance.

Doug

>You cannot use an expression in place of a table in a query. if you really want to do this, you'll have to use dynamic sql.
>
>
>CREATE PROCEDURE get_count @tcTable varchar(30), @tcColumn varchar(30), @tnValue integer AS
>
>declare @sql varchar(4000)
>SET @sql =
> 'SELECT count(*) from ' +
> @cTable +
> ' where ' +
> @tcColumn +
> ' = ' +
> cast(@tnValue as varchar(20))
>
>EXECUTE @sql
>
>
>The convention is to only use a stored procedure's return value to indicate status and not as a means for returning data. The proc get much simplier if you let the value come back as a resultset. While an OUTPUT parameter would be better, it increase the complexity of the proc more than it's probably worth.
>
>Now, for as simple as this is, you might be better off just sending an ad-hoc query to the database.
>
>-Mike
>
>>I'm trying to create a SP that can return a count of rows for a specified table, column and column value. I get an error that states "Must declare the variable @tcTable".
>>
>>Any ideas?
>>
>>Doug
>>
>>Here's the SP ...
>>
>>CREATE PROCEDURE get_count @tcTable varchar(30), @tcColumn varchar(30), @tnValue integer AS
>>
>>declare @nCount integer
>>
>>select @nCount = count(*) from @cTable where @tcColumn = @tnValue
>>
>>RETURN @nCount
>>GO
Previous
Reply
Map
View

Click here to load this message in the networking platform