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 |
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 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com |
|
|
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. |
|
|
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 KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
|
|
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 KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/
Hi,thank you very much I got this answer onhttp://www.sommarskog.se/dyn-search.htmlDECLARE @sql nvarchar(4000), -- 16 @paramlist nvarchar(4000) -- 17 -- 18SELECT @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 |
|
|
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 KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
|
|
|
|
|
|
|