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
 Development Tools
 ASP.NET
 Sql function

Author  Topic 

Swati Jain
Posting Yak Master

139 Posts

Posted - 2007-10-19 : 07:15:57
Hi ,
I want to write the sql function with parameters @var--> variable
@TableName --> Name of the table.

SqlF(@var,@TableName)
(
I want to execute the logic such as
Select * from @tableName Where var=@var

}

Considering all tablename and variable name valid , How to write this with exact syntax?

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-10-19 : 07:19:06
nope, you can't do that.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

ChandanJ
Starting Member

6 Posts

Posted - 2007-10-19 : 14:31:21
You can do this by Creating a new user defined function & making a call to this function from a stored procedure passing your input variables. But you need to return something from the SQL User defined function.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-10-19 : 14:33:31
quote:
Originally posted by ChandanJ

You can do this by Creating a new user defined function & making a call to this function from a stored procedure passing your input variables. But you need to return something from the SQL User defined function.




I don't see how this will help him. He must use dynamic SQL in order to dynamically provide the object names.

Swati Jain,

This is highly not recommended for performance and security reasons. Could you provide more details on what exactly you are trying to do and why?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Swati Jain
Posting Yak Master

139 Posts

Posted - 2007-10-20 : 07:09:08
quote:
Originally posted by tkizer

quote:
Originally posted by ChandanJ

You can do this by Creating a new user defined function & making a call to this function from a stored procedure passing your input variables. But you need to return something from the SQL User defined function.




I don't see how this will help him. He must use dynamic SQL in order to dynamically provide the object names.

Swati Jain,

This is highly not recommended for performance and security reasons. Could you provide more details on what exactly you are trying to do and why?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/



Hi,
thank you very much I got this answer on
http://www.sommarskog.se/dyn-search.html

DECLARE @sql nvarchar(4000), -- 16
@paramlist nvarchar(4000) -- 17
-- 18
SELECT @sql = -- 19
'SELECT o.OrderID, o.OrderDate, od.UnitPrice, od.Quantity, --

FROM dbo.' + CASE @historic_data
WHEN 0 THEN 'Orders'
WHEN 1 THEN 'Historicorders'
END + ' o
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-10-20 : 12:36:17
Let us know when performance sucks because of the use of dynamic SQL here.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -