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.
| Author |
Topic |
|
lovitz
Starting Member
3 Posts |
Posted - 2005-04-04 : 16:45:24
|
| Hello I would like to write function with table name parameter but don't know how. I wrote something like this but with errorsif u can help me how to use function parameter as table name THX U------------my function---------------USE ReportIF EXISTS (SELECT name FROM sysobjects WHERE name = 'f_view_report' AND type = 'IF') DROP FUNCTION f_view_reportGOcreate function f_view_report (@tablename sysname,@firstdate datetime,@lastdate datetime)returns tableASreturn(SELECT DISTINCT TOP 100 PERCENT ValueID, [TimeStamp], RealValue, Quality, FlagsFROM @tablename -- here is problem WHERE ([TimeStamp] BETWEEN CONVERT(DATETIME, @firstdate , 102) AND CONVERT(DATETIME, @lastdate, 102))ORDER BY [TimeStamp])go |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2005-04-04 : 18:55:21
|
| This is an extraordinarily bad idea, fortunately it can't be done as a UDF because of the dynamic nature of the command. It would be a significant security breach to allow someone to run this function and pass any table name they please. Not to mention that it could easily throw a syntax or runtime error.The fact that you would need a function like this suggests that 1) you're storing similar or identical data in multiple tables, which further suggests 2) your database is probably not designed correctly for the data you want to store. |
 |
|
|
lovitz
Starting Member
3 Posts |
Posted - 2005-04-05 : 02:38:37
|
| I made something like this and it works ********************************USE ReportIF EXISTS (SELECT name FROM sysobjects WHERE name = 'proc1' AND type = 'P') DROP PROCEDURE proc1GOCREATE procedure proc1 @tablename char(255),@firstdate varchar(255),@lastdate varchar(255) AS BEGIN declare @tempSql nvarchar(4000), @Value floatselect @tempSql = 'SELECT max(RealValue)FROM '+ @tablename +'WHERE [TimeStamp] BETWEEN CONVERT(DATETIME,'+ @firstdate +', 102) AND CONVERT(DATETIME,'+ @lastdate+', 102)'print @tempSqlexec sp_executesql @tempSql, N'@Value float output', @Value outputend*********--exampleexec proc1 'tag03','''2005-04-04 08:09:18''','''2005-04-04 08:09:22''' |
 |
|
|
Blastrix
Posting Yak Master
208 Posts |
Posted - 2005-04-05 : 03:04:11
|
As Rob pointed out, there's probably a chance that your database design could use some work. In the event that that is not the case, and there's a fairly legitimate reason for having multiple tables with the same columns, you might want to try creating a view, and selecting off of that.if (object_ID('dbo.vwValueTables') IS NOT NULL) DROP VIEW dbo.vwValueTablesGOCREATE VIEW dbo.vwValueTablesASSELECT TableName = 'ValueTable1', ValueID, [TimeStamp], RealValue, Quality, FlagsFROM ValueTable1UNION ALLSELECT TableName = 'ValueTable2', ValueID, [TimeStamp], RealValue, Quality, FlagsFROM ValueTable2--etc...GOif (object_ID('dbo.f_view_report') IS NOT NULL) DROP VIEW dbo.f_view_reportGOcreate function dbo.f_view_report ( @tablename varchar(255), @firstdate datetime, @lastdate datetime )returns tableASreturn(SELECT DISTINCT TOP 100 PERCENT ValueID, [TimeStamp], RealValue, Quality, FlagsFROM vwValueTableWHERE TableName = @tablename AND ([TimeStamp] BETWEEN CONVERT(DATETIME, @firstdate , 102) AND CONVERT(DATETIME, @lastdate, 102))ORDER BY [TimeStamp])GO |
 |
|
|
lovitz
Starting Member
3 Posts |
Posted - 2005-04-05 : 03:39:21
|
| Thanks that was what I need |
 |
|
|
|
|
|
|
|