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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Tables as variables

Author  Topic 

evanburen
Posting Yak Master

167 Posts

Posted - 2011-10-13 : 08:40:47
Hi

I want to pass a table name as a variable to this stored procedure but can't quite get it right

ALTER PROCEDURE p_CashSample
@RecordCount Int = NULL OUTPUT,
DECLARE @TableName Table = 'PopulationBondsDeposits'
AS
SELECT @RecordCount = COUNT(*)
FROM @TableName

-- *** Level 1 ***

SELECT TOP (CASE WHEN @RecordCount BETWEEN 2 AND 8 THEN 2
WHEN @RecordCount BETWEEN 9 AND 15 THEN 5
ELSE 10
END)
*
FROM @TableName
ORDER BY NEWID()

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-13 : 08:45:51
you will need to use Dynamic SQL to do it

please read http://www.sommarskog.se/dynamic_sql.html


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

evanburen
Posting Yak Master

167 Posts

Posted - 2011-10-13 : 09:07:15
Dang. Thanks.
Go to Top of Page
   

- Advertisement -