| Author |
Topic |
|
jaskalirai
Starting Member
31 Posts |
Posted - 2007-09-25 : 14:24:44
|
| hi there im trying to create a sproc with the code below. however i am having trouble using the table name as a variable. can anyone help me with this i do not know what im doing wrong.create procedure usp_test@tablename varchar (20)ASSELECT so.name AS [TABLENAME], MAX(si.rows) AS [ROWCOUNT]FROM sysobjects AS so INNER JOIN sysindexes AS si ON (SO.ID=SI.id)WHERE so.xtype = 'U' GROUP BY so.name ORDER BY [ROWCOUNT] |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-25 : 14:25:58
|
| You have @tablename as an input parameter, but you aren't using it anywhere in your code. Could you show us the code that is failing?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
jaskalirai
Starting Member
31 Posts |
Posted - 2007-09-25 : 15:07:29
|
quote: Originally posted by tkizer You have @tablename as an input parameter, but you aren't using it anywhere in your code. Could you show us the code that is failing?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/
that is the code the variable i first used was @name i then changed it. im rather confused |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-09-25 : 15:12:11
|
| you ARE NOT using the variable @tablename anywhere in your query!_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-25 : 15:14:33
|
| There is nothing wrong with the stored procedure that you posted, so I don't see how we can help you fix something that isn't broken. Perhaps you haven't explained your problem clearly yet.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
jaskalirai
Starting Member
31 Posts |
Posted - 2007-09-25 : 15:21:20
|
hi there wat i want to do is use that sproc. so when i use execute usp_test@name = table a i want to just display table a and a row count however i cant seem to get that working. Because i keep getting all the table within that database.Is that more clearer?? plz help me !!!!!!!!! |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-25 : 15:23:59
|
| It still isn't clear to me. Please show us exactly what output the stored procedure should return.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-25 : 15:25:18
|
| Or perhaps you just want:WHERE so.xtype = 'U' AND so.name = @tablenameBe warned though that the row count is going to be inaccurate. The only way to get a true row count is to use count(*).Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
jaskalirai
Starting Member
31 Posts |
Posted - 2007-09-25 : 15:32:12
|
| so for example i use the sproc.lets say i change the variable name to tablexy i should be able to view the table name and row count for that particular table. however when i execute the sproc i cnt. can you please clarify why and where im going wrong.My variable needs to be a table name!!!so when i use the sproc and enter any table name next to the variable it should work |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-25 : 15:33:36
|
| And did you read my last post?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
jaskalirai
Starting Member
31 Posts |
Posted - 2007-09-25 : 15:41:56
|
| thanks very much !!! i cnt believe i missed that out. im new to sql and i am currently training to be a sql developer believe it or not. thank you very much once again.one last point would i input execute dbo.usp_test if i wanted to use the sproc in another database ?? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-25 : 15:44:20
|
| To access an object not in the current database, you must use the three part naming convertion:DatabaseName.ObjectOwner.ObjectNameYou need to understand that your stored procedure is going to return incorrect information. Check out DBCC UPDATEUSAGE in BOL for more details.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
|