Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
| Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language 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
  #1 (permalink)  
Old November 30th, 2003, 10:26 AM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sql query question.

Hi everyone,

I am trying to do a select sub-query, but for some reason it is not working out.

I have the following stored procedure:

CREATE MyProcedure
@newID int,
@oldID int
AS

INSERT INTO MyTable(ExperimentID, Threshold, Energy) VALUES(@newID, Select Threshold, Energy from OldTable where ExperimentID = @oldID)

This mixing of a given value with a subquery does not seem to work. However, there must be a mechanism to do this. Does anyone has some experience with something like this and can maybe help me.

Thanks a lot.

Cheers,
Pankaj


  #2 (permalink)  
Old November 30th, 2003, 10:56 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Pankaj,

I think you need to fix two things: you should leave out VALUES and you need to swap the order of the SQL statement that retrieves the records. This should work:

INSERT INTO MyTable(ExperimentID, Threshold, Energy) SELECT @newID Threshold, Energy FROM OldTable WHERE ExperimentID = @oldID

HtH,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #3 (permalink)  
Old November 30th, 2003, 11:04 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Subqueries are not allowed in the VALUES clause of an INSERT statement. You can use a derived table as the source of the INSERT values instead:
Code:
INSERT INTO MyTable (ExperimentID, Threshold, Energy)
    SELECT @newID, Threshold, Energy
        FROM OldTable WHERE ExperimentID = @oldID;
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
  #4 (permalink)  
Old November 30th, 2003, 03:04 PM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ahhhhhh yeah, I see! Thanks a bunch guys! I really appreciate it.

Sincerely,
Pankaj



Similar Threads
Thread Thread Starter Forum Replies Last Post
Query Question Otacustes VB Databases Basics 6 July 15th, 2008 01:24 PM
sql query question ldp101068 SQL Server 2000 6 December 3rd, 2007 03:41 PM
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
SQL query question in Access dlamarche Access 3 April 3rd, 2005 10:42 PM
SQL Query question darkhalf Classic ASP Databases 4 March 17th, 2004 07:44 PM





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