Solved

Execute Immediate from function..

Posted on 2006-11-13
6
1,249 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now