Solved

Execute Immediate syntax..

Posted on 2006-11-07
6
911 Views
Last Modified: 2007-12-19
Hi,

Hope to get the solution for the below from expert asap..

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

EXECUTE IMMEDIATE @query;

Whats wrong with the above syntax, which i am trying to implement inside my cursor. I am getting error as:

Cannot add rows to sysdepends for the current stored procedure because it depends on the missing object 'IMMEDIATE'. The stored procedure will still be created.

Awaiting to hear your support on this asap.

Thanks in advance.
~ HNS ~
0
Comment
Question by:homnath_sharma
6 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 17895768
I assume you are using SQL server:
EXEC(@query);
0
 
LVL 5

Expert Comment

by:MrNetic
ID: 17898637
Remove the IMMEDIATE and run

EXEC @Variable.

Best Regards,

Paulo Condeça.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 17899116
EXECUTE IMMEDIATE is a statement used in C language. In MS_SQL you hane EXECUTE or EXEC and you can also use the sp_executesql stored procedure, so either

EXEC(@query)   (you have to use brackets)

or

EXEC sp_executesql @query  (barackets are not required)

using sp_executesql gives you the advantage that you can pas into the @query parameters and also you cna use OUT parameters to get data from the execution.
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

Introduction: Often, when running a query with joins, the results show up "duplicates", and often, those duplicates can be "eliminated" in the results using DISTINCT, for example. Using DISTINCT is simple: just add it after the SELECT keyword, an…
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…

759 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

17 Experts available now in Live!

Get 1:1 Help Now