Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 1st, 2003, 06:18 AM
Authorized User
 
Join Date: Sep 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default EXEC problem.

Hi,

I have thef ollowing EXEC statem nt in a stored rpocedure.

EXEC('Select' +@OutID+ '= TestVorgangID from ' + @tableName)

It reports incorrect syntax near =. I have tried everything and no luck. Any help would be greatly appareciated.

Thanks,
A tired Pankaj


 
Old October 1st, 2003, 06:26 AM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
Have you tried - does this work??
EXEC('Select * from ' + @tableName)

Ian

 
Old October 1st, 2003, 06:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

What is @OutID? What's its value?

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 1st, 2003, 07:32 AM
Registered User
 
Join Date: Aug 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:EXEC('Select' +@OutID+ '= TestVorgangID from ' + @tableName)
You may just need a space after Select

 
Old October 1st, 2003, 02:07 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 ... and before the equal sign as follows:

Code:
EXEC('Select '  +@OutID+ ' = TestVorgangID  from ' + @tableName)


Rand
 
Old October 2nd, 2003, 04:22 AM
Authorized User
 
Join Date: Sep 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks guys. I will try it out.

Pankaj

 
Old October 2nd, 2003, 04:31 AM
Authorized User
 
Join Date: Sep 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nope, still no luck :-( Here is the whole procedure

CREATE PROCEDURE UpdateTestVorgaenge
    @tableName nVarchar (250),
    @TestName nVARCHAR(250),
    @OutID int OUTPUT

AS
EXEC('INSERT INTO ' + @tableName + ' (Name) values ("' + @TestName + '" ) ')
EXEC('Select ' + @OutID + ' = TestVorgangID from ' + @tableName)

The surprising thing is the first statement seems to execute just fine. The second line always gives this error on execution...Incorrect syntax near =.


 
Old October 2nd, 2003, 05:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Place your output parameter inside the quotes:
Code:
EXEC('Select @OutID = TestVorgangID from ' + @tableName)
Sometimes it's helpful to PRINT the string you are trying to execute; if you had, you would have seen the problem...

Note that this query will still likely fail, though for a different reason, unless there is only one row in the table whose name you are passing as a parameter.

Edit: Now that I think about it, this query still won't work, as I'm sure there will be variable scoping problems. That is, the variable @OutID inside the exec is at a different scope that then the @OutID in the stored procedure.

See BOL for sp_executesql for an example of how to return a value via dynamic SQL.

See also http://p2p.wrox.com/topic.asp?TOPIC_ID=4534 from some security pitfalls using dynamic sql.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 4th, 2003, 05:39 PM
Authorized User
 
Join Date: Jun 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

are you just trying to get the new records identity?
Try

CREATE PROCEDURE UpdateTestVorgaenge
    @tableName nVarchar (250),
    @TestName nVARCHAR(250),
    @OutID int OUTPUT
AS
    EXEC('INSERT INTO ' + @tableName + ' (Name) values ("' + @TestName + '" ) ')
    SET @OutID = @@Identity





Similar Threads
Thread Thread Starter Forum Replies Last Post
exec:java classpath problem mslinn BOOK: Beginning Spring Framework 2 ISBN: 978-0-470-10161-2 2 December 1st, 2008 08:05 AM
Get value with EXEC Javierera SQL Server 2005 6 November 3rd, 2008 04:28 PM
EXEC function ajutla Beginning PHP 0 August 19th, 2004 09:18 AM
Problem to run delete and exec DTS in Stored Proc tyh79 SQL Server DTS 7 July 14th, 2004 10:27 AM
Still Fighting Exec. from ASP - Please Help MAtkins Classic ASP Basics 0 December 2nd, 2003 07:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.