Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 June 11th, 2008, 04:35 PM
Authorized User
 
Join Date: Sep 2004
Posts: 62
Thanks: 0
Thanked 1 Time in 1 Post
Default INSERT INTO with SubQuery - ERROR! :-(

I am trying to basically copy a record from one table into another, at the same time adding 3 extra columns that the original table does not possess. I have this in a stored procedure like this:

@REC int,
@ChangedIP varchar(15),
@ChangedBY varchar(30)

AS

    INSERT INTO table_LOG
    VALUES ((SELECT * FROM table WHERE record = @REC),GETDATE(),@ChangedIP,@ChangedBY)

I am getting this error:

Subqueries are not allowed in this context. Only scalar expressions are allowed.

How can I achieve this? This is a procedure that will get called right before the original record gets updated in my application so the purpose of this is sort of a "history" table to log the original record along with a few bits of info about the change...

Appreciate any help on this...

 
Old June 11th, 2008, 06:24 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

When you use the VALUES( ) version of INSERT INTO, you can only insert *ONE* record. *AND* you can only have ONE field between eahc set of commmas (or between the "ends" and a comma). So that subquery is returning (a) potentially many records [you know it doesn't, SQL Server doesn't] and (b) many fields.

Happily the solution to what you want to *DO*, not to your question, is easy:
Code:
CREATE PROC whatever
@REC int,
@ChangedIP varchar(15),
@ChangedBY varchar(30)
AS
    INSERT INTO table_LOG
    SELECT *, GETDATE(), @ChangedID, @ChangedBy FROM table WHERE record = @REC
A different "form" of the INSERT INTO works just fine.
 
Old June 12th, 2008, 05:43 PM
Authorized User
 
Join Date: Sep 2004
Posts: 62
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks man - appreciate it as always!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Query Error & Run-Time Error 3022 DavidWE Access 1 July 31st, 2008 11:17 AM
Subquery debbiecoates SQL Server 2000 4 June 25th, 2008 03:49 AM
error in self join with subquery Aneri Oracle ASP 0 March 2nd, 2007 02:26 AM
multi-multi-multiple Insert with subquery??? jmjyiannis SQL Server 2000 1 May 5th, 2006 02:20 PM
subquery khansa MySQL 1 February 21st, 2006 02:49 AM





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