Author |
Topic |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2006-08-16 : 09:33:32
|
David writes "hello, hope you can help me...i want to write this kind of function:ALTER FUNCTION [dbo].[getsize](@tableId int,)RETURNS intAS BEGINDECLARE @table nvarchar(40)SET @table = 'table_' + convert(nvarchar(10), @tableId)DECLARE @SQL nvarchar(512)SET @SQL = 'SELECT @value = count(*) FROM dbo.[' + @table+ ']'DECLARE @value intEXEC sp_executesql @SQL, N'@value int out', @value out RETURN @valueEND but, as you sure now, this function is not goodas i get an error, "only functions and ext. proc. can be used"is there any other solution?" |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-08-16 : 09:46:11
|
[code]CREATE FUNCTION [dbo].[getsize](@tableId int,)RETURNS intAS BEGINDECLARE @table nvarchar(40), @RowCount int SET @table = 'table_' + convert(nvarchar(10), @tableId)SELECT @RowCount = ind.rowcntFROM sysobjects sysLEFT OUTER JOIN sysindexes indON ind.[id] = ind.[id]WHERE ind.xtype = 'U' AND ind.indid < 2 and ind.[name] = @table return @RowCount END [/code]Chirag |
 |
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-08-16 : 11:47:28
|
How is the following (however this is a stored procedure and not working exactly same as a function):Create procedure Row_Count (@tblName varchar(100), @Rowcount nvarchar(30) output)asdeclare @strSQL nvarchar (4000), @out intset @out = 0SELECT @strSQL = 'select @outInside = count(*) from ' + @tblName SELECT @Rowcount = N' @outInside int OUTPUT'EXEC sp_ExecuteSQL @strSQL, @Rowcount, @outInside = @out OUTPUTSelect @out-- Call this as :-- Row_Count 'urTblName', 0 Srinika |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-08-16 : 12:25:41
|
http://www.nigelrivett.net/SQLTsql/sp_executesql.htmlMadhivananFailing to plan is Planning to fail |
 |
|
david_tm
Starting Member
2 Posts |
Posted - 2006-08-19 : 07:25:08
|
Srinika...thanxbut i need functionit's not hard to write such spper asoera ad astra |
 |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-08-19 : 08:50:17
|
David, I dont think then you will be able to acheive the same using function, you can try the function which i posted, but for that there are lots of contraints for its accuracy as its posted in Srinika's thread. But what is the purpose of writing this kind of function? may be someone over here, can give you some better option to do it in a different way.BTW quote: per asoera ad astra
What does this means ??? Chirag |
 |
|
david_tm
Starting Member
2 Posts |
Posted - 2006-08-26 : 02:01:04
|
@chiragkhabariaoh my, it was misspeledfrom WikiPediaper aspera ad astra "through hardships to the stars" From Seneca the Younger. Motto of NASA and the South African Air Force. A common variant, ad astra per aspera ("to the stars through hardships"), is the state motto of Kansas. Ad Astra ("To the Stars") is the title of a magazine published by the National Space Society.per aspera ad astra |
 |
|
|