?
Solved

Execute Immediate from function..

Posted on 2006-11-13
6
Medium Priority
?
1,302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 100 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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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