Solved

Execute Immediate from function..

Posted on 2006-11-13
6
1,283 Views
Last Modified: 2010-05-18
Hi,

I am trying to execute below code within MSSQL function..

SET @query = @query + ' FROM DBO.' + @sTable + ' WHERE ' + @sTable +'.ComponentId = ' + CONVERT(CHAR, @id);
 EXECUTE IMMEDIATE @query;
 EXECUTE (@query);

Getting error as:
Invalid use of 'EXECUTE' within a function.

Please let me know how should i achieve this..
I tried using execute and execute immediate. But its giving error.

Thanks in advance.
~ HNS ~
0
Comment
Question by:homnath_sharma
  • 3
  • 2
6 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 50 total points
ID: 17936280
Hi homnath_sharma,
you can't run dynamic sql DIRECTLY inside a function, You can use OPENQUERY but not recommended


Cheers!
0
 

Author Comment

by:homnath_sharma
ID: 17936292

oK, Could you please provide some simple code example...

Its would be easy for me to understand in better way..


0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17936297
homnath_sharma,
You can create an sp to do this
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:homnath_sharma
ID: 17936331
Aneesh,

I have to migrate SybaseSQL Anywhere Trigger which has row level. So i dont find any thing like

for each row in mssql server.

Could you let me know how do i do it..

~HNS ~
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17936379
If it is in sql server 2000, then you can use a cursor or temp table or table variable
In case it is 2005, you can use ROW_NUMBER() to do the same
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17936638
check sp_executesql in BOL in SQL SERVER for executing dynamic queries.

eg

CREATE PROCEDURE InsertSales @OrdID INT, @CustID INT,
                 @OrderDate DATETIME, @DeliveryDate DATETIME
AS
DECLARE @SQLInsString NVARCHAR(500)
DECLARE @OrderMonth INT

-- Build the INSERT statement.
SET @SQLInsString = 'INSERT INTO ' +
       /* Build the name of the table. */
       SUBSTRING( DATENAME(mm, @OrderDate), 1, 3) +
       CAST(DATEPART(yy, @OrderDate) AS CHAR(4) ) +
       'Sales' +
       /* Build a VALUES clause. */
       ' VALUES (@InsOrderID, @InsCustID, @InsOrdDate,' +
       ' @InsOrdMonth, @InsDelDate)'

/* Set the value to use for the order month because
   functions are not allowed in the sp_executesql parameter
   list. */
SET @OrderMonth = DATEPART(mm, @OrderDate)

EXEC sp_executesql @SQLInsString,
     N'@InsOrderID INT, @InsCustID INT, @InsOrdDate DATETIME,
       @InsOrdMonth INT, @InsDelDate DATETIME',
     @OrdID, @CustID, @OrderDate,
     @OrderMonth, @DeliveryDate
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Foxpro errors 4 42
T-SQL: Do I need CLUSTERED here? 13 59
T-SQL: New to using transactions 9 46
Comparison query - 4 columns 9 22
This article describes some very basic things about SQL Server filegroups.
APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question