Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / July 2008

Tip: Looking for answers? Try searching our database.

Table-Valued User-Defined Function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SQL Servant - 31 Jul 2008 17:36 GMT
I don't know if this is possible and I couldn't understand what Books Online
says about this (there's a surprise!), but...

Is it possible to declare a variable (not an input or output parameter)
inside a table-valued function? I need to make the select statement dynamic,
depending on the input parameter, so I need to declare something like:

DECLARE @sql VARCHAR(250)

The resulting sql would be generated via IF/CASE conditions. As the whole
sql statement is quite large, the majority would be placed in the @sql
variable at the beginning. The "where" clause is the bit that is dynamic,
which would be appended to the @sql string.

Is it possible to declare variables in such a function, apart from the
input/output parameters?

Thanks,
SQL Servant
Aaron Bertrand [SQL Server MVP] - 31 Jul 2008 17:39 GMT
You can declare variables inside a function (this just changes it from an
inline to a multi-step function, and has performance implications).  But you
CANNOT execute dynamic SQL in the function.  Use a procedure.

On 7/31/08 12:36 PM, in article
920F876C-1ED6-4386-9589-FE7D5E7A9040@microsoft.com, "SQL Servant"
<SQLServant@discussions.microsoft.com> wrote:

> I don't know if this is possible and I couldn't understand what Books Online
> says about this (there's a surprise!), but...
[quoted text clipped - 15 lines]
> Thanks,
> SQL Servant
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.